OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 # for details. All rights reserved. Use of this source code is governed by a | |
3 # BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import codecs | |
6 import itertools | |
7 import os | |
8 import re | |
9 | |
10 import cherrypy | |
11 import yaml | |
12 | |
13 import handlers | |
14 | |
15 class Doc(object): | |
16 """The handler for /doc/*.""" | |
17 | |
18 def index(self): | |
19 raise cherrypy.HTTPRedirect('/doc/') | |
20 | |
21 def show(self, filename): | |
22 """Display a documentation page. | |
23 | |
24 Each page is static HTML wrapped in a dynamic layout. The HTML is | |
25 generated offline from Markdown source files in /doc; the titles are | |
26 loaded from those source files as well. | |
27 """ | |
28 | |
29 # Redirect from the old names for the commands. | |
30 if filename == 'pub-install.html': | |
31 raise cherrypy.HTTPRedirect('/doc/pub-get.html') | |
32 | |
33 if filename == 'pub-update.html': | |
34 raise cherrypy.HTTPRedirect('/doc/pub-upgrade.html') | |
35 | |
36 if filename == '': filename = 'index.html' | |
37 root = os.path.join(os.path.dirname(__file__), '..') | |
38 | |
39 html_path = os.path.join(root, 'views', 'doc', filename) | |
40 if not os.path.isfile(html_path): | |
41 handlers.http_error(404) | |
42 | |
43 markdown_filename = re.sub("\.html$", ".markdown", filename) | |
44 markdown_path = os.path.join(root, 'doc', markdown_filename) | |
45 with codecs.open(markdown_path, encoding='utf-8') as f: | |
46 frontmatter = self._frontmatter(f) | |
47 | |
48 with codecs.open(html_path, encoding='utf-8') as f: | |
49 html = """<article> | |
50 <h1>%s</h1> | |
51 %s | |
52 </article>""" % (frontmatter['title'], f.read()) | |
53 return handlers.layout(html, title=frontmatter['title']) | |
54 | |
55 def _frontmatter(self, f): | |
56 """Parses the YAML frontmatter of a file.""" | |
57 if f.readline() != '---\n': return {} | |
58 yaml_lines = itertools.takewhile(lambda line: line != '---\n', f) | |
59 return yaml.load(''.join(yaml_lines)) | |
OLD | NEW |