| 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 import mimetypes | 5 import mimetypes |
| 6 import posixpath | 6 import posixpath |
| 7 | 7 |
| 8 from compiled_file_system import SingleFile | 8 from compiled_file_system import SingleFile |
| 9 from directory_zipper import DirectoryZipper | 9 from directory_zipper import DirectoryZipper |
| 10 from docs_server_utils import ToUnicode | 10 from docs_server_utils import ToUnicode |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 if self._supports_templates: | 80 if self._supports_templates: |
| 81 content = Handlebar(content, name=path) | 81 content = Handlebar(content, name=path) |
| 82 elif (mimetype.startswith('text/') or | 82 elif (mimetype.startswith('text/') or |
| 83 mimetype in ('application/javascript', 'application/json')): | 83 mimetype in ('application/javascript', 'application/json')): |
| 84 content = ToUnicode(text) | 84 content = ToUnicode(text) |
| 85 else: | 85 else: |
| 86 content = text | 86 content = text |
| 87 return ContentAndType(content, mimetype) | 87 return ContentAndType(content, mimetype) |
| 88 | 88 |
| 89 def _MaybeMarkdown(self, path): | 89 def _MaybeMarkdown(self, path): |
| 90 if posixpath.splitext(path)[1] != '.html': | 90 base, ext = posixpath.splitext(path) |
| 91 if ext != '.html': |
| 91 return path | 92 return path |
| 92 | 93 if self.file_system.Exists(path).Get(): |
| 93 dirname, file_name = posixpath.split(path) | |
| 94 if dirname != '': | |
| 95 dirname = dirname + '/' | |
| 96 file_list = self.file_system.ReadSingle(dirname).Get() | |
| 97 if file_name in file_list: | |
| 98 return path | 94 return path |
| 99 | 95 as_md = base + '.md' |
| 100 if posixpath.splitext(file_name)[0] + '.md' in file_list: | 96 if self.file_system.Exists(as_md).Get(): |
| 101 return posixpath.splitext(path)[0] + '.md' | 97 return as_md |
| 102 return path | 98 return path |
| 103 | 99 |
| 104 def GetContentAndType(self, path): | 100 def GetContentAndType(self, path): |
| 105 path = path.lstrip('/') | 101 path = path.lstrip('/') |
| 106 base, ext = posixpath.splitext(path) | 102 base, ext = posixpath.splitext(path) |
| 107 | 103 |
| 108 # Check for a zip file first, if zip is enabled. | 104 # Check for a zip file first, if zip is enabled. |
| 109 if self._directory_zipper and ext == '.zip': | 105 if self._directory_zipper and ext == '.zip': |
| 110 zip_future = self._directory_zipper.Zip(base) | 106 zip_future = self._directory_zipper.Zip(base) |
| 111 return Future(delegate=Gettable( | 107 return Future(delegate=Gettable( |
| 112 lambda: ContentAndType(zip_future.Get(), 'application/zip'))) | 108 lambda: ContentAndType(zip_future.Get(), 'application/zip'))) |
| 113 | 109 |
| 114 return self._content_cache.GetFromFile(self._MaybeMarkdown(path)) | 110 return self._content_cache.GetFromFile(self._MaybeMarkdown(path)) |
| 115 | 111 |
| 116 def Cron(self): | 112 def Cron(self): |
| 117 # Running Refresh() on the file system is enough to pull GitHub content, | 113 # Running Refresh() on the file system is enough to pull GitHub content, |
| 118 # which is all we need for now while the full render-every-page cron step | 114 # which is all we need for now while the full render-every-page cron step |
| 119 # is in effect. | 115 # is in effect. |
| 120 # TODO(kalman): Walk over the whole filesystem and compile the content. | 116 futures = [] |
| 121 return self.file_system.Refresh() | 117 for root, _, files in self.file_system.Walk(''): |
| 118 futures += [self.GetContentAndType(posixpath.join(root, filename)) |
| 119 for filename in files] |
| 120 return Future(delegate=Gettable(lambda: [f.Get() for f in futures])) |
| 122 | 121 |
| 123 def __repr__(self): | 122 def __repr__(self): |
| 124 return 'ContentProvider of <%s>' % repr(self.file_system) | 123 return 'ContentProvider of <%s>' % repr(self.file_system) |
| OLD | NEW |