You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.9 KiB
79 lines
1.9 KiB
#!/usr/bin/python
|
|
import markdown,sys
|
|
from HTMLParser import HTMLParser
|
|
|
|
class MDHTML2TexParser(HTMLParser):
|
|
txt=''
|
|
oul=False
|
|
def handle_starttag(self, tag, attrs):
|
|
if tag=='h2':
|
|
self.txt+='\\section{'
|
|
if tag=='h3':
|
|
self.txt+='\\subsection{'
|
|
if tag=='h4':
|
|
self.txt+='\\subsubsection{'
|
|
if tag=='strong':
|
|
self.txt+='\\textbf{'
|
|
if tag=='em':
|
|
self.txt+='\\textit{'
|
|
|
|
if tag=='ul':
|
|
self.txt+='\\begin{itemize}\n'
|
|
self.oul=True
|
|
if tag=='ol':
|
|
self.txt+='\\begin{enumerate}\n'
|
|
self.oul=True
|
|
if tag=='li':
|
|
self.txt+='\\item '
|
|
if tag=='p':
|
|
self.txt+='\n\n'
|
|
|
|
def handle_endtag(self, tag):
|
|
if tag=='h2' or\
|
|
tag=='h3' or\
|
|
tag=='h4':
|
|
self.txt+='}\n'
|
|
if tag=='strong' or\
|
|
tag=='em':
|
|
self.txt+='}'
|
|
if tag=='ul':
|
|
self.txt+='\\end{itemize}\n'
|
|
if tag=='ol':
|
|
self.txt+='\\end{enumerate}\n'
|
|
|
|
def handle_data(self, data):
|
|
self.txt+=data
|
|
|
|
def handle_entityref(self,ref):
|
|
self.handle_charref(ref)
|
|
def handle_charref(self,ref):
|
|
if self.body:
|
|
self.txt += '&'+ref+';'
|
|
|
|
if not(len(sys.argv)==3 or len(sys.argv)==2):
|
|
print "Usage: %s infile [outfile] " % sys.argv[0]
|
|
sys.exit(1)
|
|
html=markdown.markdown(open(sys.argv[1]).read(),["math"])
|
|
parser = MDHTML2TexParser()
|
|
parser.feed(html)
|
|
|
|
if len(sys.argv)==2:
|
|
f=sys.stdout
|
|
else:
|
|
f=open(sys.argv[2],'w')
|
|
|
|
f.write("""
|
|
\\documentclass{scrartcl}
|
|
\\usepackage[latin1]{inputenc}
|
|
\\usepackage[T1]{fontenc}
|
|
\\usepackage[ngerman]{babel}
|
|
\\usepackage{amsmath}
|
|
|
|
\\title{Ein Testdokument}
|
|
\\author{Otto Normalverbraucher}
|
|
\\date{05. Januar 2004}
|
|
\\begin{document}
|
|
""")
|
|
f.write(parser.txt)
|
|
f.write("\\end{document}\n")
|
|
|
|
|