| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Simple Markdown browser for a Git checkout.""" | 6 """Simple Markdown browser for a Git checkout.""" |
| 7 from __future__ import print_function | 7 from __future__ import print_function |
| 8 | 8 |
| 9 import SimpleHTTPServer | 9 import SimpleHTTPServer |
| 10 import SocketServer | 10 import SocketServer |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 if not full_path.startswith(self.server.top_level): | 131 if not full_path.startswith(self.server.top_level): |
| 132 self._DoUnknown() | 132 self._DoUnknown() |
| 133 elif path in ('/base.css', '/doc.css', '/prettify.css'): | 133 elif path in ('/base.css', '/doc.css', '/prettify.css'): |
| 134 self._DoCSS(path[1:]) | 134 self._DoCSS(path[1:]) |
| 135 elif not os.path.exists(full_path): | 135 elif not os.path.exists(full_path): |
| 136 self._DoNotFound() | 136 self._DoNotFound() |
| 137 elif path.lower().endswith('.md'): | 137 elif path.lower().endswith('.md'): |
| 138 self._DoMD(path) | 138 self._DoMD(path) |
| 139 elif os.path.exists(full_path + '/README.md'): | 139 elif os.path.exists(full_path + '/README.md'): |
| 140 self._DoMD(path + '/README.md') | 140 self._DoMD(path + '/README.md') |
| 141 elif path.lower().endswith('.png'): |
| 142 self._DoImage(full_path, 'image/png') |
| 143 elif path.lower().endswith('.jpg'): |
| 144 self._DoImage(full_path, 'image/jpeg') |
| 141 else: | 145 else: |
| 142 self._DoDirListing(full_path) | 146 self._DoDirListing(full_path) |
| 143 | 147 |
| 144 def _DoMD(self, path): | 148 def _DoMD(self, path): |
| 145 extensions = [ | 149 extensions = [ |
| 146 'markdown.extensions.def_list', | 150 'markdown.extensions.def_list', |
| 147 'markdown.extensions.fenced_code', | 151 'markdown.extensions.fenced_code', |
| 148 'markdown.extensions.tables', | 152 'markdown.extensions.tables', |
| 149 'markdown.extensions.toc', | 153 'markdown.extensions.toc', |
| 150 'gitiles_autolink', | 154 'gitiles_autolink', |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 for d in sorted(dirs): | 220 for d in sorted(dirs): |
| 217 if d.startswith('.'): | 221 if d.startswith('.'): |
| 218 continue | 222 continue |
| 219 self.wfile.write('<a href="%s/%s">%s/</a><br/>\n' % | 223 self.wfile.write('<a href="%s/%s">%s/</a><br/>\n' % |
| 220 (self.path.rstrip('/'), d, d)) | 224 (self.path.rstrip('/'), d, d)) |
| 221 | 225 |
| 222 break | 226 break |
| 223 | 227 |
| 224 self._WriteTemplate('footer.html') | 228 self._WriteTemplate('footer.html') |
| 225 | 229 |
| 230 def _DoImage(self, full_path, mime_type): |
| 231 self._WriteHeader(mime_type) |
| 232 with open(full_path) as f: |
| 233 self.wfile.write(f.read()) |
| 234 f.close() |
| 235 |
| 226 def _Read(self, relpath, relative_to=None): | 236 def _Read(self, relpath, relative_to=None): |
| 227 if relative_to is None: | 237 if relative_to is None: |
| 228 relative_to = self.server.top_level | 238 relative_to = self.server.top_level |
| 229 assert not relpath.startswith(os.sep) | 239 assert not relpath.startswith(os.sep) |
| 230 path = os.path.join(relative_to, relpath) | 240 path = os.path.join(relative_to, relpath) |
| 231 with codecs.open(path, encoding='utf-8') as fp: | 241 with codecs.open(path, encoding='utf-8') as fp: |
| 232 return fp.read() | 242 return fp.read() |
| 233 | 243 |
| 234 def _WriteHeader(self, content_type='text/plain', status_code=200): | 244 def _WriteHeader(self, content_type='text/plain', status_code=200): |
| 235 self.send_response(status_code) | 245 self.send_response(status_code) |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 contents.text = 'Contents' | 306 contents.text = 'Contents' |
| 297 contents.tail = '\n' | 307 contents.tail = '\n' |
| 298 toc_aux = ElementTree.SubElement(toc_node, 'div', {'class': 'toc-aux'}) | 308 toc_aux = ElementTree.SubElement(toc_node, 'div', {'class': 'toc-aux'}) |
| 299 toc_aux.text = '\n' | 309 toc_aux.text = '\n' |
| 300 toc_aux.append(ul_with_the_desired_toc_entries) | 310 toc_aux.append(ul_with_the_desired_toc_entries) |
| 301 toc_aux.tail = '\n' | 311 toc_aux.tail = '\n' |
| 302 | 312 |
| 303 | 313 |
| 304 if __name__ == '__main__': | 314 if __name__ == '__main__': |
| 305 sys.exit(main(sys.argv[1:])) | 315 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |