aboutsummaryrefslogtreecommitdiff
path: root/doc/api/python/create_python_examples.py
blob: 6dd99a0f1d16716ab772c0913716620bbfb6349f (plain)
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
#!/usr/bin/env python
# vim: set fileencoding=utf-8 sw=4 sts=4 et :

import sys, os, re
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

srcdir = sys.argv[1]

lexer = PythonLexer()
formatter = HtmlFormatter(linenos=True, cssclass="syntax")

topuri = '../../'
toplinks = file(os.path.join(srcdir, '../toplinks.html.part.in')).read()
header = file(os.path.join(srcdir, '../../header.html.part.in')).read()
footer = file(os.path.join(srcdir, '../../footer.html.part.in')).read()
css = '<link rel="stylesheet" href="python_syntax.css" type="text/css" />'

header = header.replace('###TOPLINKS###', toplinks)
header = header.replace('###TOPURI###', topuri)
header = header.replace('</head>', css + '</head>')

for example in sys.argv[2:]:
    input = file(os.path.join(srcdir, example)).read()

    example_html = "python/" + example.replace(".py", ".html")

    out_file = file(example_html, "w")
    out_file.write(header)
    out_file.write("<h1>%s</h1>" % example)
    out_file.write("<p>%s</h1>" % re.search('"""(.*?)"""', input).groups()[0])
    out_file.close()

    highlight(input, lexer, formatter, file(example_html, "a"))

    file(example_html, "a").write(footer)