parent
6e845d2506
commit
c581a6d76b
@ -0,0 +1,79 @@ |
|||||||
|
#!/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") |
||||||
|
|
||||||
@ -0,0 +1,23 @@ |
|||||||
|
def makeExtension(configs=None) : |
||||||
|
return MathExtension() |
||||||
|
|
||||||
|
from markdown.inlinepatterns import Pattern |
||||||
|
import markdown, urllib |
||||||
|
|
||||||
|
class MathPattern(Pattern): |
||||||
|
def __init__(self,md): |
||||||
|
Pattern.__init__(self,"\$\$(.*?)\$\$",md) |
||||||
|
def handleMatch(self, m): |
||||||
|
|
||||||
|
texcode=m.group(2) |
||||||
|
el = markdown.util.etree.Element('span') |
||||||
|
el.text="$"+self.unescape(texcode)+"$" |
||||||
|
|
||||||
|
#el.attrib["alt"]=self.unescape(texcode) |
||||||
|
#el.attrib["class"]='tex' |
||||||
|
return el |
||||||
|
|
||||||
|
class MathExtension(markdown.Extension): |
||||||
|
def extendMarkdown(self, md, md_globals): |
||||||
|
md.inlinePatterns.add('math', MathPattern(md), '<image_link') |
||||||
|
|
||||||
Loading…
Reference in new issue