OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 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 callout_counter = 1 |
| 81 if backend == 'xhtml11': |
| 82 preamble = ( |
| 83 '</p></div><div class="listingblock"><div class="content"><pre><code>' |
| 84 ) |
| 85 postamble = '</code></pre></div></div><p><div class="paragraph">' |
| 86 c = ansi2html.Ansi2HTMLConverter(inline=True, scheme='custom') |
| 87 |
| 88 in_code = False |
| 89 body = c.convert(output, full=False) |
| 90 for i, line in enumerate(body.splitlines()): |
| 91 if line.startswith('# '): |
| 92 if in_code: |
| 93 w(postamble) |
| 94 in_code = False |
| 95 w(line[1:]) |
| 96 else: |
| 97 if not in_code: |
| 98 w(preamble) |
| 99 in_code = True |
| 100 ext = '' |
| 101 for _ in xrange(callouts[i]): |
| 102 if not ext: |
| 103 ext += '</span>' |
| 104 ext += ' <b><%d></b>' % callout_counter |
| 105 callout_counter += 1 |
| 106 if ext: |
| 107 ext += '<span>' |
| 108 w(line + ext + '\n') |
| 109 if in_code: |
| 110 w(postamble) |
| 111 else: |
| 112 preamble = '</simpara><literallayout class="monospaced">' |
| 113 postamble = '</literallayout><simpara>' |
| 114 |
| 115 in_code = False |
| 116 body = simpleXML(output) |
| 117 for i, line in enumerate(body.splitlines()): |
| 118 if line.startswith('# '): |
| 119 if in_code: |
| 120 w(postamble) |
| 121 in_code = False |
| 122 w(line[1:]) |
| 123 else: |
| 124 if not in_code: |
| 125 w(preamble) |
| 126 in_code = True |
| 127 ext = '' |
| 128 for _ in xrange(callouts[i]): |
| 129 ext += ' <emphasis role="strong">(%d)</emphasis>' % callout_counter |
| 130 callout_counter += 1 |
| 131 w(line + ext + '\n') |
| 132 if in_code: |
| 133 w(postamble) |
| 134 |
| 135 |
| 136 if __name__ == '__main__': |
| 137 main() |
OLD | NEW |