Mercurial > jouvence
comparison fontaine/html.py @ 9:a5488b474c6b
Add HTML renderer.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 04 Jan 2017 02:56:08 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
8:02d2e4d8b0c1 | 9:a5488b474c6b |
---|---|
1 import os.path | |
2 from markupsafe import escape | |
3 from .renderer import BaseDocumentRenderer, BaseTextRenderer | |
4 | |
5 | |
6 def _elem(out, elem_name, class_name, contents): | |
7 f = out.write | |
8 f('<%s' % elem_name) | |
9 if class_name: | |
10 f(' class="fontaine-%s"' % class_name) | |
11 f('>') | |
12 f(contents) | |
13 f('</%s>\n' % elem_name) | |
14 | |
15 | |
16 def _br(text, strip_first=False): | |
17 lines = text.split('\n') | |
18 if strip_first and lines[0].strip() == '': | |
19 lines = lines[1:] | |
20 return '<br/>\n'.join(lines) | |
21 | |
22 | |
23 def _res(filename): | |
24 path = os.path.join(os.path.dirname(__file__), 'resources', filename) | |
25 with open(path, 'r') as fp: | |
26 return fp.read() | |
27 | |
28 | |
29 class HtmlDocumentRenderer(BaseDocumentRenderer): | |
30 def __init__(self, standalone=True): | |
31 super().__init__(HtmlTextRenderer()) | |
32 self.standalone = standalone | |
33 | |
34 def get_css(self): | |
35 return _res('html_styles.css') | |
36 | |
37 def write_header(self, doc, out): | |
38 if self.standalone: | |
39 meta = doc.title_values.get | |
40 data = { | |
41 # TODO: need a "strip formatting" to have a clean title. | |
42 'title': meta('title', "Fountain Screenplay"), | |
43 'description': meta('description', ''), | |
44 'css': self.get_css() | |
45 } | |
46 out.write(_res('html_header.html') % data) | |
47 out.write('<div class="fontaine-doc">\n') | |
48 | |
49 def write_footer(self, doc, out): | |
50 out.write('</div>\n') | |
51 if self.standalone: | |
52 out.write(_res('html_footer.html')) | |
53 | |
54 def write_title_page(self, values, out): | |
55 out.write('<div class="fontaine-title-page">\n') | |
56 | |
57 _elem(out, 'h1', None, _br(values['title'])) | |
58 _elem(out, 'p', 'title-page-heading', _br(values['credit'])) | |
59 _elem(out, 'p', 'title-page-heading', _br(values['author'])) | |
60 | |
61 ddate = values.get('date') or values.get('draft date') | |
62 if ddate: | |
63 _elem(out, 'p', 'title-page-footer', _br(ddate)) | |
64 contact = values.get('contact') | |
65 if contact: | |
66 _elem(out, 'p', 'title-page-footer', _br(contact)) | |
67 | |
68 out.write('</div>\n') | |
69 self.write_pagebreak(out) | |
70 | |
71 def write_scene_heading(self, text, out): | |
72 _elem(out, 'p', 'scene-heading', text) | |
73 | |
74 def write_action(self, text, out): | |
75 _elem(out, 'p', 'action', _br(text, True)) | |
76 | |
77 def write_centeredaction(self, text, out): | |
78 _elem(out, 'p', 'action-centered', _br(text, True)) | |
79 | |
80 def write_character(self, text, out): | |
81 _elem(out, 'p', 'character', text) | |
82 | |
83 def write_dialog(self, text, out): | |
84 _elem(out, 'p', 'dialog', _br(text)) | |
85 | |
86 def write_parenthetical(self, text, out): | |
87 _elem(out, 'p', 'parenthetical', text) | |
88 | |
89 def write_transition(self, text, out): | |
90 _elem(out, 'p', 'transition', text) | |
91 | |
92 def write_lyrics(self, text, out): | |
93 _elem(out, 'p', 'lyrics', _br(text, True)) | |
94 | |
95 def write_pagebreak(self, out): | |
96 out.write('<hr/>\n') | |
97 | |
98 | |
99 class HtmlTextRenderer(BaseTextRenderer): | |
100 def render_text(self, text): | |
101 return super().render_text(escape(text)) | |
102 | |
103 def make_italics(self, text): | |
104 return '<em>%s</em>' % text | |
105 | |
106 def make_bold(self, text): | |
107 return '<strong>%s</strong>' % text | |
108 | |
109 def make_underline(self, text): | |
110 return '<u>%s</u>' % text |