OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright 2014 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 | |
6 import collections | |
7 import os | |
8 import re | |
9 import sys | |
10 | |
11 from xml.sax.saxutils import escape | |
12 | |
13 from cStringIO import StringIO | |
14 | |
15 if not os.path.exists('ansi2html'): | |
16 print 'You must run ./make_docs.sh once before running this script.' | |
17 sys.exit(1) | |
18 | |
19 # This dependency is pulled in by make_docs.sh | |
20 # if it doesn't exist, run ./make_docs.sh first | |
21 sys.path.insert(0, 'ansi2html') | |
22 | |
23 import ansi2html # pylint: disable=F0401, W0611 | |
24 import ansi2html.converter # pylint: disable=F0401, W0611 | |
25 | |
26 def simpleXML(string): | |
27 BRIGHT = 1 | |
28 DIM = 2 | |
29 NORMAL = 22 | |
30 RESET = 0 | |
31 ESC_RE = re.compile('(\x1B\\[[^m]*?)m') | |
32 | |
33 ret = StringIO() | |
34 boldstate = False | |
35 | |
36 for tok in ESC_RE.split(string): | |
37 if not tok: | |
38 continue | |
39 if tok[0] == '\x1b': | |
40 codes = map(int, filter(bool, tok[2:].split(';'))) | |
41 if not codes: | |
42 codes = [RESET] | |
43 for code in codes: | |
44 # only care about Bright | |
45 if code == BRIGHT and boldstate is False: | |
46 boldstate = True | |
47 ret.write('<emphasis role="strong">') | |
48 elif code in (DIM, NORMAL, RESET) and boldstate: | |
49 boldstate = False | |
50 ret.write('</emphasis>') | |
51 else: | |
52 ret.write(escape(tok)) | |
53 | |
54 if boldstate: | |
55 ret.write('</emphasis>') | |
56 | |
57 return ret.getvalue() | |
58 | |
59 | |
60 def main(): | |
61 ansi2html.converter.SCHEME['custom'] = ( | |
62 "#000000", "#e42e16", "#19c518", "#e7e71c", "#492ee1", | |
63 "#d338d3", "#33d6e5", "#ffffff", | |
64 ) | |
65 | |
66 backend = sys.argv[1] | |
67 output = sys.stdin.read().rstrip() | |
68 | |
69 callout_re = re.compile('\x1b\[(\d+)c\n') | |
70 callouts = collections.defaultdict(int) | |
71 for i, line in enumerate(output.splitlines(True)): | |
72 m = callout_re.match(line) | |
73 if m: | |
74 callouts[i + int(m.group(1)) - len(callouts)] += 1 | |
75 | |
76 output = callout_re.sub('', output) | |
77 | |
78 w = sys.stdout.write | |
79 | |
80 comment_marker = '###COMMENT###' | |
81 | |
82 callout_counter = 1 | |
83 if backend == 'xhtml11': | |
84 preamble = ( | |
85 '</p></div><div class="listingblock"><div class="content"><pre><code>' | |
86 ) | |
87 postamble = '</code></pre></div></div><p><div class="paragraph">' | |
88 c = ansi2html.Ansi2HTMLConverter(inline=True, scheme='custom') | |
89 | |
90 in_code = False | |
91 body = c.convert(output, full=False) | |
92 for i, line in enumerate(body.splitlines()): | |
93 if line.startswith(comment_marker): | |
94 if in_code: | |
95 w(postamble) | |
96 in_code = False | |
97 w(line[len(comment_marker):]) | |
98 else: | |
99 if not in_code: | |
100 w(preamble) | |
101 in_code = True | |
102 ext = '' | |
103 for _ in xrange(callouts[i]): | |
104 if not ext: | |
105 ext += '</span>' | |
106 ext += ' <b><%d></b>' % callout_counter | |
107 callout_counter += 1 | |
108 if ext: | |
109 ext += '<span>' | |
110 w(line + ext + '\n') | |
111 if in_code: | |
112 w(postamble) | |
113 else: | |
114 preamble = '</simpara><literallayout class="monospaced">' | |
115 postamble = '</literallayout><simpara>' | |
116 | |
117 in_code = False | |
118 body = simpleXML(output) | |
119 for i, line in enumerate(body.splitlines()): | |
120 if line.startswith(comment_marker): | |
121 if in_code: | |
122 w(postamble) | |
123 in_code = False | |
124 w(line[len(comment_marker):]) | |
125 else: | |
126 if not in_code: | |
127 w(preamble) | |
128 in_code = True | |
129 ext = '' | |
130 for _ in xrange(callouts[i]): | |
131 ext += ' <emphasis role="strong">(%d)</emphasis>' % callout_counter | |
132 callout_counter += 1 | |
133 w(line + ext + '\n') | |
134 if in_code: | |
135 w(postamble) | |
136 | |
137 | |
138 if __name__ == '__main__': | |
139 main() | |
OLD | NEW |