Syntax highlighting using Mediawiki formatting: Difference between revisions
Syntax highlighting using Mediawiki formatting (view source)
Revision as of 19:13, 7 October 2023
, 7 months agoAdd Python
(→Introduction: Not sure <nowiki> tags wouldwork for <, >, etc. so removed mention oif them) |
(Add Python) |
||
Line 370:
} ''# nextChar''
=={{header|Python}}==
{{libheader|pygments}}
This solution builds on lexers available in Pygments by defining a formatter outputting simple MediaWiki markup, and a filter to translate characters to HTML escape sequences. Note that I've taken liberties with said escaping.
''"""Syntax highlighting using Mediawiki formatting."""''
'''from''' html '''import''' escape
'''from''' textwrap '''import''' indent
'''from''' pygments '''import''' highlight
'''from''' pygments.filter '''import''' Filter
'''from''' pygments.formatter '''import''' Formatter
'''from''' pygments.lexers '''import''' get_lexer_by_name
'''from''' pygments.token '''import''' Token
'''class''' MediaWikiFormatter(Formatter):
''"""Format source code using MediaWiki markup."""''
'''def''' __init__(self, **options):
super().__init__(**options)
self.styles = {
Token: ("", ""),
Token.Comment: ("''", "''"),
Token.Keyword: ("'''", "'''"),
Token.String.Doc: ("''", "''"),
}
'''def''' format(self, token_source, outfile):
last_val = ""
last_type = '''None'''
'''for''' token_type, value in token_source:
''# Work up the token hierarchy until a style is found.''
'''while''' token_type not in self.styles:
token_type = token_type.parent
''# Group consecutive tokens of the same type.''
'''if''' token_type == last_type:
last_val += value
'''else''':
'''if''' last_val:
style_begin, style_end = self.styles[last_type]
outfile.write(style_begin + last_val + style_end)
last_val = value
last_type = token_type
''# Flush remaining values.''
'''if''' last_val:
style_begin, style_end = self.styles[last_type]
outfile.write(style_begin + last_val + style_end)
'''class''' HTMLEscapeFilter(Filter):
''"""Convert the characters &, <, > and ' to HTML-safe sequences."""''
'''def''' __init__(self, **options):
super().__init__(**options)
'''def''' filter(self, _, stream):
'''for''' ttype, value in stream:
'''yield''' ttype, escape(value)
'''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(
indent(
highlight(fd.read(), lexer, formatter),
" ",
'''lambda''' line: '''True''',
),
end="",
)
'''if''' __name__ == "__main__":
main()
|