-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex2twiki.py
138 lines (116 loc) · 3.46 KB
/
latex2twiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
# Original idea from :
# Maxime Biais <[email protected]>
# but has been nearly all rewritten since...
# Marc Poulhiès <[email protected]>
#
# 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: latex2twiki.py,v 1.2 2005/07/27 12:40:53 poulhies Exp $
import sys, re
bullet_level=0
bdoc = None
end_line = 1
verbatim_mode = 0
def dummy():
pass
def inc_bullet():
global bullet_level
bullet_level += 1
def dec_bullet():
global bullet_level
bullet_level -= 1
def start_doc():
global bdoc;
bdoc = 1
def do_not_el():
global end_line
end_line = None
def do_el():
global end_line;
end_line = 1
def decide_el():
global end_line
if bullet_level == 0:
return "\n"
else:
return " "
def start_verbatim():
global verbatim_mode
verbatim_mode = 1
def end_verbatim():
global verbatim_mode
verbatim_mode = 0
conv_table = { '>':'>',
'<':'<'}
def translate_to_html(char):
global verbatim_mode
global conv_table
if verbatim_mode == 0:
return conv_table[char]
else:
return char
def header(i):
offset = 1
return r"---"+r'+' * (i+offset) + r" \1"
NONE = "__@NONE@__"
tr_list2 = [
(r">", (lambda: translate_to_html('>')), dummy),
(r"<", (lambda: translate_to_html('<')), dummy),
(r"\\footnotesize", None, dummy),
(r"\\small", None, dummy),
(r"\\begin\{document}", None, start_doc),
(r"\\\\$", (lambda : "\n\n"), dummy),
(r"\\\$", (lambda : "$"), dummy),
(r"\\emph{(.*?)}", (lambda : r"_\1_ "), dummy),
(r"\\textit{(.*?)}", (lambda :r"_\1_ "), dummy),
(r"\\texttt{(.*?)}", (lambda : r"=\1= "), dummy),
(r"\\textbf{(.*?)}", (lambda : r"*\1* "), dummy),
(r"\\begin{verbatim}", (lambda : "<verbatim>"), start_verbatim),
(r"\\end{verbatim}", (lambda : "</verbatim>"), end_verbatim),
(r"\\begin{itemize}", (lambda : "\n"), inc_bullet),
(r"\\end{itemize}", None, dec_bullet),
(r"\\item (.*?)", (lambda : r"\n" + (r" " * bullet_level) + r"* \1"), dummy),
(r"\\begin{.*?}", None, dummy),
(r"\\end{.*?}", None, dummy),
(r"``(.*?)''", (lambda :r'"\1"'), dummy),
(r"\\subsubsection{(.*?)}", (lambda : header(3)), dummy),
(r"\\subsection{(.*?)}", (lambda : header(2)), dummy),
(r"\\section{(.*?)}", (lambda : header(1)), dummy),
(r"\\_", (lambda :"_"), dummy),
(r"\\tableofcontents",None, dummy),
(r"\\null",None, dummy),
(r"\\newpage",None, dummy),
(r"\\thispagestyle{.*?}", None, dummy),
(r"\\maketitle", None, dummy),
(r"\n$", decide_el, dummy),
(r"[^\\]?\{", None, dummy),
(r"[^\\]?\}", None, dummy)
]
in_stream = sys.stdin;
out_stream = sys.stdout
for i in in_stream.readlines():
mystr = i
for reg in tr_list2:
p = re.compile(reg[0])
if p.search(mystr):
reg[2]()
if reg[1] != None:
mystr = p.sub(reg[1](), mystr)
else:
mystr = p.sub("", mystr)
if bdoc != None:
print >> out_stream, mystr,