md_browser.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #!/usr/bin/env python3
  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
  4. # found in the LICENSE file.
  5. """Simple Markdown browser for a Git checkout."""
  6. import http.server
  7. import socketserver
  8. import argparse
  9. import codecs
  10. import html
  11. import os
  12. import re
  13. import socket
  14. import sys
  15. import threading
  16. import time
  17. import urllib.request, urllib.parse, urllib.error
  18. import webbrowser
  19. from xml.etree import ElementTree
  20. THIS_DIR = os.path.realpath(os.path.dirname(__file__))
  21. SRC_DIR = os.path.dirname(os.path.dirname(THIS_DIR))
  22. sys.path.insert(0, os.path.join(SRC_DIR, 'third_party', 'Python-Markdown'))
  23. import markdown
  24. def main(argv):
  25. parser = argparse.ArgumentParser(prog='md_browser')
  26. parser.add_argument('-p', '--port', type=int, default=8080,
  27. help='port to run on (default = %(default)s)')
  28. parser.add_argument('-d', '--directory', type=str, default=SRC_DIR)
  29. parser.add_argument('-e', '--external', action='store_true',
  30. help='whether to bind to external port')
  31. parser.add_argument('file', nargs='?',
  32. help='open file in browser')
  33. args = parser.parse_args(argv)
  34. top_level = os.path.realpath(args.directory)
  35. hostname = '0.0.0.0' if args.external else 'localhost'
  36. server_address = (hostname, args.port)
  37. s = Server(server_address, top_level)
  38. origin = 'http://' + hostname
  39. if args.port != 80:
  40. origin += ':%s' % args.port
  41. print('Listening on %s/' % origin)
  42. thread = None
  43. if args.file:
  44. path = os.path.realpath(args.file)
  45. if not path.startswith(top_level):
  46. print('%s is not under %s' % (args.file, args.directory))
  47. return 1
  48. rpath = os.path.relpath(path, top_level)
  49. url = '%s/%s' % (origin, rpath)
  50. print('Opening %s' % url)
  51. thread = threading.Thread(target=_open_url, args=(url,))
  52. thread.start()
  53. elif os.path.isfile(os.path.join(top_level, 'docs', 'README.md')):
  54. print(' Try loading %s/docs/README.md' % origin)
  55. elif os.path.isfile(os.path.join(args.directory, 'README.md')):
  56. print(' Try loading %s/README.md' % origin)
  57. retcode = 1
  58. try:
  59. s.serve_forever()
  60. except KeyboardInterrupt:
  61. retcode = 130
  62. except Exception as e:
  63. print('Exception raised: %s' % str(e))
  64. s.shutdown()
  65. if thread:
  66. thread.join()
  67. return retcode
  68. def _open_url(url):
  69. time.sleep(1)
  70. webbrowser.open(url)
  71. def _gitiles_slugify(value, _separator):
  72. """Convert a string (representing a section title) to URL anchor name.
  73. This function is passed to "toc" extension as an extension option, so we
  74. can emulate the way how Gitiles converts header titles to URL anchors.
  75. Gitiles' official documentation about the conversion is at:
  76. https://gerrit.googlesource.com/gitiles/+/master/Documentation/markdown.md#Named-anchors
  77. Args:
  78. value: The name of a section that is to be converted.
  79. _separator: Unused. This is actually a configurable string that is used
  80. as a replacement character for spaces in the title, typically set to
  81. '-'. Since we emulate Gitiles' way of slugification here, it makes
  82. little sense to have the separator charactor configurable.
  83. """
  84. # TODO(yutak): Implement accent removal. This does not seem easy without
  85. # some library. For now we just make accented characters turn into
  86. # underscores, just like other non-ASCII characters.
  87. def decode_escaped_chars(regex_match):
  88. # Python-Markdown encodes escaped sequences (ex. "\_") as "\x02 (integer
  89. # ascii code) \x03". We decode the integer ascii code to align with Gitiles
  90. # behavior (ex. 95 -> '_').
  91. return chr(int(regex_match.group(1)))
  92. # Non-ASCII turns into '?'.
  93. value = value.encode('ascii', 'replace').decode('ascii')
  94. value = re.sub('\x02(\\d+)\x03', decode_escaped_chars, value)
  95. value = re.sub(r'[^- a-zA-Z0-9]', '_', value) # Non-alphanumerics to '_'.
  96. value = value.replace(' ', '-')
  97. value = re.sub(r'([-_])[-_]+', r'\1', value) # Fold hyphens and underscores.
  98. return value
  99. class Server(socketserver.TCPServer):
  100. def __init__(self, server_address, top_level):
  101. socketserver.TCPServer.__init__(self, server_address, Handler)
  102. self.top_level = top_level
  103. def server_bind(self):
  104. self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  105. self.socket.bind(self.server_address)
  106. class Handler(http.server.SimpleHTTPRequestHandler):
  107. def do_GET(self):
  108. self.path = urllib.parse.unquote(self.path)
  109. path = self.path
  110. # strip off the repo and branch info, if present, for compatibility
  111. # with gitiles.
  112. if path.startswith('/chromium/src/+/master'):
  113. path = path[len('/chromium/src/+/master'):]
  114. full_path = os.path.normpath(os.path.join(self.server.top_level, path[1:]))
  115. if not full_path.startswith(self.server.top_level):
  116. self._DoUnknown()
  117. elif path in ('/base.css', '/doc.css', '/prettify.css'):
  118. self._DoCSS(path[1:])
  119. elif not os.path.exists(full_path):
  120. self._DoNotFound()
  121. elif path.lower().endswith('.md'):
  122. self._DoMD(path)
  123. elif os.path.exists(full_path + '/README.md'):
  124. separator = '/'
  125. if path.endswith('/'):
  126. separator = ''
  127. self._DoMD(path + separator + 'README.md')
  128. elif path.lower().endswith('.png'):
  129. self._DoImage(full_path, 'image/png')
  130. elif path.lower().endswith('.jpg'):
  131. self._DoImage(full_path, 'image/jpeg')
  132. elif path.lower().endswith('.svg'):
  133. self._DoImage(full_path, 'image/svg+xml')
  134. elif os.path.isdir(full_path):
  135. self._DoDirListing(full_path)
  136. elif os.path.exists(full_path):
  137. self._DoRawSourceFile(full_path)
  138. else:
  139. self._DoUnknown()
  140. def _DoMD(self, path):
  141. extensions = [
  142. 'markdown.extensions.def_list',
  143. 'markdown.extensions.fenced_code',
  144. 'markdown.extensions.tables',
  145. 'markdown.extensions.toc',
  146. 'gitiles_autolink',
  147. 'gitiles_ext_blocks',
  148. 'gitiles_smart_quotes',
  149. ]
  150. extension_configs = {
  151. 'markdown.extensions.toc': {
  152. 'slugify': _gitiles_slugify
  153. },
  154. }
  155. contents = self._Read(path[1:])
  156. md = markdown.Markdown(extensions=extensions,
  157. extension_configs=extension_configs,
  158. tab_length=4,
  159. output_format='html4')
  160. has_a_single_h1 = (len([line for line in contents.splitlines()
  161. if (line.startswith('#') and
  162. not line.startswith('##'))]) == 1)
  163. md.treeprocessors.register(_AdjustTOC(has_a_single_h1), 'adjust_toc', 4)
  164. md_fragment = md.convert(contents)
  165. try:
  166. self._WriteHeader('text/html')
  167. self._WriteTemplate('header.html')
  168. self._Write('<div class="doc">')
  169. self._Write(md_fragment)
  170. self._Write('</div>')
  171. self._WriteTemplate('footer.html')
  172. except:
  173. raise
  174. def _DoRawSourceFile(self, full_path):
  175. self._WriteHeader('text/html')
  176. self._WriteTemplate('header.html')
  177. self._Write('<table class="FileContents">')
  178. with open(full_path) as fp:
  179. # Escape html over the entire file at once.
  180. data = fp.read().replace(
  181. '&', '&amp;').replace(
  182. '<', '&lt;').replace(
  183. '>', '&gt;').replace(
  184. '"', '&quot;')
  185. for i, line in enumerate(data.splitlines(), start=1):
  186. self._Write(
  187. ('<tr class="u-pre u-monospace FileContents-line">'
  188. '<td class="u-lineNum u-noSelect FileContents-lineNum">'
  189. '<a name="%(num)s" '
  190. 'onclick="window.location.hash=%(quot)s#%(num)s%(quot)s">'
  191. '%(num)s</a></td>'
  192. '<td class="FileContents-lineContents">%(line)s</td></tr>') % {
  193. 'num': i,
  194. 'quot': "'",
  195. 'line': line
  196. })
  197. self._Write('</table>')
  198. self._WriteTemplate('footer.html')
  199. def _DoCSS(self, template):
  200. self._WriteHeader('text/css')
  201. self._WriteTemplate(template)
  202. def _DoNotFound(self):
  203. self._WriteHeader('text/html', status_code=404)
  204. self._Write('<html><body>%s not found</body></html>' %
  205. html.escape(self.path))
  206. def _DoUnknown(self):
  207. self._WriteHeader('text/html', status_code=501)
  208. self._Write('<html><body>I do not know how to serve %s.</body>'
  209. '</html>' % html.escape(self.path))
  210. def _DoDirListing(self, full_path):
  211. self._WriteHeader('text/html')
  212. self._WriteTemplate('header.html')
  213. self._Write('<div class="doc">')
  214. self._Write('<div class="Breadcrumbs">\n')
  215. self._Write('<a class="Breadcrumbs-crumb">%s</a>\n' %
  216. html.escape(self.path))
  217. self._Write('</div>\n')
  218. escaped_dir = html.escape(self.path.rstrip('/'), quote=True)
  219. for _, dirs, files in os.walk(full_path):
  220. for f in sorted(files):
  221. if f.startswith('.'):
  222. continue
  223. f = html.escape(f, quote=True)
  224. if f.endswith('.md'):
  225. bold = ('<b>', '</b>')
  226. else:
  227. bold = ('', '')
  228. self._Write('<a href="%s/%s">%s%s%s</a><br/>\n' %
  229. (escaped_dir, f, bold[0], f, bold[1]))
  230. self._Write('<br/>\n')
  231. for d in sorted(dirs):
  232. if d.startswith('.'):
  233. continue
  234. d = html.escape(d, quote=True)
  235. self._Write('<a href="%s/%s">%s/</a><br/>\n' % (escaped_dir, d, d))
  236. break
  237. self._Write('</div>')
  238. self._WriteTemplate('footer.html')
  239. def _DoImage(self, full_path, mime_type):
  240. self._WriteHeader(mime_type)
  241. with open(full_path, 'rb') as f:
  242. self.wfile.write(f.read())
  243. def _Read(self, relpath, relative_to=None):
  244. if relative_to is None:
  245. relative_to = self.server.top_level
  246. assert not relpath.startswith(os.sep)
  247. path = os.path.join(relative_to, relpath)
  248. with codecs.open(path, encoding='utf-8') as fp:
  249. return fp.read()
  250. def _Write(self, contents):
  251. self.wfile.write(contents.encode('utf-8'))
  252. def _WriteHeader(self, content_type='text/plain', status_code=200):
  253. self.send_response(status_code)
  254. self.send_header('Content-Type', content_type)
  255. self.end_headers()
  256. def _WriteTemplate(self, template):
  257. contents = self._Read(os.path.join('tools', 'md_browser', template),
  258. relative_to=SRC_DIR)
  259. self._Write(contents)
  260. class _AdjustTOC(markdown.treeprocessors.Treeprocessor):
  261. def __init__(self, has_a_single_h1):
  262. super(_AdjustTOC, self).__init__()
  263. self.has_a_single_h1 = has_a_single_h1
  264. def run(self, tree):
  265. # Given
  266. #
  267. # # H1
  268. #
  269. # [TOC]
  270. #
  271. # ## first H2
  272. #
  273. # ## second H2
  274. #
  275. # the markdown.extensions.toc extension generates:
  276. #
  277. # <div class='toc'>
  278. # <ul><li><a>H1</a>
  279. # <ul><li>first H2
  280. # <li>second H2</li></ul></li><ul></div>
  281. #
  282. # for [TOC]. But, we want the TOC to have its own subheading, so
  283. # we rewrite <div class='toc'><ul>...</ul></div> to:
  284. #
  285. # <div class='toc'>
  286. # <h2>Contents</h2>
  287. # <div class='toc-aux'>
  288. # <ul>...</ul></div></div>
  289. #
  290. # In addition, if the document only has a single H1, it is usually the
  291. # title, and we don't want the title to be in the TOC. So, we remove it
  292. # and shift all of the title's children up a level, leaving:
  293. #
  294. # <div class='toc'>
  295. # <h2>Contents</h2>
  296. # <div class='toc-aux'>
  297. # <ul><li>first H2
  298. # <li>second H2</li></ul></div></div>
  299. for toc_node in tree.findall(".//*[@class='toc']"):
  300. toc_ul = toc_node[0]
  301. if self.has_a_single_h1:
  302. toc_ul_li = toc_ul[0]
  303. ul_with_the_desired_toc_entries = toc_ul_li[1]
  304. else:
  305. ul_with_the_desired_toc_entries = toc_ul
  306. toc_node.remove(toc_ul)
  307. contents = ElementTree.SubElement(toc_node, 'h2')
  308. contents.text = 'Contents'
  309. contents.tail = '\n'
  310. toc_aux = ElementTree.SubElement(toc_node, 'div', {'class': 'toc-aux'})
  311. toc_aux.text = '\n'
  312. toc_aux.append(ul_with_the_desired_toc_entries)
  313. toc_aux.tail = '\n'
  314. if __name__ == '__main__':
  315. sys.exit(main(sys.argv[1:]))