trac 2 tex
Wednesday, June 3rd, 2009 at 2:18 pm, uncategorized
During our Thesis my team mate and I use a Trac setup as management tool. Needless to say we use LaTeX to write our documentation. Recently we wanted to import some Wiki pages into existing tex documents. But it turned out the two formats can not be easily converted.
I couldn’t find a conversion script so here is my own. The days when I was still writing Trac plugins in python are long gone (3 years) so the script became unnecessarily verbose, has no error handling and a bit awkward but it works so far.
Usage: $> python trac2tex.py content.txt
#!/usr/bin/python # -*- coding:iso-8859-15 -*- # # Converts trac content into tex. # Supports headings and enumerations # import re,sys curInd = 0 def sectionize(name, ind): print '\\' + {1: 'section', 2: 'subsection', 3: 'subsubsection', 4: 'paragraph' }[ind] + '{' + name + '}' def itemize(item, ind): global curInd if ind < curInd: enditemize() elif ind > curInd: out(ind, '\\begin{itemize}') curInd = ind out(ind,'\\item ' + item) def enditemize(): global curInd if not curInd: return curInd -= 1 out(curInd,'\\end{itemize}') def texify(text): for i,j in {'ü':'\\"{u}', 'ä':'\\"{a}', 'ö':'\\"{o}', '!':'', '&':'\\&', '_':'\\_'}.iteritems(): text = text.replace(i,j) return text def out(ind,text): print '\t' * curInd + text f = open( sys.argv[1],'r') for line in f: line = texify(line) #trac * items item = re.search( '(\s+)(\*\s?)(.+)', line) if item: itemize(item.group(3), len( item.group(1))) continue else: enditemize() #trac titles title = re.search( '(=+)\s([^=]+)\s', line ) if title: sectionize(title.group(2),len(title.group(1))) continue print line while(curInd): enditemize()
