Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: chrome/common/extensions/docs/server2/content_provider.py

Issue 93743005: Support markdown template for html editor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add _MaybeMarkdown Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 import logging
5 import mimetypes 5 import mimetypes
6 import os 6 import os
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
11 from file_system import FileNotFoundError
11 from future import Gettable, Future 12 from future import Gettable, Future
12 from third_party.handlebar import Handlebar 13 from third_party.handlebar import Handlebar
14 from third_party.markdown import markdown
13 15
14 16
15 class ContentAndType(object): 17 class ContentAndType(object):
16 '''Return value from ContentProvider.GetContentAndType. 18 '''Return value from ContentProvider.GetContentAndType.
17 ''' 19 '''
18 20
19 def __init__(self, content, content_type): 21 def __init__(self, content, content_type):
20 self.content = content 22 self.content = content
21 self.content_type = content_type 23 self.content_type = content_type
22 24
(...skipping 25 matching lines...) Expand all
48 self._supports_templates = supports_templates 50 self._supports_templates = supports_templates
49 if supports_zip: 51 if supports_zip:
50 self._directory_zipper = DirectoryZipper(compiled_fs_factory, file_system) 52 self._directory_zipper = DirectoryZipper(compiled_fs_factory, file_system)
51 else: 53 else:
52 self._directory_zipper = None 54 self._directory_zipper = None
53 55
54 @SingleFile 56 @SingleFile
55 def _CompileContent(self, path, text): 57 def _CompileContent(self, path, text):
56 assert text is not None, path 58 assert text is not None, path
57 mimetype = mimetypes.guess_type(path)[0] 59 mimetype = mimetypes.guess_type(path)[0]
58 if mimetype is None: 60 if os.path.splitext(path)[1] == '.md':
61 # markdown extensions' introduction in the following url
62 # http://pythonhosted.org/Markdown/extensions/index.html
not at google - send to devlin 2013/12/20 11:50:41 Thanks for linking to this! Reading through them,
hukun 2013/12/23 05:12:58 Done
63 content = markdown(ToUnicode(text), extensions=['extra',
64 'admonition', 'codehilite', 'headerid', 'meta', 'nl2br',
65 'sane_lists', 'toc', 'wikilinks'])
66 if self._supports_templates:
67 content = Handlebar(content, name=path)
68 mimetype = 'text/html'
69 elif mimetype is None:
59 content = text 70 content = text
60 mimetype = 'text/plain' 71 mimetype = 'text/plain'
61 elif mimetype == 'text/html': 72 elif mimetype == 'text/html':
62 content = ToUnicode(text) 73 content = ToUnicode(text)
63 if self._supports_templates: 74 if self._supports_templates:
64 content = Handlebar(content, name=path) 75 content = Handlebar(content, name=path)
65 elif (mimetype.startswith('text/') or 76 elif (mimetype.startswith('text/') or
66 mimetype in ('application/javascript', 'application/json')): 77 mimetype in ('application/javascript', 'application/json')):
67 content = ToUnicode(text) 78 content = ToUnicode(text)
68 else: 79 else:
69 content = text 80 content = text
70 return ContentAndType(content, mimetype) 81 return ContentAndType(content, mimetype)
71 82
83 def _MaybeMarkdown(self, path):
84 if os.path.splitext(path)[1] != '.html':
85 return path
86
87 try:
88 self.file_system.ReadSingle(path).Get()
not at google - send to devlin 2013/12/20 11:50:41 I would prefer not to implement this with exceptio
hukun 2013/12/23 05:12:58 Done
89 return path
90 except FileNotFoundError:
91 md_file_path = path.replace('html', 'md')
92 try:
93 self.file_system.ReadSingle(md_file_path).Get()
94 logging.info('%s not found, try markdown templates at %s' % (path,
95 md_file_path))
not at google - send to devlin 2013/12/20 11:50:41 don't worry about logging or anything, just let th
hukun 2013/12/23 05:12:58 Done
96 return md_file_path
97 except FileNotFoundError:
98 return path
99
72 def GetContentAndType(self, path): 100 def GetContentAndType(self, path):
73 path = path.lstrip('/') 101 path = path.lstrip('/')
74 base, ext = os.path.splitext(path) 102 base, ext = os.path.splitext(path)
75 103
76 # Check for a zip file first, if zip is enabled. 104 # Check for a zip file first, if zip is enabled.
77 if self._directory_zipper and ext == '.zip': 105 if self._directory_zipper and ext == '.zip':
78 zip_future = self._directory_zipper.Zip(base) 106 zip_future = self._directory_zipper.Zip(base)
79 return Future(delegate=Gettable( 107 return Future(delegate=Gettable(
80 lambda: ContentAndType(zip_future.Get(), 'application/zip'))) 108 lambda: ContentAndType(zip_future.Get(), 'application/zip')))
81 109
82 return self._content_cache.GetFromFile(path) 110 return self._content_cache.GetFromFile(self._MaybeMarkdown(path))
83 111
84 def Cron(self): 112 def Cron(self):
85 # 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,
86 # 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
87 # is in effect. 115 # is in effect.
88 # TODO(kalman): Walk over the whole filesystem and compile the content. 116 # TODO(kalman): Walk over the whole filesystem and compile the content.
89 return self.file_system.Refresh() 117 return self.file_system.Refresh()
90 118
91 def __repr__(self): 119 def __repr__(self):
92 return 'ContentProvider of <%s>' % repr(self.file_system) 120 return 'ContentProvider of <%s>' % repr(self.file_system)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698