DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Latex2wiki

10.17.2006
| 4556 views |
  • submit to reddit
        Translate a subset of LaTeX into MoinMoin wiki syntax.


#!/usr/bin/env python

#    Copyright (C) 2003, Maxime Biais <maxime@biais.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# $Id: latex2wiki.py,v 1.1.1.1 2004/03/14 18:31:50 max Exp $

import sys, re

def dummy(d):
    pass

NONE = "__@NONE@__"

tr_list = [
    (r"\\includegraphics.*{(.*)\.eps}", "attachment::%s.png", dummy),
    (r"\\caption{.*}", "", dummy),
    (r"\\label{.*}", "", dummy),
    (r"(.*)\\emph{(.*)}(.*)", """%s'''%s'''%s""", dummy),
    (r"\\item (.*)", " * %s", dummy),
    (r"\\begin{.*}", "", dummy),
    (r"\\end{.*}", "", dummy),
    (r"(.*)``(.*)''(.*)", "%s\"%s\"%s", dummy),
    (r"\\chapter{(.*)}", NONE, dummy),
    (r"\\paragraph{(.*)}", "==== %s ====", dummy),
    (r"\\subsubsection{(.*)}", "==== %s ====", dummy),
    (r"\\subsection{(.*)}", "=== %s ===", dummy),
    (r"\\section{(.*)}", "== %s ==", dummy),
    (r"(.*)\\fig{.*}(.*)", "%s suivant %s", dummy)
    ]

in_stream  = open(sys.argv[1], "r")
if len(sys.argv) < 3:
    out_stream = sys.stdout
else:
    out_stream = open(sys.argv[2], "w")


for i in in_stream.readlines():
    cur_write = 0
    for reg in tr_list:
        m = re.search(reg[0], i)
        if m:
            reg[2](i)
            cur_write = 1
            if reg[1] == NONE:
                break
            print >> out_stream, reg[1] % m.groups()
            break
    if not cur_write:
        out_stream.write(i)