2012-03-19 65 views
0

如何使用實際值自定義.rst文件中的佔位符?docutils/reStructuredText模板功能

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

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

我想通過運行下面的命令與價值this is the value of custom property更換${custom_text}屬性:

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構建管理工具使用)自定義模板。

回答

8

使用replace指令執行reStructuredText文件中的替換。例如:

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輸出。

+0

您對reStructuredText的模板功能給出了很好的答案。我需要承認,即使我認爲應該有某種方式,我也不知道reStructuredText的模板功能。但是,恐怕這不會解決我的問題 - 我想從命令行或使用傳統的'.properties'文件自定義模板(可以由Ant/Maven構建管理工具使用) – altern 2012-03-20 16:56:57

+1

您將不得不自己寫這些東西。由於rst2html.py沒有'-p'或'-D'選項,您將需要某種包裝腳本。通過運行reStructuredText文件(例如C語言預處理器)可以實現'-D'行爲。當我有更多時間時,我會考慮屬性文件。但是,您希望使用reStructuredText的方式不適合使用,這可能比它的價值更麻煩。 – Chris 2012-03-20 18:14:04

+0

好吧,我明白了。看起來你是對的 - 我應該使用另一種格式。 – altern 2012-03-20 18:35:53