Last active
July 7, 2020 19:57
-
-
Save XCaminhante/10a8a25d3b9e8c2f1af0b286dd514a08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
from __future__ import print_function | |
from docutils import core | |
from docutils.writers.html5_polyglot import Writer,HTMLTranslator | |
import sys, os | |
import getopt | |
class File (): | |
def __init__ (self,filename): | |
self.filename = filename | |
def read (self): | |
f = open(self.filename) | |
content = f.read() | |
f.close() | |
return content | |
def write (self,new_content): | |
f = open(self.filename,"wb") | |
f.write(new_content) | |
f.close() | |
def filename_with_html_extension (self): | |
return os.path.splitext(os.path.basename(self.filename))[0]+".html" | |
class ReST_document (): | |
def __init__ (self, restinput, stylesheet=None): | |
stylesheet_content = '' | |
if stylesheet: | |
stylesheet_content = File(stylesheet).read() | |
class HTMLFragmentTranslator( HTMLTranslator ): | |
def __init__ (self, document): | |
HTMLTranslator.__init__ (self, document) | |
self.head_prefix = [] | |
self.body_prefix = ["</head><body class=\"prose\">"] | |
self.body_suffix = ['</body></html>'] | |
self.stylesheet = ['<style>\n',stylesheet_content,'</style>'] | |
def astext (self): | |
return ''.join(self.body) | |
self.html_fragment_writer = Writer() | |
self.html_fragment_writer.translator_class = HTMLFragmentTranslator | |
self.restinput = restinput | |
def to_xhtml5 (self): | |
return core.publish_string(self.restinput, writer = self.html_fragment_writer) | |
def usage (): | |
print("Usage:\n") | |
print("rst3_to_html.py doc1.rst [.. docN.rst] [-o target.html] [-c stylesheet.css]") | |
print("default operation is to convert every doc.rst file into a doc.html file") | |
print("-o concatenates all input files and creates a unique final file") | |
print("-c defines a common embed stylesheet for all target files") | |
def main (): | |
try: | |
opts, args = getopt.gnu_getopt(sys.argv[1:],"o:c:") | |
except getopt.GetoptError as err: | |
usage() | |
sys.exit(1) | |
if len(args)<1: | |
usage() | |
sys.exit(1) | |
main_output = None | |
stylesheet = None | |
for opt, val in opts: | |
if opt == '-o': | |
main_output = val | |
elif opt == '-c': | |
stylesheet = val | |
if main_output: | |
rstcontent = '' | |
for a in args: | |
rstcontent += File(a).read() | |
result = ReST_document(rstcontent,stylesheet).to_xhtml5() | |
File(main_output).write(result) | |
else: | |
for a in args: | |
rstcontent = File(a).read() | |
result = ReST_document(rstcontent,stylesheet).to_xhtml5() | |
File(File(a).filename_with_html_extension()).write(result) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment