Jump to content

Syntax highlighting using Mediawiki formatting: Difference between revisions

Add 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.
 
''&quot;&quot;&quot;Syntax highlighting using Mediawiki formatting.&quot;&quot;&quot;''
'''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):
''&quot;&quot;&quot;Format source code using MediaWiki markup.&quot;&quot;&quot;''
'''def''' __init__(self, **options):
super().__init__(**options)
self.styles = {
Token: (&quot;&quot;, &quot;&quot;),
Token.Comment: (&quot;&#x27;&#x27;&quot;, &quot;&#x27;&#x27;&quot;),
Token.Keyword: (&quot;&#x27;&#x27;&#x27;&quot;, &quot;&#x27;&#x27;&#x27;&quot;),
Token.String.Doc: (&quot;&#x27;&#x27;&quot;, &quot;&#x27;&#x27;&quot;),
}
'''def''' format(self, token_source, outfile):
last_val = &quot;&quot;
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):
''&quot;&quot;&quot;Convert the characters &amp;, &lt;, &gt; and &#x27; to HTML-safe sequences.&quot;&quot;&quot;''
'''def''' __init__(self, **options):
super().__init__(**options)
'''def''' filter(self, _, stream):
'''for''' ttype, value in stream:
'''yield''' ttype, escape(value)
'''def''' main(language_name=&quot;python&quot;, infile='''None'''):
formatter = MediaWikiFormatter(style=&quot;bw&quot;)
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),
&quot; &quot;,
'''lambda''' line: '''True''',
),
end=&quot;&quot;,
)
'''if''' __name__ == &quot;__main__&quot;:
main()
140

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.