【问题标题】:docutils/reStructuredText template featuresdocutils/reStructuredText 模板功能
【发布时间】:2012-04-04 01:26:11
【问题描述】:

如何使用实际值自定义 .rst 文件中的占位符?

例如,我有 example.rst 文件,内容如下:

Header
------------------------------------ 
${custom_text}

我想通过运行以下命令将 ${custom_text} 属性替换为值 this is the value of custom property

rst2html example.rst -o example.html -Dcustom_text="this is the value of custom property"

我也想知道是否可以使用.properties 文件自定义模板?例如,我想使用example.properties 文件运行rst2html example.rst -o example.html -p example.properties 命令,其内容如下:

custom_text=this is the value of custom property

有可能吗? reStructuredText 是否完全支持模板功能?

编辑:请注意,我想从命令行自定义模板或使用常规的.properties 文件(可由 Ant/Maven 构建管理工具使用)。

【问题讨论】:

    标签: documentation markup restructuredtext docutils


    【解决方案1】:

    有几个处理模板的选项:

    a) 使用内部替换机制:

    模板.txt::

     Header
     ======
    
     |custom text|
    

    example.txt::

     .. |custom text| replace:: this is the value of custom property
    

    转换为rst2html example.txt -o example.html

    这样您就可以处理内联文本和图像。

    b) 编写您自己的包装器:一个 Python 脚本,它读取 templateproperties 文件,进行替换,并使用 Docutils.core 中的一个函数进行转换。

    【讨论】:

      【解决方案2】:

      reStructuredText 文件中的替换是使用replace 指令执行的。例如:

      I am using |RST|.
      
      .. |RST| replace:: reStructuredText
      

      会产生文字

      我正在使用 reStructuredText。

      您可以使用include 指令在单独的文件中定义替换模板列表。例如,给定以下两个文件:

      example.rst

      Header
      ======
      
      .. Include templates from external file (this is a comment).
      .. include:: properties.rst
      
      I can include text, like |my custom text|, from other files.
      

      properties.rst

      .. |my custom text| replace:: "example text"
      

      将生成文档:

      标题

      我可以包含来自其他文件的文本,例如“示例文本”。

      这里我使用了命令rst2html.py example.rst 来生成HTML 输出。

      【讨论】:

      • 您对 reStructuredText 的模板功能给出了很好的回答。我需要承认我不知道 reStructuredText 的模板功能,即使我认为应该有某种方式。但是,恐怕这并不能解决我的问题 - 我想从命令行自定义模板或使用常规的 .properties 文件(可以由 Ant/Maven 构建管理工具使用)
      • 你将不得不自己写这些东西。由于 rst2html.py 没有 -p-D 选项,因此您需要某种包装脚本。 -D 行为可以通过例如 C 语言预处理器运行 reStructuredText 文件来实现。当我有更多时间时,我会考虑一下属性文件。但是,您希望以非设计为使用的方式使用 reStructuredText,这可能比它的价值更麻烦。
      • 好的,我明白了。看来你是对的 - 我应该使用另一种格式。
      • 有没有办法使用 replace 指令进行 multiline 替换?我不能让它这样做。
      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2020-06-26
      • 2011-10-24
      • 1970-01-01
      • 2010-09-13
      • 2014-05-03
      • 2014-05-18
      相关资源
      最近更新 更多