ยปCore Development>Code coverage>Tools/scripts/highlight.py

Python code coverage for Tools/scripts/highlight.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a'''Add syntax highlighting to Python source code'''
3n/a
4n/a__author__ = 'Raymond Hettinger'
5n/a
6n/aimport builtins
7n/aimport functools
8n/aimport html as html_module
9n/aimport keyword
10n/aimport re
11n/aimport tokenize
12n/a
13n/a#### Analyze Python Source #################################
14n/a
15n/adef is_builtin(s):
16n/a 'Return True if s is the name of a builtin'
17n/a return hasattr(builtins, s)
18n/a
19n/adef combine_range(lines, start, end):
20n/a 'Join content from a range of lines between start and end'
21n/a (srow, scol), (erow, ecol) = start, end
22n/a if srow == erow:
23n/a return lines[srow-1][scol:ecol], end
24n/a rows = [lines[srow-1][scol:]] + lines[srow: erow-1] + [lines[erow-1][:ecol]]
25n/a return ''.join(rows), end
26n/a
27n/adef analyze_python(source):
28n/a '''Generate and classify chunks of Python for syntax highlighting.
29n/a Yields tuples in the form: (category, categorized_text).
30n/a '''
31n/a lines = source.splitlines(True)
32n/a lines.append('')
33n/a readline = functools.partial(next, iter(lines), '')
34n/a kind = tok_str = ''
35n/a tok_type = tokenize.COMMENT
36n/a written = (1, 0)
37n/a for tok in tokenize.generate_tokens(readline):
38n/a prev_tok_type, prev_tok_str = tok_type, tok_str
39n/a tok_type, tok_str, (srow, scol), (erow, ecol), logical_lineno = tok
40n/a kind = ''
41n/a if tok_type == tokenize.COMMENT:
42n/a kind = 'comment'
43n/a elif tok_type == tokenize.OP and tok_str[:1] not in '{}[](),.:;@':
44n/a kind = 'operator'
45n/a elif tok_type == tokenize.STRING:
46n/a kind = 'string'
47n/a if prev_tok_type == tokenize.INDENT or scol==0:
48n/a kind = 'docstring'
49n/a elif tok_type == tokenize.NAME:
50n/a if tok_str in ('def', 'class', 'import', 'from'):
51n/a kind = 'definition'
52n/a elif prev_tok_str in ('def', 'class'):
53n/a kind = 'defname'
54n/a elif keyword.iskeyword(tok_str):
55n/a kind = 'keyword'
56n/a elif is_builtin(tok_str) and prev_tok_str != '.':
57n/a kind = 'builtin'
58n/a if kind:
59n/a text, written = combine_range(lines, written, (srow, scol))
60n/a yield '', text
61n/a text, written = tok_str, (erow, ecol)
62n/a yield kind, text
63n/a line_upto_token, written = combine_range(lines, written, (erow, ecol))
64n/a yield '', line_upto_token
65n/a
66n/a#### Raw Output ###########################################
67n/a
68n/adef raw_highlight(classified_text):
69n/a 'Straight text display of text classifications'
70n/a result = []
71n/a for kind, text in classified_text:
72n/a result.append('%15s: %r\n' % (kind or 'plain', text))
73n/a return ''.join(result)
74n/a
75n/a#### ANSI Output ###########################################
76n/a
77n/adefault_ansi = {
78n/a 'comment': ('\033[0;31m', '\033[0m'),
79n/a 'string': ('\033[0;32m', '\033[0m'),
80n/a 'docstring': ('\033[0;32m', '\033[0m'),
81n/a 'keyword': ('\033[0;33m', '\033[0m'),
82n/a 'builtin': ('\033[0;35m', '\033[0m'),
83n/a 'definition': ('\033[0;33m', '\033[0m'),
84n/a 'defname': ('\033[0;34m', '\033[0m'),
85n/a 'operator': ('\033[0;33m', '\033[0m'),
86n/a}
87n/a
88n/adef ansi_highlight(classified_text, colors=default_ansi):
89n/a 'Add syntax highlighting to source code using ANSI escape sequences'
90n/a # http://en.wikipedia.org/wiki/ANSI_escape_code
91n/a result = []
92n/a for kind, text in classified_text:
93n/a opener, closer = colors.get(kind, ('', ''))
94n/a result += [opener, text, closer]
95n/a return ''.join(result)
96n/a
97n/a#### HTML Output ###########################################
98n/a
99n/adef html_highlight(classified_text,opener='<pre class="python">\n', closer='</pre>\n'):
100n/a 'Convert classified text to an HTML fragment'
101n/a result = [opener]
102n/a for kind, text in classified_text:
103n/a if kind:
104n/a result.append('<span class="%s">' % kind)
105n/a result.append(html_module.escape(text))
106n/a if kind:
107n/a result.append('</span>')
108n/a result.append(closer)
109n/a return ''.join(result)
110n/a
111n/adefault_css = {
112n/a '.comment': '{color: crimson;}',
113n/a '.string': '{color: forestgreen;}',
114n/a '.docstring': '{color: forestgreen; font-style:italic;}',
115n/a '.keyword': '{color: darkorange;}',
116n/a '.builtin': '{color: purple;}',
117n/a '.definition': '{color: darkorange; font-weight:bold;}',
118n/a '.defname': '{color: blue;}',
119n/a '.operator': '{color: brown;}',
120n/a}
121n/a
122n/adefault_html = '''\
123n/a<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
124n/a "http://www.w3.org/TR/html4/strict.dtd">
125n/a<html>
126n/a<head>
127n/a<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
128n/a<title> {title} </title>
129n/a<style type="text/css">
130n/a{css}
131n/a</style>
132n/a</head>
133n/a<body>
134n/a{body}
135n/a</body>
136n/a</html>
137n/a'''
138n/a
139n/adef build_html_page(classified_text, title='python',
140n/a css=default_css, html=default_html):
141n/a 'Create a complete HTML page with colorized source code'
142n/a css_str = '\n'.join(['%s %s' % item for item in css.items()])
143n/a result = html_highlight(classified_text)
144n/a title = html_module.escape(title)
145n/a return html.format(title=title, css=css_str, body=result)
146n/a
147n/a#### LaTeX Output ##########################################
148n/a
149n/adefault_latex_commands = {
150n/a 'comment': r'{\color{red}#1}',
151n/a 'string': r'{\color{ForestGreen}#1}',
152n/a 'docstring': r'{\emph{\color{ForestGreen}#1}}',
153n/a 'keyword': r'{\color{orange}#1}',
154n/a 'builtin': r'{\color{purple}#1}',
155n/a 'definition': r'{\color{orange}#1}',
156n/a 'defname': r'{\color{blue}#1}',
157n/a 'operator': r'{\color{brown}#1}',
158n/a}
159n/a
160n/adefault_latex_document = r'''
161n/a\documentclass{article}
162n/a\usepackage{alltt}
163n/a\usepackage{upquote}
164n/a\usepackage{color}
165n/a\usepackage[usenames,dvipsnames]{xcolor}
166n/a\usepackage[cm]{fullpage}
167n/a%(macros)s
168n/a\begin{document}
169n/a\center{\LARGE{%(title)s}}
170n/a\begin{alltt}
171n/a%(body)s
172n/a\end{alltt}
173n/a\end{document}
174n/a'''
175n/a
176n/adef alltt_escape(s):
177n/a 'Replace backslash and braces with their escaped equivalents'
178n/a xlat = {'{': r'\{', '}': r'\}', '\\': r'\textbackslash{}'}
179n/a return re.sub(r'[\\{}]', lambda mo: xlat[mo.group()], s)
180n/a
181n/adef latex_highlight(classified_text, title = 'python',
182n/a commands = default_latex_commands,
183n/a document = default_latex_document):
184n/a 'Create a complete LaTeX document with colorized source code'
185n/a macros = '\n'.join(r'\newcommand{\py%s}[1]{%s}' % c for c in commands.items())
186n/a result = []
187n/a for kind, text in classified_text:
188n/a if kind:
189n/a result.append(r'\py%s{' % kind)
190n/a result.append(alltt_escape(text))
191n/a if kind:
192n/a result.append('}')
193n/a return default_latex_document % dict(title=title, macros=macros, body=''.join(result))
194n/a
195n/a
196n/aif __name__ == '__main__':
197n/a import argparse
198n/a import os.path
199n/a import sys
200n/a import textwrap
201n/a import webbrowser
202n/a
203n/a parser = argparse.ArgumentParser(
204n/a description = 'Add syntax highlighting to Python source code',
205n/a formatter_class=argparse.RawDescriptionHelpFormatter,
206n/a epilog = textwrap.dedent('''
207n/a examples:
208n/a
209n/a # Show syntax highlighted code in the terminal window
210n/a $ ./highlight.py myfile.py
211n/a
212n/a # Colorize myfile.py and display in a browser
213n/a $ ./highlight.py -b myfile.py
214n/a
215n/a # Create an HTML section to embed in an existing webpage
216n/a ./highlight.py -s myfile.py
217n/a
218n/a # Create a complete HTML file
219n/a $ ./highlight.py -c myfile.py > myfile.html
220n/a
221n/a # Create a PDF using LaTeX
222n/a $ ./highlight.py -l myfile.py | pdflatex
223n/a
224n/a '''))
225n/a parser.add_argument('sourcefile', metavar = 'SOURCEFILE',
226n/a help = 'file containing Python sourcecode')
227n/a parser.add_argument('-b', '--browser', action = 'store_true',
228n/a help = 'launch a browser to show results')
229n/a parser.add_argument('-c', '--complete', action = 'store_true',
230n/a help = 'build a complete html webpage')
231n/a parser.add_argument('-l', '--latex', action = 'store_true',
232n/a help = 'build a LaTeX document')
233n/a parser.add_argument('-r', '--raw', action = 'store_true',
234n/a help = 'raw parse of categorized text')
235n/a parser.add_argument('-s', '--section', action = 'store_true',
236n/a help = 'show an HTML section rather than a complete webpage')
237n/a args = parser.parse_args()
238n/a
239n/a if args.section and (args.browser or args.complete):
240n/a parser.error('The -s/--section option is incompatible with '
241n/a 'the -b/--browser or -c/--complete options')
242n/a
243n/a sourcefile = args.sourcefile
244n/a with open(sourcefile) as f:
245n/a source = f.read()
246n/a classified_text = analyze_python(source)
247n/a
248n/a if args.raw:
249n/a encoded = raw_highlight(classified_text)
250n/a elif args.complete or args.browser:
251n/a encoded = build_html_page(classified_text, title=sourcefile)
252n/a elif args.section:
253n/a encoded = html_highlight(classified_text)
254n/a elif args.latex:
255n/a encoded = latex_highlight(classified_text, title=sourcefile)
256n/a else:
257n/a encoded = ansi_highlight(classified_text)
258n/a
259n/a if args.browser:
260n/a htmlfile = os.path.splitext(os.path.basename(sourcefile))[0] + '.html'
261n/a with open(htmlfile, 'w') as f:
262n/a f.write(encoded)
263n/a webbrowser.open('file://' + os.path.abspath(htmlfile))
264n/a else:
265n/a sys.stdout.write(encoded)