1 """
2 MediaWiki translator, aimed at Wikipedia/WikiBooks type of web pages.
3 Syntax defined by http://en.wikipedia.org/wiki/Help:Wiki_markup
4 and http://en.wikipedia.org/wiki/Help:Displaying_a_formula.
5 The prefix m in the name mwiki distinguishes this translator from
6 gwiki (googlecode wiki).
7
8 Not yet implemented:
9 mwiki_ref_and_label (just using code from gwiki)
10
11 Just using plan ASCII solutions for index_bib (requires some work to
12 port to MediaWiki, but is straightforward - use rst as template) and
13 exercise (probably ok with the plain solution).
14 """
15
16
17 import re, os, commands, sys
18 from common import default_movie, plain_exercise
19
21
22
23 envir2lang = dict(cod='python', pycod='python', cycod='python',
24 fcod='fortran', ccod='c', cppcod='cpp',
25 mcod='matlab', plcod='perl', shcod='bash',
26 pro='python', pypro='python', cypro='python',
27 fpro='fortran', cpro='c', cpppro='cpp',
28 mpro='matlab', plpro='perl', shpro='bash',
29 sys='bash', dat='python')
30
31 for key in envir2lang:
32 language = envir2lang[key]
33 cpattern = re.compile(r'^!bc\s+%s\s*\n' % key, flags=re.MULTILINE)
34 filestr = cpattern.sub('<syntaxhighlight lang="%s">\n' % \
35 envir2lang[key], filestr)
36 c = re.compile(r'^!bc.+$\n', re.MULTILINE)
37 filestr = c.sub('<code>\n', filestr)
38 filestr = re.sub(r'!ec\n', '</code>\n', filestr)
39 c = re.compile(r'^!bt\n', re.MULTILINE)
40 filestr = c.sub(':<math>\n', filestr)
41 filestr = re.sub(r'!et\n', '</math>\n', filestr)
42 return filestr
43
44
76
77 from common import table_analysis
78
79
80 -def mwiki_author(authors_and_institutions, auth2index,
81 inst2index, index2inst, auth2email):
82
83 authors = []
84 for author, i, email in authors_and_institutions:
85 if email is None:
86 email_text = ''
87 else:
88 name, adr = email.split('@')
89 email_text = ' (%s at %s)' % (name, adr)
90 authors.append('_%s_%s' % (author, email_text))
91
92 if len(authors) == 1:
93 authors = authors[0]
94 elif len(authors) == 2:
95 authors = authors[0] + ' and ' + authors[1]
96 elif len(authors) > 2:
97 authors[-1] = 'and ' + authors[-1]
98 authors = ', '.join(authors)
99 else:
100
101 return ''
102 text = '\n\nBy ' + authors + '\n\n'
103
104 return text
105
107
108
109 pattern = r'[Ss]ection(s?)\s+ref\{'
110 replacement = r'the section\g<1> ref{'
111 filestr = re.sub(pattern, replacement, filestr)
112 pattern = r'[Cc]hapter(s?)\s+ref\{'
113 replacement = r'the chapter\g<1> ref{'
114 filestr = re.sub(pattern, replacement, filestr)
115
116
117 filestr = re.sub(r'label\{.+?\}', '', filestr)
118
119
120
121
122 for label in section_label2title:
123 title = section_label2title[label]
124 filestr = filestr.replace('ref{%s}' % label,
125 '[#%s]' % title.replace(' ', '_'))
126
127 from common import ref2equations
128 filestr = ref2equations(filestr)
129
130
131 filestr = re.sub(r'ref\{(.+?)\}', '\g<1>', filestr)
132
133 return filestr
134
135
136 -def define(FILENAME_EXTENSION,
137 BLANKLINE,
138 INLINE_TAGS_SUBST,
139 CODE,
140 LIST,
141 ARGLIST,
142 TABLE,
143 EXERCISE,
144 FIGURE_EXT,
145 CROSS_REFS,
146 INDEX_BIB,
147 INTRO,
148 OUTRO):
149
150
151 FILENAME_EXTENSION['mwiki'] = '.mwiki'
152 BLANKLINE['mwiki'] = '\n'
153
154
155 INLINE_TAGS_SUBST['mwiki'] = {
156 'math': r'\g<begin><math>\g<subst></math>\g<end>',
157 'math2': r'\g<begin><math>\g<latexmath></math>\g<end>',
158 'emphasize': r"\g<begin>''\g<subst>''\g<end>",
159 'bold': r"\g<begin>'''\g<subst>'''\g<end>",
160 'verbatim': r'\g<begin><code>\g<subst></code>\g<end>',
161 'linkURL': r'\g<begin>[\g<url> \g<link>]\g<end>',
162 'linkURL2': r'[\g<url> \g<link>]',
163 'linkURL3': r'[\g<url> \g<link>]',
164 'linkURL2v': r'[\g<url> <code>\g<link></code>]',
165 'linkURL3v': r'[\g<url> <code>\g<link></code>]',
166 'plainURL': r'\g<url>',
167 'chapter': '\n\n\n' + r"""== '''\g<subst>''' ==\n""",
168 'section': '\n\n\n' + r'== \g<subst> ==\n',
169 'subsection': '\n\n' + r'=== \g<subst> ===\n',
170 'subsubsection': '\n' + r'==== \g<subst> ====\n',
171 'paragraph': r"''\g<subst>'' ",
172 'title': r'#TITLE (actually governed by the filename): \g<subst>\n',
173 'date': r'===== \g<subst> =====',
174 'author': mwiki_author,
175
176 'figure': mwiki_figure,
177 'movie': default_movie,
178 'comment': '<!--> %s -->',
179 'abstract': r'\n*\g<type>.* \g<text>\g<rest>',
180 }
181
182 CODE['mwiki'] = mwiki_code
183 from html import html_table
184 TABLE['mwiki'] = html_table
185
186
187 LIST['mwiki'] = {
188 'itemize': {'begin': '\n', 'item': '*', 'end': '\n\n'},
189 'enumerate': {'begin': '\n', 'item': '#', 'end': '\n\n'},
190 'description': {'begin': '\n', 'item': '* %s ', 'end': '\n\n'},
191 'separator': '\n'}
192
193 LIST['mwiki'] = LIST['html']
194
195
196
197
198 ARGLIST['mwiki'] = {
199 'parameter': '*argument*',
200 'keyword': '*keyword argument*',
201 'return': '*return value(s)*',
202 'instance variable': '*instance variable*',
203 'class variable': '*class variable*',
204 'module variable': '*module variable*',
205 }
206
207 FIGURE_EXT['mwiki'] = ('.png', '.gif', '.jpg', '.jpeg')
208 CROSS_REFS['mwiki'] = mwiki_ref_and_label
209 from plaintext import plain_index_bib
210 EXERCISE['mwiki'] = plain_exercise
211 INDEX_BIB['mwiki'] = plain_index_bib
212
213
214 INTRO['mwiki'] = ''
215