Syntax highlighting using Mediawiki formatting: Difference between revisions

m
→‎{{header|Python}}: Handle indentation in the formatter and set formatter name and aliases .
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."""''
name = " MediaWiki",
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):
buffer = indentStringIO()
last_val = ""
last_type = '''None'''
Line 494 ⟶ 501:
'''if''' last_val:
style_begin, style_end = self.styles[last_type]
outfilebuffer.write(style_begin + last_val + style_end)
last_val = value
Line 502 ⟶ 509:
'''if''' last_val:
style_begin, style_end = self.styles[last_type]
outfilebuffer.write(style_begin + last_val + style_end)
''# Write indented lines to the output file.''
outfile.write(
),indent(
buffer.getvalue(),
end="" self.indent,
'''lambda''' line_: '''True''',
)
)
Line 517 ⟶ 533:
'''def''' main(language_name="python", infile='''None'''):
formatter = MediaWikiFormatter(style="bw")
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(
highlight(fd.read(), lexer, formatter),
" ",
'''lambda''' line: '''True''',
),
end="",
)
147

edits