| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 """Simple Markdown browser for a Git checkout.""" | 5 """Simple Markdown browser for a Git checkout.""" |
| 6 from __future__ import print_function | 6 from __future__ import print_function |
| 7 | 7 |
| 8 import SimpleHTTPServer | 8 import SimpleHTTPServer |
| 9 import SocketServer | 9 import SocketServer |
| 10 import argparse | 10 import argparse |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 # strip off the repo and branch info, if present, for compatibility | 85 # strip off the repo and branch info, if present, for compatibility |
| 86 # with gitiles. | 86 # with gitiles. |
| 87 if path.startswith('/chromium/src/+/master'): | 87 if path.startswith('/chromium/src/+/master'): |
| 88 path = path[len('/chromium/src/+/master'):] | 88 path = path[len('/chromium/src/+/master'):] |
| 89 | 89 |
| 90 full_path = os.path.abspath(os.path.join(self.server.top_level, path[1:])) | 90 full_path = os.path.abspath(os.path.join(self.server.top_level, path[1:])) |
| 91 | 91 |
| 92 if not full_path.startswith(SRC_DIR): | 92 if not full_path.startswith(SRC_DIR): |
| 93 self._DoUnknown() | 93 self._DoUnknown() |
| 94 elif path == '/doc.css': | 94 elif path == '/doc.css': |
| 95 self._WriteTemplate('doc.css') | 95 self._DoCSS('doc.css') |
| 96 elif not os.path.exists(full_path): | 96 elif not os.path.exists(full_path): |
| 97 self._DoNotFound() | 97 self._DoNotFound() |
| 98 elif path.lower().endswith('.md'): | 98 elif path.lower().endswith('.md'): |
| 99 self._DoMD(path) | 99 self._DoMD(path) |
| 100 elif os.path.exists(full_path + '/README.md'): | 100 elif os.path.exists(full_path + '/README.md'): |
| 101 self._DoMD(path + '/README.md') | 101 self._DoMD(path + '/README.md') |
| 102 else: | 102 else: |
| 103 self._DoUnknown() | 103 self._DoUnknown() |
| 104 | 104 |
| 105 def _DoMD(self, path): | 105 def _DoMD(self, path): |
| 106 extensions = [ | 106 extensions = [ |
| 107 'markdown.extensions.def_list', | 107 'markdown.extensions.def_list', |
| 108 'markdown.extensions.fenced_code', | 108 'markdown.extensions.fenced_code', |
| 109 'markdown.extensions.tables', | 109 'markdown.extensions.tables', |
| 110 'markdown.extensions.toc', | 110 'markdown.extensions.toc', |
| 111 'gitiles_ext_blocks', | 111 'gitiles_ext_blocks', |
| 112 ] | 112 ] |
| 113 extension_configs = { | 113 extension_configs = { |
| 114 'markdown.extensions.toc': { | 114 'markdown.extensions.toc': { |
| 115 'slugify': _gitiles_slugify | 115 'slugify': _gitiles_slugify |
| 116 }, | 116 }, |
| 117 } | 117 } |
| 118 | 118 |
| 119 contents = self._Read(path[1:]) | 119 contents = self._Read(path[1:]) |
| 120 md_fragment = markdown.markdown(contents, | 120 md_fragment = markdown.markdown(contents, |
| 121 extensions=extensions, | 121 extensions=extensions, |
| 122 extension_configs=extension_configs, | 122 extension_configs=extension_configs, |
| 123 output_format='html4').encode('utf-8') | 123 output_format='html4').encode('utf-8') |
| 124 try: | 124 try: |
| 125 self._WriteHeader('text/html') |
| 125 self._WriteTemplate('header.html') | 126 self._WriteTemplate('header.html') |
| 126 self.wfile.write(md_fragment) | 127 self.wfile.write(md_fragment) |
| 127 self._WriteTemplate('footer.html') | 128 self._WriteTemplate('footer.html') |
| 128 except: | 129 except: |
| 129 raise | 130 raise |
| 130 | 131 |
| 132 def _DoCSS(self, template): |
| 133 self._WriteHeader('text/css') |
| 134 self._WriteTemplate(template) |
| 135 |
| 131 def _DoNotFound(self): | 136 def _DoNotFound(self): |
| 137 self._WriteHeader('text/html') |
| 132 self.wfile.write('<html><body>%s not found</body></html>' % self.path) | 138 self.wfile.write('<html><body>%s not found</body></html>' % self.path) |
| 133 | 139 |
| 134 def _DoUnknown(self): | 140 def _DoUnknown(self): |
| 141 self._WriteHeader('text/html') |
| 135 self.wfile.write('<html><body>I do not know how to serve %s.</body>' | 142 self.wfile.write('<html><body>I do not know how to serve %s.</body>' |
| 136 '</html>' % self.path) | 143 '</html>' % self.path) |
| 137 | 144 |
| 138 def _Read(self, relpath): | 145 def _Read(self, relpath): |
| 139 assert not relpath.startswith(os.sep) | 146 assert not relpath.startswith(os.sep) |
| 140 path = os.path.join(self.server.top_level, relpath) | 147 path = os.path.join(self.server.top_level, relpath) |
| 141 with codecs.open(path, encoding='utf-8') as fp: | 148 with codecs.open(path, encoding='utf-8') as fp: |
| 142 return fp.read() | 149 return fp.read() |
| 143 | 150 |
| 151 def _WriteHeader(self, content_type='text/plain'): |
| 152 self.send_response(200) |
| 153 self.send_header('Content-Type', content_type) |
| 154 self.end_headers() |
| 155 |
| 144 def _WriteTemplate(self, template): | 156 def _WriteTemplate(self, template): |
| 145 contents = self._Read(os.path.join('tools', 'md_browser', template)) | 157 contents = self._Read(os.path.join('tools', 'md_browser', template)) |
| 146 self.wfile.write(contents.encode('utf-8')) | 158 self.wfile.write(contents.encode('utf-8')) |
| 147 | 159 |
| 148 | 160 |
| 149 if __name__ == '__main__': | 161 if __name__ == '__main__': |
| 150 sys.exit(main(sys.argv[1:])) | 162 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |