Chromium Code Reviews| 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 os | 6 import os |
| 7 import posixpath | |
| 7 | 8 |
| 8 from compiled_file_system import SingleFile | 9 from compiled_file_system import SingleFile |
| 9 from directory_zipper import DirectoryZipper | 10 from directory_zipper import DirectoryZipper |
| 10 from docs_server_utils import ToUnicode | 11 from docs_server_utils import ToUnicode |
| 12 from file_system import FileNotFoundError | |
| 11 from future import Gettable, Future | 13 from future import Gettable, Future |
| 12 from third_party.handlebar import Handlebar | 14 from third_party.handlebar import Handlebar |
| 15 from third_party.markdown import markdown | |
| 13 | 16 |
| 14 | 17 |
| 15 class ContentAndType(object): | 18 class ContentAndType(object): |
| 16 '''Return value from ContentProvider.GetContentAndType. | 19 '''Return value from ContentProvider.GetContentAndType. |
| 17 ''' | 20 ''' |
| 18 | 21 |
| 19 def __init__(self, content, content_type): | 22 def __init__(self, content, content_type): |
| 20 self.content = content | 23 self.content = content |
| 21 self.content_type = content_type | 24 self.content_type = content_type |
| 22 | 25 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 51 else: | 54 else: |
| 52 self._directory_zipper = None | 55 self._directory_zipper = None |
| 53 | 56 |
| 54 @SingleFile | 57 @SingleFile |
| 55 def _CompileContent(self, path, text): | 58 def _CompileContent(self, path, text): |
| 56 assert text is not None, path | 59 assert text is not None, path |
| 57 mimetype = mimetypes.guess_type(path)[0] | 60 mimetype = mimetypes.guess_type(path)[0] |
| 58 if mimetype is None: | 61 if mimetype is None: |
| 59 content = text | 62 content = text |
| 60 mimetype = 'text/plain' | 63 mimetype = 'text/plain' |
| 64 if path.endswith(".md"): | |
| 65 content = markdown(content, extensions=['extra', 'admonition', | |
|
not at google - send to devlin
2013/12/20 03:56:43
do you need ToUnicode(content) here?
hukun
2013/12/20 07:41:53
yes, more safe.
| |
| 66 'codehilite', 'headerid', 'meta', 'nl2br', 'sane_lists', | |
| 67 'toc', 'wikilinks']) | |
|
not at google - send to devlin
2013/12/20 03:56:43
what do all of these mean? can you link to some do
hukun
2013/12/20 07:41:53
Done
| |
| 68 mimetype = 'text/html' | |
| 69 if self._supports_templates: | |
| 70 content = Handlebar(content, name=path) | |
|
not at google - send to devlin
2013/12/20 03:56:43
how about doing this outside the mimetype is None?
hukun
2013/12/20 07:41:53
Done
| |
| 61 elif mimetype == 'text/html': | 71 elif mimetype == 'text/html': |
| 62 content = ToUnicode(text) | 72 content = ToUnicode(text) |
| 63 if self._supports_templates: | 73 if self._supports_templates: |
| 64 content = Handlebar(content, name=path) | 74 content = Handlebar(content, name=path) |
| 65 elif (mimetype.startswith('text/') or | 75 elif (mimetype.startswith('text/') or |
| 66 mimetype in ('application/javascript', 'application/json')): | 76 mimetype in ('application/javascript', 'application/json')): |
| 67 content = ToUnicode(text) | 77 content = ToUnicode(text) |
| 68 else: | 78 else: |
| 69 content = text | 79 content = text |
| 70 return ContentAndType(content, mimetype) | 80 return ContentAndType(content, mimetype) |
| 71 | 81 |
| 82 def _CheckHTMLFileExists(self, path): | |
|
not at google - send to devlin
2013/12/20 03:56:43
how about _MaybeMarkdown or something? this doesn'
hukun
2013/12/20 07:41:53
Done
| |
| 83 dir_path, file_path = posixpath.split(path) | |
| 84 if dir_path and not dir_path.endswith('/'): | |
| 85 dir_path += '/' | |
| 86 | |
|
not at google - send to devlin
2013/12/20 03:56:43
use os.path.splitext, handy, and you can optimise
hukun
2013/12/20 07:41:53
Done
| |
| 87 dir_stat = self.file_system.Stat(dir_path) | |
|
not at google - send to devlin
2013/12/20 03:56:43
ReadSingle would be easier than Stat here.
hukun
2013/12/20 07:41:53
Done
| |
| 88 if not dir_stat.child_versions.get(file_path): | |
| 89 path = path.replace('html', 'md') | |
|
not at google - send to devlin
2013/12/20 03:56:43
the logic should be probably be that IF it's an ht
hukun
2013/12/20 07:41:53
Done
| |
| 90 | |
| 91 return path | |
| 92 | |
| 72 def GetContentAndType(self, path): | 93 def GetContentAndType(self, path): |
| 73 path = path.lstrip('/') | 94 path = path.lstrip('/') |
| 74 base, ext = os.path.splitext(path) | 95 base, ext = os.path.splitext(path) |
| 75 | 96 |
| 76 # Check for a zip file first, if zip is enabled. | 97 # Check for a zip file first, if zip is enabled. |
| 77 if self._directory_zipper and ext == '.zip': | 98 if self._directory_zipper and ext == '.zip': |
| 78 zip_future = self._directory_zipper.Zip(base) | 99 zip_future = self._directory_zipper.Zip(base) |
| 79 return Future(delegate=Gettable( | 100 return Future(delegate=Gettable( |
| 80 lambda: ContentAndType(zip_future.Get(), 'application/zip'))) | 101 lambda: ContentAndType(zip_future.Get(), 'application/zip'))) |
| 81 | 102 |
| 82 return self._content_cache.GetFromFile(path) | 103 return self._content_cache.GetFromFile(self._CheckHTMLFileExists(path)) |
| 83 | 104 |
| 84 def Cron(self): | 105 def Cron(self): |
| 85 # Running Refresh() on the file system is enough to pull GitHub content, | 106 # Running Refresh() on the file system is enough to pull GitHub content, |
| 86 # which is all we need for now while the full render-every-page cron step | 107 # which is all we need for now while the full render-every-page cron step |
| 87 # is in effect. | 108 # is in effect. |
| 88 # TODO(kalman): Walk over the whole filesystem and compile the content. | 109 # TODO(kalman): Walk over the whole filesystem and compile the content. |
| 89 return self.file_system.Refresh() | 110 return self.file_system.Refresh() |
| 90 | 111 |
| 91 def __repr__(self): | 112 def __repr__(self): |
| 92 return 'ContentProvider of <%s>' % repr(self.file_system) | 113 return 'ContentProvider of <%s>' % repr(self.file_system) |
| OLD | NEW |