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 | |
not at google - send to devlin
2014/01/03 03:18:01
leave this here
hukun
2014/01/06 08:25:19
Done
| |
5 import mimetypes | 4 import mimetypes |
6 import os | 5 import os |
7 | 6 |
8 from compiled_file_system import SingleFile | 7 from compiled_file_system import SingleFile |
9 from directory_zipper import DirectoryZipper | 8 from directory_zipper import DirectoryZipper |
10 from docs_server_utils import ToUnicode | 9 from docs_server_utils import ToUnicode |
10 from file_system import FileNotFoundError | |
11 from future import Gettable, Future | 11 from future import Gettable, Future |
12 from third_party.handlebar import Handlebar | 12 from third_party.handlebar import Handlebar |
13 from third_party.markdown import markdown | |
13 | 14 |
14 | 15 |
15 class ContentAndType(object): | 16 class ContentAndType(object): |
16 '''Return value from ContentProvider.GetContentAndType. | 17 '''Return value from ContentProvider.GetContentAndType. |
17 ''' | 18 ''' |
18 | 19 |
19 def __init__(self, content, content_type): | 20 def __init__(self, content, content_type): |
20 self.content = content | 21 self.content = content |
21 self.content_type = content_type | 22 self.content_type = content_type |
22 | 23 |
(...skipping 25 matching lines...) Expand all Loading... | |
48 self._supports_templates = supports_templates | 49 self._supports_templates = supports_templates |
49 if supports_zip: | 50 if supports_zip: |
50 self._directory_zipper = DirectoryZipper(compiled_fs_factory, file_system) | 51 self._directory_zipper = DirectoryZipper(compiled_fs_factory, file_system) |
51 else: | 52 else: |
52 self._directory_zipper = None | 53 self._directory_zipper = None |
53 | 54 |
54 @SingleFile | 55 @SingleFile |
55 def _CompileContent(self, path, text): | 56 def _CompileContent(self, path, text): |
56 assert text is not None, path | 57 assert text is not None, path |
57 mimetype = mimetypes.guess_type(path)[0] | 58 mimetype = mimetypes.guess_type(path)[0] |
58 if mimetype is None: | 59 if os.path.splitext(path)[1] == '.md': |
60 # markdown extensions' introduction in the following url | |
61 # http://pythonhosted.org/Markdown/extensions/index.html | |
not at google - send to devlin
2014/01/03 03:18:01
I can't really parse the comment. How about:
# Se
hukun
2014/01/06 08:25:19
Done
| |
62 content = markdown(ToUnicode(text), extensions=('extra', 'headerid', | |
63 'sane_lists')) | |
not at google - send to devlin
2014/01/03 03:18:01
indentation:
content = markdown(ToUnicode(text),
hukun
2014/01/06 08:25:19
Done
| |
64 if self._supports_templates: | |
65 content = Handlebar(content, name=path) | |
66 mimetype = 'text/html' | |
67 elif mimetype is None: | |
59 content = text | 68 content = text |
60 mimetype = 'text/plain' | 69 mimetype = 'text/plain' |
61 elif mimetype == 'text/html': | 70 elif mimetype == 'text/html': |
62 content = ToUnicode(text) | 71 content = ToUnicode(text) |
63 if self._supports_templates: | 72 if self._supports_templates: |
64 content = Handlebar(content, name=path) | 73 content = Handlebar(content, name=path) |
65 elif (mimetype.startswith('text/') or | 74 elif (mimetype.startswith('text/') or |
66 mimetype in ('application/javascript', 'application/json')): | 75 mimetype in ('application/javascript', 'application/json')): |
67 content = ToUnicode(text) | 76 content = ToUnicode(text) |
68 else: | 77 else: |
69 content = text | 78 content = text |
70 return ContentAndType(content, mimetype) | 79 return ContentAndType(content, mimetype) |
71 | 80 |
81 def _MaybeMarkdown(self, path): | |
82 if os.path.splitext(path)[1] != '.html': | |
not at google - send to devlin
2014/01/03 03:21:41
use posixpath.splitext
hukun
2014/01/06 08:25:19
Done
| |
83 return path | |
84 | |
not at google - send to devlin
2014/01/03 03:18:01
Use posixpath not os.path since the latter will br
hukun
2014/01/06 08:25:19
Done
| |
85 file_list = self.file_system.ReadSingle(os.path.dirname(path) + '/').Get() | |
86 file_name = os.path.basename(path) | |
87 if file_name not in file_list: | |
not at google - send to devlin
2014/01/03 03:18:01
prefer early return like "if file_name in file_lis
hukun
2014/01/06 08:25:19
Done
| |
88 md_file_name = file_name.replace('html', 'md') | |
not at google - send to devlin
2014/01/03 03:18:01
replace .html -> .md not html -> md in case the fi
not at google - send to devlin
2014/01/03 03:21:41
Actually even better may be to write more like
ba
hukun
2014/01/06 08:25:19
Done
hukun
2014/01/06 08:25:19
Done
| |
89 if md_file_name in file_list: | |
90 return path.replace('html', 'md') | |
not at google - send to devlin
2014/01/03 03:18:01
ditto.
hukun
2014/01/06 08:25:19
Done
| |
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._MaybeMarkdown(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 |