1 # Test the parsing and formatting by pygments:
4 from docutils
import nodes
, utils
, core
6 from pygments_code_block_directive
import DocutilsInterface
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
)
28 node
= nodes
.inline(value
, value
, classes
=[cls
])
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
)