2010-09-29 85 views
14

我使用rst編寫博客,並手動將其轉換爲html併發布。用於將rst轉換爲html的單個py文件

現在,我想通過使用GAE

我知道docutils的可以首先轉換爲HTML,

但docutils的大創建一個簡單的博客系統,是有一個單一的PY文件將rst轉換爲html?

任何人通知GAE

回答

6

看一看爲hacking docutils的說明。您不需要整個docutils從第一個生成html,但您確實需要閱讀器,解析器,轉換器和編寫器。通過一些努力,您可以將所有這些文件合併到現有docutils文件中的單個文件中。

1

如果Pyfunc的答案不符合您的需求,您可以考慮使用Markdown語言。語法與rst類似,markdown.py非常小且易於使用。它仍然不是一個單獨的文件,但是您可以將它作爲模塊導入到您可能擁有的任何現有腳本中。

http://www.freewisdom.org/projects/python-markdown/

14

Sphinx文檔生成器Python庫包含許多重構文本(RST)命令行轉換器。

安裝獅身人面像:

$ pip install sphinx 

然後使用許多RST2的一個*的.py助手:

$ rst2html.py in_file.rst out_file.html 
+10

我不認爲你需要完整的獅身人面像這一點。 rst2html.py是docutils的一部分,它是sphinx的依賴。 – 2014-06-13 03:27:42

0

那麼你可以用下面的代碼試試吧,用法是:

compile_rst.py yourtext.rst

or

compile_rst.py yourtext.rst desiredname.html

# compile_rst.py 

from __future__ import print_function 
from docutils import core 
from docutils.writers.html4css1 import Writer,HTMLTranslator 
import sys, os 

class HTMLFragmentTranslator(HTMLTranslator): 
    def __init__(self, document): 
     HTMLTranslator.__init__(self, document) 
     self.head_prefix = ['','','','',''] 
     self.body_prefix = [] 
     self.body_suffix = [] 
     self.stylesheet = [] 
    def astext(self): 
     return ''.join(self.body) 

html_fragment_writer = Writer() 
html_fragment_writer.translator_class = HTMLFragmentTranslator 

def reST_to_html(s): 
    return core.publish_string(s, writer = html_fragment_writer) 

if __name__ == '__main__': 
    if len(sys.argv)>1: 
     if sys.argv[1] != "": 
      rstfile = open(sys.argv[1]) 
      text = rstfile.read() 
      rstfile.close() 
      if len(sys.argv)>2: 
       if sys.argv[2] != "": 
        htmlfile = sys.argv[2] 
      else: 
       htmlfile = os.path.splitext(os.path.basename(sys.argv[1]))[0]+".html" 
      result = reST_to_html(text) 
      print(result) 
      output = open(htmlfile, "wb") 
      output.write(result) 
      output.close() 
    else: 
     print("Usage:\ncompile_rst.py docname.rst\nwhich results in => docname.html\ncompile_rst.py docname.rst desiredname.html\nwhich results in => desiredname.html")