如何自定义 pygments 的输出? [英] How can I customize the output from pygments?

查看:40
本文介绍了如何自定义 pygments 的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我通过 pygments 运行 python 源文件,它会输出 html 代码,其元素类属于 pygments 正在使用的某个 CSS 文件.是否可以将样式属性包含在输出的 html 中,以便我不必提供 CSS 文件?

解决方案

通过将 noclasses 属性设置为 True,只会生成内联样式.这是一个可以很好地完成工作的片段:

<预><代码>格式化程序 = HtmlFormatter(style=MyStyle)formatter.noclasses = True打印突出显示(内容,PythonLexer(),格式化程序)

If I run a python source file through pygments, it outputs html code whose elements class belong to some CSS file pygments is using. Could the style attributes be included in the outputted html so that I don't have to provide a CSS file?

解决方案

By setting the noclasses attribute to True, only inline styles will be generated. Here's a snippet that does the job just fine:


formatter = HtmlFormatter(style=MyStyle)
formatter.noclasses = True
print highlight(content,PythonLexer(),formatter)

这篇关于如何自定义 pygments 的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆