Package: pygmentshtmltemplate
Pygments HTML Template --  pygments.formatter pluginYou can write a template for pygments HtmlFormatter with this package.Template Example<Pygmentshtmltemplate><!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(1)--><h1>${title}</h1><!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(2)--><olclass="${cssclass}"><!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(3)--><Line><!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(4)--><li_="${highlighted}"id="line-${lineno}"class="hll"><Tokens/></li><!-- - - - - - - - - - - -(5)--><liid="line-${lineno}"><Tokens/></li><!-- - - - - - - - - - - - - - - - - - - - - - - - - - -(6)--></Line><Tokens><!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(7)--><codeclass="${token_class} init"><Tokentype="Name.Function"fullmatch="__init__"/></code><!--(8)--><codeclass="${token_class}"title="${token_type}"><Token/></code><!-- - - - - - - - - - - - -(9)--></Tokens></ol><!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (10)--><p>${filename}</p><!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (11)--></Pygmentshtmltemplate>(1): Must have a root nodePygmentshtmltemplateThe.parsers.parsefunction uses parserxml.sax.make_parser()in the Python standard library. So the template must be able to read by the the SAX parser. You should not parse untrusted data.(2), (3), (10), (11): Wrapping html parts<Line>(4) and<Tokens>(7) block represent line formats and each token format in each code line.In (2),${title}representstitleoption which is set to the formatter class. And that is replaced with its value.(5), (6): Select a line wrapperEach of<Line>(4) children represents a line format. Html elements can have options value for testing. If all values non-nil, then that line will be rendered.highlightandlinenoare not the formatter class options, but implicitly generated fromlinenostartandhl_linesoption.False,None, and empty Sequence are failed, but0(zero) is success.Properties which name starts with_are only used for testing, but they are not rendered.The formatter tries the tests in textual order. The last line of the<Line>block should be always successful.(8), (9): Select a token wrapperHtml properties are the same as line’s.token_classandtoken_typeare not the formatter options.token_classis generated byformatter._get_css_classes(ttype: pygments.token._TokenType).token_typeis generated by'.'.join(ttype: pygments.token._TokenType)<Token>(8) element can havetype,match, andfullmatchproperties for testing.When Pygments gives the formatter a ‘tokentype’ and a ‘tokenvalue’, the formatter will test follows:pygments.token.is_token_subtype(tokentype, pytments.token.string_to_tokentype(${type}))re.fullmatch(${fullmatch}, tokenvalue)orre.match(${match}, tokenvalue)When all tests are failed, a ‘tokenvalue’ is rendered as it is.Install% pip install pygmentshtmltemplateYou can confirm pygments.formatters plugins:>>>importimportlib>>>importlib.metadata.entry_points().select(group='pygments.formatters')[EntryPoint(name='pygmentshtmltemplate',value='pygmentshtmltemplate:FormatterWithTemplate',group='pygments.formatters')]UsageFormatterWithTemplateis the subclass ofpygments.formatters.HtmlFormatter.FormatterWithTemplate.nameisFormatterWithTemplate, andFormatterWithTemplate.aliasesis['fmtr_tmpl']Formatter optionsThe optionsfilename,cssclass,linenostart,hl_lines,classprefix, andtitleare in common spec with theHtmlFormatter. Other options of theHtmlFormatterare suppressed by theFormatterWithTemplate. Almost other options may get alternatives by wrapping parts of a template.There is the original optiontemplatewhich is set to the path of a template file. If thetemplateisn’t set, the default template (the following code) is used.<Pygmentshtmltemplate><olclass="${cssclass}"><Line><li_="${highlighted}"class="hll"><Tokens/></li><li><Tokens/></li></Line><Tokens><codeclass="${token_class}"><Token/></code></Tokens></ol></Pygmentshtmltemplate>Command lineThe command line interface is provided by the Pygments. For example:$ pygmentize -f fmtr_tmpl -O template=template.xml -o output.html want_to_highlight.py

$ pygmentize -f fmtr_tmpl -S default -a .highlightIn reStructuredTextYou can use this formatter in the reStructuredText with docutils.First, register the reST directivepygmentshtmltemplate.docutis.PygHtmlTmplRstDirective. The target source code can be provided at that directive content block. Or using:file:option, you can provide the source file.fromdocutils.coreimportpublish_stringfromdocutils.writersimporthtml4css1fromdocutils.parsers.rstimportdirectivesfrompygmentshtmltemplate.docutilsimportPygHtmlTmplRstDirectivedirectives.register_directive('fmtr-tmpl',PygHtmlTmplRstDirective)ReST=f'''\PygHtmlTmplRstDirective test============================pygmentize a file.. fmtr-tmpl:: python :file:{__file__}pygmentize a content block.. fmtr-tmpl:: python:hl_lines: 1"""highlight this line"""def hello(foo):print(f'hello,{{foo}}!')'''if__name__=='__main__':formatted=publish_string(ReST,writer=html4css1.Writer())withopen(output,'wb')asout:out.write(formatted)Future consideredTemplate for thefulloptionNow, theformatter.get_style_defs()method corresponds to the only default template.