moved the DocutilsInterface class to pygments_code_block_directive.py
[pylit.git] / rstdocs / features / pygments_docutils_interface.py
blob756a4c128975993559b5fe62c562f664be4ee824
1 # Test the parsing and formatting by pygments:
4 from docutils import nodes, utils, core
5 import pygments.lexers
6 from pygments_code_block_directive import DocutilsInterface
8 source_string = """\
9 def my_function():
10 "just a test"
11 print 8/2
12 """
14 lexer = pygments.lexers.get_lexer_by_name('python')
15 tokens = list(pygments.lex(source_string, lexer))
16 document = utils.new_document('generated')
17 literal_block = nodes.literal_block(raw_code=source_string.splitlines(True),
18 classes=["code-block", "python"])
19 document += literal_block
21 # You could add e.g. 'p' (Token.Punctuation) to unstyled_tokens.
22 unstyled_tokens = ['', ]
23 for cls, value in DocutilsInterface(tokens):
24 if cls in unstyled_tokens:
25 # insert as Text to decrease the verbosity of the output.
26 node = nodes.Text(value, value)
27 else:
28 node = nodes.inline(value, value, classes=[cls])
29 literal_block += node
31 writer_names = ('html', 'pseudoxml', 'xml', 'latex', 'newlatex2e', 's5')
32 for name in writer_names[2:3]:
33 print "\nusing writer %r\n" % name
34 print core.publish_from_doctree(document, writer_name=name)