代码之家  ›  专栏  ›  技术社区  ›  Adam Matan

斯芬克斯:为一个单词设置颜色

  •  18
  • Adam Matan  · 技术社区  · 14 年前

    有没有办法设置单字(或字符)的颜色 sphinx font 标签。

    3 回复  |  直到 13 年前
        1
  •  12
  •   ahcox ughoavgfhw    13 年前

    如果您不想与html绑定,请尝试对您的单词应用与普通正文不同的样式。

    在这个改编自rst2pdf手册的示例中,我应用了现有的rubric样式,在我使用的后端中是红色的:

    Before red.
    
    .. role:: rubric
    
    I like color :rubric:`rubric`.
    
    After red.
    

    单词的实际外观将取决于在生成文档时所使用的样式表中如何定义您选择的样式。 如果需要蓝色文本,请创建蓝色文本样式并从普通文本样式派生。 打印rst2的默认值pdf.py格式,执行此操作(来自rst2pdf手册):

    rst2pdf --print-stylesheet
    

    继续rst2pdf样式表的示例,将其添加到样式表中以获得蓝色文本样式:

    bluetext:
      parent: bodytext
      textColor: blue
    

    在文档中,您可以引用此样式以获得蓝色单词。 注意,这个位是通用的,如果您在html或任何后端的样式表中定义了蓝色样式,则应该生成蓝色文本。

    Before blue.
    
    .. role:: bluetext
    
    I like color :bluetext:`blue`.
    
    After blue.
    

    enter image description here

        2
  •  12
  •   Næreen    7 年前

    website ,我使用以下组合:

    • 一个包含角色定义的重构文本文件,每种颜色一个-请参阅 .special.rst (比特桶链接)
    • 包含每个角色的颜色规则的CSS文件-请参见 hacks.css (比特桶链接)

    然后,在每个需要颜色的rST文件中,我首先导入 .special.rst 在顶部,手动:

    .. include:: .special.rst
    

    rst_epilog conf.py 文件:

    rst_epilog = "\n.. include:: .special.rst\n"
    

    然后每个角色都可以在纯rST语法中轻松使用:

    This is :red:`red !` And :blue:`this part is blue`.
    

    this page

    它可以很好地用于html输出(和类似html的),但不能用于PDF。参考 first answer above 用于生成带有颜色的PDF。

        3
  •  6
  •   Adam Matan    14 年前

    这是可行的,但是将HTML放在一个单独的段落中。

    .. raw:: html
    
        <font color="blue">Blue word,</font>
    
    And a word without color
    

    如果有人有更好的答案,我会接受的。

        4
  •  4
  •   Karl N. Redman    6 年前

    html .

    参考文献: reStructuredText Directives

    .. role:: raw-html(raw)
       :format: html
    
    :raw-html:`<font color="blue">Blue word,</font>` And a word without color
    
        5
  •  4
  •   ankostis    4 年前

    狮身人面像 已经支持颜色 s5defs.txt standard definition file inclusion (但缺少CSS文件):

    1. 将此文本创建/附加到 rst_epilog 狮身人面像配置,在您的 docs/conf.py 文件:

       rst_prolog = """
       .. include:: <s5defs.txt>
      
       """
      
    2. Sphinx's instructions 添加带有颜色的css (例如,采用 hack.css @Næreen's answer

    • _static/css/s4defs-roles.css ;

    • 将其路径附加到 shtml_css_files 狮身人面像配置:

          html_css_files = ['css/s4defs-roles.css']
      

    然后您可以使用:

    Some :red:`colored text` at last!
    

    提示: 阅读 this SO 如果您还希望样式出现在 输出。