Attachment 'latex2wiki.py'
Download 1 """Translate a subset of LaTeX into MoinMoin wiki syntax.
2
3 This program was originally written by Maxime Biais <maxime@biais.org>
4 and then modified by Allen Downey <downey@allendowney.com>. The
5 original is available at http://wiki.loria.fr/wiki/Latex2wiki
6
7 The primary limitation of this program is that it only recognizes
8 Latex patterns if they appear on a single line, so {\tt this pattern}
9 would get translated, but {\tt this
10 pattern} would not.
11
12 Other limitations include:
13
14 1) It doesn't distinguish between itemize, enumerate and
15 description; everything becomes itemize.
16
17 2) It only recognizes the subset of Latex I use.
18
19 3) It's not particularly efficient, but for the files I have
20 translated, it doesn't matter.
21
22 """
23
24 # Copyright (C) 2003, Maxime Biais <maxime@biais.org>
25 # Modified version Copyright 2005 Allen B. Downey
26 # Modified version for my latex text 2008 Reimar Bauer
27 #
28 # This program is free software; you can redistribute it and/or modify
29 # it under the terms of the GNU General Public License as published by
30 # the Free Software Foundation; either version 2 of the License, or
31 # (at your option) any later version.
32 #
33 # This program is distributed in the hope that it will be useful,
34 # but WITHOUT ANY WARRANTY; without even the implied warranty of
35 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 # GNU General Public License for more details.
37 #
38 # You should have received a copy of the GNU General Public License
39 # along with this program; if not, write to the Free Software
40 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 #
42
43 import sys, re
44
45 # rewrites is a list of rewrite rules.
46 # The first entry in each line is a regular expression.
47 # The second line is the format string
48 # used to rewrite the line if the regexp matches.
49
50 rewrites = [
51 # comments
52 (r"%.*", ""),
53 # simple Latex commands we ignore
54 (r"\\[^ {]*$", ""),
55 # other Latex commands we ignore
56 (r"\\label{.*}", ""),
57 (r"\\index{.*}", ""),
58 (r"\\documentclass{.*}", ""),
59 (r"\\usepackage{.*}", ""),
60 (r"\\newcommand{.*}", ""),
61 (r"\\setlength{.*}", ""),
62 # some characters that have to be quoted in Latex
63 # that don't have to be quoted in wiki
64 (r"(.*)\\#(.*)", "%s#%s"),
65 (r"(.*)\\\$(.*)", "%s$%s"),
66 (r"(.*)\\&(.*)", "%s&%s"),
67 (r"(.*)\\_(.*)", "%s_%s"),
68 # text formats
69 (r"(.*)\\emph{([^}]*)}(.*)", "%s'''%s'''%s"),
70 (r"(.*){\\sf ([^}]*)}(.*)", "%s`%s`%s"),
71 (r"(.*){\\bf ([^}]*)}(.*)", "%s'''%s'''%s"),
72 (r"(.*){\\em ([^}]*)}(.*)", "%s''%s''%s"),
73 # urls (a url at the beginning of a line gets a special rule
74 (r"^\\url{([^}]*)}(.*)", "%s %s"),
75 (r"(.*)\\url{([^}]*)}(.*)", "%s %s %s"),
76 (r"(.*){\\tt ([^}]*)}(.*)", "%s{{{%s}}}%s"),
77 (r"(.*)\\texttt{([^}]*)}(.*)", "%s{{{%s}}}%s"),
78 (r"(.*)\\textit{([^}]*)}(.*)", "%s''%s''%s"),
79 (r"(.*)\\textbf{([^}]*)}(.*)", "%s'''%s'''%s"),
80
81 (r"(.*)\\colorbox{silver}{([^}]*)}(.*)", "%s{{{%s}}}%s"),
82 (r"(.*)\\colorbox{lightgray}{\\parbox{14.4cm}{(.*)", "%s{{{{{#!wiki blue %s"),
83 #(r"(.*)\\includegraphics([^\[]*)([^\[]*)", "%s{{attachment:%s}} %s"),
84 (r"(.*)\\includegraphics\[([$\]]*)([^\[]*)", "%s{{attachment:%s}} %s"),
85 # arrow is a special command that appears in some of my files
86 (r"(.*)\\arrow (.*)", "%s-->%s"),
87 # footnotes get translated into parenthetical comments
88 # (which some people think is a preferable style anyway)
89 (r"(.*)\\footnote{(.*)}(.*)", "%s (footnote: %s) %s"),
90 # all items become bulleted items (no unemerations)
91 (r"\\item (.*)", " * %s"),
92 # verbatim becomes code display
93 (r"\\begin{verbatim}", "{{{{{"),
94 (r"\\end{verbatim}", "}}}}}"),
95 # listings becomes python code display
96 (r"\\begin{lstlisting}(.*)", "{{{{{#!python %s" ),
97 (r"\\end{lstlisting}", "}}}}}"),
98 # all other latex environments are ignored
99 (r"\\begin{.*}", ""),
100 (r"\\end{.*}", ""),
101 # turn Latex quotation marks into plain old quotation marks
102 (r"(.*)``(.*)''(.*)", "%s\"%s\"%s"),
103 # headings and title page entries
104 (r"\\paragraph{(.*)}", "==== %s ===="),
105 (r"\\subsubsection{(.*)}", "==== %s ===="),
106 (r"\\subsection{(.*)}", "=== %s ==="),
107 (r"\\section{(.*)}", "== %s =="),
108 (r"\\chapter{(.*)}", "= Chapter %s ="),
109 (r"\\title{(.*)}", "= %s ="),
110 (r"\\author{(.*)}", "%s"),
111 (r"\\date{(.*)}", "%s"),
112 (r"\\caption{(.*)}", "(caption: %s)"),
113
114 ]
115
116
117 def apply_rewrites(line, rewrites):
118 """apply each rewrite rule repeatedly and return the result"""
119 for reg, format in rewrites:
120 # keep applying each rule until it doesn't match.
121 # compiling the reg would probably improve performance.
122 while True:
123 m = re.search(reg, line)
124 if not m: break
125 line = format % m.groups()
126 return line
127
128
129 def main(script, infile, outfile=None):
130 in_stream = open(infile)
131 if outfile:
132 out_stream = open(outfile, 'w')
133 else:
134 out_stream = sys.stdout
135
136 for line in in_stream:
137 line = line.rstrip()
138 line = apply_rewrites(line, rewrites)
139 print >> out_stream, line
140
141 if __name__ == '__main__':
142 main(*sys.argv)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.