| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from HTMLParser import HTMLParser | 5 from HTMLParser import HTMLParser |
| 6 import mimetypes | 6 import mimetypes |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 | 9 |
| 10 from compiled_file_system import SingleFile | 10 from compiled_file_system import SingleFile |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 @SingleFile | 56 @SingleFile |
| 57 def _CompileContent(self, path, text): | 57 def _CompileContent(self, path, text): |
| 58 assert text is not None, path | 58 assert text is not None, path |
| 59 mimetype = mimetypes.guess_type(path)[0] | 59 mimetype = mimetypes.guess_type(path)[0] |
| 60 if mimetype is None: | 60 if mimetype is None: |
| 61 content = text | 61 content = text |
| 62 mimetype = 'text/plain' | 62 mimetype = 'text/plain' |
| 63 elif mimetype == 'text/html': | 63 elif mimetype == 'text/html': |
| 64 content = ToUnicode(text) | 64 content = ToUnicode(text) |
| 65 if self._supports_templates: | 65 if self._supports_templates: |
| 66 content = Handlebar(content) | 66 content = Handlebar(content, name=path) |
| 67 elif (mimetype.startswith('text/') or | 67 elif (mimetype.startswith('text/') or |
| 68 mimetype in ('application/javascript', 'application/json')): | 68 mimetype in ('application/javascript', 'application/json')): |
| 69 content = ToUnicode(text) | 69 content = ToUnicode(text) |
| 70 else: | 70 else: |
| 71 content = text | 71 content = text |
| 72 return ContentAndType(content, mimetype) | 72 return ContentAndType(content, mimetype) |
| 73 | 73 |
| 74 def GetContentAndType(self, path): | 74 def GetContentAndType(self, path): |
| 75 path = path.lstrip('/') | 75 path = path.lstrip('/') |
| 76 base, ext = os.path.splitext(path) | 76 base, ext = os.path.splitext(path) |
| 77 | 77 |
| 78 # Check for a zip file first, if zip is enabled. | 78 # Check for a zip file first, if zip is enabled. |
| 79 if self._directory_zipper and ext == '.zip': | 79 if self._directory_zipper and ext == '.zip': |
| 80 zip_future = self._directory_zipper.Zip(base) | 80 zip_future = self._directory_zipper.Zip(base) |
| 81 return Future(delegate=Gettable( | 81 return Future(delegate=Gettable( |
| 82 lambda: ContentAndType(zip_future.Get(), 'application/zip'))) | 82 lambda: ContentAndType(zip_future.Get(), 'application/zip'))) |
| 83 | 83 |
| 84 return self._content_cache.GetFromFile(path, binary=True) | 84 return self._content_cache.GetFromFile(path, binary=True) |
| 85 | 85 |
| 86 def Cron(self): | 86 def Cron(self): |
| 87 # Running Refresh() on the file system is enough to pull GitHub content, | 87 # Running Refresh() on the file system is enough to pull GitHub content, |
| 88 # which is all we need for now while the full render-every-page cron step | 88 # which is all we need for now while the full render-every-page cron step |
| 89 # is in effect. | 89 # is in effect. |
| 90 # TODO(kalman): Walk over the whole filesystem and compile the content. | 90 # TODO(kalman): Walk over the whole filesystem and compile the content. |
| 91 return self.file_system.Refresh() | 91 return self.file_system.Refresh() |
| 92 | 92 |
| 93 def __repr__(self): | 93 def __repr__(self): |
| 94 return 'ContentProvider of <%s>' % repr(self.file_system) | 94 return 'ContentProvider of <%s>' % repr(self.file_system) |
| OLD | NEW |