forked from rubengerritsen/LatexToCanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlightCode.py
executable file
·67 lines (55 loc) · 2.14 KB
/
highlightCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
"""
Created on Wed Sep 2 13:22:12 2020
@author: ruben
"""
from __future__ import print_function
import sys
import pandocfilters as pf
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
def eprint(*args, **kwargs):
""" Equivalent to print(), but prints to stderr.
Needed (for debuggin)since we can't print to stdout, since
that disrupts the pipeline.
"""
print(*args, file=sys.stderr, **kwargs)
def get_default_style():
""" CSS specification of the code block style.
might interfere with the formatting done by Canvas, so
keep it basic.
"""
return ''
def highlightCode(code, language='python', style='default'):
""" Highlights plain text string as code.
@params:
code: a string containing the basic code to format
language: programming language to base highlighting on
style: choose color scheme options: ['default', 'emacs', 'friendly',
'colorful', 'autumn', 'murphy', 'manni', 'monokai', 'perldoc',
'pastie', 'borland', 'trac', 'native', 'fruity', 'bw', 'vim',
'vs', 'tango', 'rrt', 'xcode', 'igor', 'paraiso-light',
'paraiso-dark', 'lovelace', 'algol', 'algol_nu', 'arduino',
'rainbow_dash', 'abap']
"""
lexer = language
style = style
formatter = HtmlFormatter(style=style,
linenos=False,
noclasses=True,
cssclass='',
cssstyles= get_default_style(),
nobackground=True
)
html = highlight(code, get_lexer_by_name(lexer), formatter)
return html
def formatCode(key, value, format, meta):
style = 'default'
if key == 'CodeBlock':
if value[0][1]:
return pf.Plain([pf.RawInline("html", highlightCode(value[1],style=style, language=value[0][1][0]))])
else:
return pf.Plain([pf.RawInline("html", highlightCode(value[1],style=style, language='text'))])
if __name__ == "__main__":
pf.toJSONFilter(formatCode)