forked from acg/python-flattery
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hilite_markdown.py
executable file
·77 lines (58 loc) · 1.72 KB
/
hilite_markdown.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
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
"""
Convert markdown to html.
Use pygments to syntax highlight any code blocks that begin with a shebang.
Wrap html and style it somewhat like docs.python.org.
"""
import sys
import re
from markdown import Markdown
from markdown import TextPreprocessor
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name, TextLexer
INLINE_STYLES = False
INCLUDE_SHEBANG = False
class CodeBlockPreprocessor(TextPreprocessor):
pattern = re.compile( r'^(?P<codeblock> {4}#!(?P<shebang>.*)\n(?P<body>(( {4}.*)?\n)*))', re.M)
formatter = HtmlFormatter(noclasses=INLINE_STYLES)
def run(self, lines):
def repl(m):
# extract program from shebang
argv = m.group('shebang').split()
program = argv[0].split('/')[-1]
if program == 'env' and len(argv) >= 2:
program = argv[1]
try:
lexer = get_lexer_by_name(program)
except ValueError:
lexer = TextLexer()
if INCLUDE_SHEBANG:
code = m.group('codeblock')
else:
code = m.group('body')
code = highlight(code, lexer, self.formatter)
code = code.replace('\n\n', '\n \n').replace('\n', '<br />')
return '\n\n<div class="code">%s</div>\n\n' % code
return self.pattern.sub(repl, lines)
if len(sys.argv) > 1:
infile = file(sys.argv[1])
else:
infile = sys.stdin
md = Markdown()
md.textPreprocessors.insert(0, CodeBlockPreprocessor())
html = md.convert(infile.read())
template = """
<html>
<head>
<link rel="stylesheet" href="default.css" type="text/css" />
<link rel="stylesheet" href="pygments.css" type="text/css" />
</head>
<body>
<div class="body">
%s
</div>
</body>
</html>
"""
print template % html