Syntax highlighting using Mediawiki formatting: Difference between revisions
Content added Content deleted
m (replaced task sub-headings) |
m (→{{header|Python}}: Handle indentation in the formatter and set formatter name and aliases .) |
||
Line 458:
'''from''' html '''import''' escape
'''from''' textwrap '''import''' indent
'''from''' io '''import''' StringIO
'''from''' pygments '''import''' highlight
Line 468 ⟶ 469:
'''class''' MediaWikiFormatter(Formatter):
''"""Format source code using MediaWiki markup."""''
aliases = ["mediawiki", "wiki"]
filenames = []
'''def''' __init__(self, **options):
super().__init__(**options)
self.indent = options.get("indent", " ")
self.styles = {
Line 480 ⟶ 486:
'''def''' format(self, token_source, outfile):
last_val = ""
last_type = '''None'''
Line 494 ⟶ 501:
'''if''' last_val:
style_begin, style_end = self.styles[last_type]
last_val = value
Line 502 ⟶ 509:
'''if''' last_val:
style_begin, style_end = self.styles[last_type]
''# Write indented lines to the output file.''
outfile.write(
buffer.getvalue(),
)▼
)
Line 517 ⟶ 533:
'''def''' main(language_name="python", infile='''None'''):
formatter = MediaWikiFormatter(
lexer = get_lexer_by_name(language_name)
lexer.add_filter(HTMLEscapeFilter())
'''with''' open(infile or __file__) '''as''' fd:
print(highlight(fd.read(), lexer, formatter), end="")
▲ indent(
▲ " ",
▲ '''lambda''' line: '''True''',
▲ ),
▲ end="",
▲ )
|