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 18 matching lines...) Expand all Loading... |
29 def main(argv): | 29 def main(argv): |
30 parser = argparse.ArgumentParser(prog='md_browser') | 30 parser = argparse.ArgumentParser(prog='md_browser') |
31 parser.add_argument('-p', '--port', type=int, default=8080, | 31 parser.add_argument('-p', '--port', type=int, default=8080, |
32 help='port to run on (default = %(default)s)') | 32 help='port to run on (default = %(default)s)') |
33 parser.add_argument('-d', '--directory', type=str, default=SRC_DIR) | 33 parser.add_argument('-d', '--directory', type=str, default=SRC_DIR) |
34 parser.add_argument('file', nargs='?', | 34 parser.add_argument('file', nargs='?', |
35 help='open file in browser') | 35 help='open file in browser') |
36 args = parser.parse_args(argv) | 36 args = parser.parse_args(argv) |
37 | 37 |
38 top_level = os.path.realpath(args.directory) | 38 top_level = os.path.realpath(args.directory) |
| 39 server_address = ('localhost', args.port) |
| 40 s = Server(server_address, top_level) |
39 | 41 |
40 s = Server(args.port, top_level) | 42 print('Listening on http://%s:%s/' % server_address) |
41 | |
42 print('Listening on http://localhost:%s/' % args.port) | |
43 thread = None | 43 thread = None |
44 if args.file: | 44 if args.file: |
45 path = os.path.realpath(args.file) | 45 path = os.path.realpath(args.file) |
46 if not path.startswith(top_level): | 46 if not path.startswith(top_level): |
47 print('%s is not under %s' % (args.file, args.directory)) | 47 print('%s is not under %s' % (args.file, args.directory)) |
48 return 1 | 48 return 1 |
49 rpath = os.path.relpath(path, top_level) | 49 rpath = os.path.relpath(path, top_level) |
50 url = 'http://localhost:%d/%s' % (args.port, rpath) | 50 url = 'http://localhost:%d/%s' % (args.port, rpath) |
51 print('Opening %s' % url) | 51 print('Opening %s' % url) |
52 thread = threading.Thread(target=_open_url, args=(url,)) | 52 thread = threading.Thread(target=_open_url, args=(url,)) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 # underscores, just like other non-ASCII characters. | 99 # underscores, just like other non-ASCII characters. |
100 | 100 |
101 value = value.encode('ascii', 'replace') # Non-ASCII turns into '?'. | 101 value = value.encode('ascii', 'replace') # Non-ASCII turns into '?'. |
102 value = re.sub(r'[^- a-zA-Z0-9]', '_', value) # Non-alphanumerics to '_'. | 102 value = re.sub(r'[^- a-zA-Z0-9]', '_', value) # Non-alphanumerics to '_'. |
103 value = value.replace(u' ', u'-') | 103 value = value.replace(u' ', u'-') |
104 value = re.sub(r'([-_])[-_]+', r'\1', value) # Fold hyphens and underscores. | 104 value = re.sub(r'([-_])[-_]+', r'\1', value) # Fold hyphens and underscores. |
105 return value | 105 return value |
106 | 106 |
107 | 107 |
108 class Server(SocketServer.TCPServer): | 108 class Server(SocketServer.TCPServer): |
109 def __init__(self, port, top_level): | 109 def __init__(self, server_address, top_level): |
110 SocketServer.TCPServer.__init__(self, ('0.0.0.0', port), Handler) | 110 SocketServer.TCPServer.__init__(self, server_address, Handler) |
111 self.port = port | |
112 self.top_level = top_level | 111 self.top_level = top_level |
113 self.retcode = None | |
114 | 112 |
115 def server_bind(self): | 113 def server_bind(self): |
116 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | 114 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
117 self.socket.bind(self.server_address) | 115 self.socket.bind(self.server_address) |
118 | 116 |
119 | 117 |
120 class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): | 118 class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): |
121 def do_GET(self): | 119 def do_GET(self): |
122 path = self.path | 120 path = self.path |
123 | 121 |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 contents.text = 'Contents' | 338 contents.text = 'Contents' |
341 contents.tail = '\n' | 339 contents.tail = '\n' |
342 toc_aux = ElementTree.SubElement(toc_node, 'div', {'class': 'toc-aux'}) | 340 toc_aux = ElementTree.SubElement(toc_node, 'div', {'class': 'toc-aux'}) |
343 toc_aux.text = '\n' | 341 toc_aux.text = '\n' |
344 toc_aux.append(ul_with_the_desired_toc_entries) | 342 toc_aux.append(ul_with_the_desired_toc_entries) |
345 toc_aux.tail = '\n' | 343 toc_aux.tail = '\n' |
346 | 344 |
347 | 345 |
348 if __name__ == '__main__': | 346 if __name__ == '__main__': |
349 sys.exit(main(sys.argv[1:])) | 347 sys.exit(main(sys.argv[1:])) |
OLD | NEW |