【问题标题】:Set the html fontsize in `pygmentize`, the command line tool of `pygments`在`pygments`的命令行工具`pygmentize`中设置html字体大小
【发布时间】:2020-10-13 19:22:14
【问题描述】:

我正在尝试使用pygments 通过命令行工具pygmentize 使用以下命令将python 文件转换为html 文件:

pygmentize -f html -O full -O linenos=1 out_file.html in_file.py

很遗憾,默认的fontsize太小了,不知道怎么调大。使用-O fontsize=16 不会报错,但也没有效果。

此外,我尝试了一个丑陋的补丁,将html *{font-size: 1.05em !important;} 插入到 html 文档的 css 部分,但这会影响行号布局。

我可以通过 python 终端调用“pygments”,但不知道如何将命令行实现到 python 代码中

【问题讨论】:

    标签: python html command-line pygments pygmentize


    【解决方案1】:

    好的,我使用以下 python 代码解决了这个问题:

    from pygments import highlight
    from pygments.lexers import get_lexer_by_name
    from pygments.formatters import get_formatter_by_name
    
    lexer = get_lexer_by_name('python')
    formatter = get_formatter_by_name('html', linenos='inline', full=1)
    
    with open("conver_me.py", 'r') as f_in:
        code = "".join(f_in.readlines())
    
    with open("output.html", 'w') as f_out:
        highlight(code, lexer, formatter, f_out)
    

    然后我添加了一行

    html *{font-size: 1.05em !important;}
    

    进入html文档的css部分并调整表格填充

    span.lineno { background-color: #f0f0f0; padding: 0px 5px 5px 5px; }
    

    【讨论】:

    • 真的没有办法直接“在pygmentizepygments的命令行工具中设置字体大小”吗?
    猜你喜欢
    • 2013-02-03
    • 1970-01-01
    • 2018-07-04
    • 2014-10-17
    • 1970-01-01
    • 2013-06-21
    • 2021-11-25
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多