1 """
2 Google Code Wiki translator.
3 Syntax defined by http://code.google.com/p/support/wiki/WikiSyntax
4 Here called gwiki to make the dialect clear (g for google).
5 """
6
7
8 import re, os, commands, sys
9 from common import default_movie, plain_exercise
10
12 c = re.compile(r'^!bc(.*?)\n', re.MULTILINE)
13 filestr = c.sub(r'{{{\n', filestr)
14 filestr = re.sub(r'!ec\n', r'}}}\n', filestr)
15 c = re.compile(r'^!bt\n', re.MULTILINE)
16 filestr = c.sub(r'{{{\n', filestr)
17 filestr = re.sub(r'!et\n', r'}}}\n', filestr)
18 return filestr
19
65
66 from common import table_analysis
67
69 """Native gwiki table."""
70
71
72 column_width = [c+2 for c in table_analysis(table['rows'])]
73
74
75
76
77
78
79 s = '\n'
80 for i, row in enumerate(table['rows']):
81 if row == ['horizontal rule']:
82 continue
83 if i == 1 and \
84 table['rows'][i-1] == ['horizontal rule'] and \
85 table['rows'][i+1] == ['horizontal rule']:
86 headline = True
87 else:
88 headline = False
89
90 for column, w in zip(row, column_width):
91 if headline:
92 c = ' %s ' % (('_'+ column + '_').center(w))
93 else:
94 c = ' %s ' % column.ljust(w)
95 s += ' || %s ' % c
96 s += ' ||\n'
97 s += '\n\n'
98 return s
99
100 -def gwiki_author(authors_and_institutions, auth2index,
101 inst2index, index2inst, auth2email):
102
103 authors = []
104 for author, i, email in authors_and_institutions:
105 if email is None:
106 email_text = ''
107 else:
108 name, adr = email.split('@')
109 email_text = ' (%s at %s)' % (name, adr)
110 authors.append('_%s_%s' % (author, email_text))
111
112 if len(authors) == 1:
113 authors = authors[0]
114 elif len(authors) == 2:
115 authors = authors[0] + ' and ' + authors[1]
116 elif len(authors) > 2:
117 authors[-1] = 'and ' + authors[-1]
118 authors = ', '.join(authors)
119 else:
120
121 return ''
122 text = '\n\nBy ' + authors + '\n\n'
123
124 return text
125
127
128
129 pattern = r'[Ss]ection(s?)\s+ref\{'
130 replacement = r'the section\g<1> ref{'
131 filestr = re.sub(pattern, replacement, filestr)
132 pattern = r'[Cc]hapter(s?)\s+ref\{'
133 replacement = r'the chapter\g<1> ref{'
134 filestr = re.sub(pattern, replacement, filestr)
135
136
137 filestr = re.sub(r'label\{.+?\}', '', filestr)
138
139
140
141
142 for label in section_label2title:
143 title = section_label2title[label]
144 filestr = filestr.replace('ref{%s}' % label,
145 '[#%s]' % title.replace(' ', '_'))
146
147 from common import ref2equations
148 filestr = ref2equations(filestr)
149
150
151 filestr = re.sub(r'ref\{(.+?)\}', '\g<1>', filestr)
152
153 return filestr
154
155
156 -def define(FILENAME_EXTENSION,
157 BLANKLINE,
158 INLINE_TAGS_SUBST,
159 CODE,
160 LIST,
161 ARGLIST,
162 TABLE,
163 EXERCISE,
164 FIGURE_EXT,
165 CROSS_REFS,
166 INDEX_BIB,
167 INTRO,
168 OUTRO):
169
170
171 FILENAME_EXTENSION['gwiki'] = '.gwiki'
172 BLANKLINE['gwiki'] = '\n'
173
174
175 INLINE_TAGS_SUBST['gwiki'] = {
176
177 'math': r'\g<begin>`\g<subst>`\g<end>',
178 'math2': r'\g<begin>`\g<puretext>`\g<end>',
179 'emphasize': r'\g<begin>_\g<subst>_\g<end>',
180 'bold': r'\g<begin>*\g<subst>*\g<end>',
181 'verbatim': r'\g<begin>`\g<subst>`\g<end>',
182 'linkURL': r'\g<begin>[\g<url> \g<link>]\g<end>',
183 'linkURL2': r'[\g<url> \g<link>]',
184 'linkURL3': r'[\g<url> \g<link>]',
185 'linkURL2v': r"[\g<url> `\g<link>`]",
186 'linkURL3v': r"[\g<url> `\g<link>`]",
187 'plainURL': r'\g<url>',
188 'chapter': '\n\n\n' + r'= \g<subst> =\n',
189 'section': '\n\n\n' + r'== \g<subst> ==\n',
190 'subsection': '\n\n' + r'=== \g<subst> ===\n',
191 'subsubsection': '\n' + r'==== \g<subst> ====\n',
192
193
194
195 'paragraph': r'*\g<subst>* ',
196 'title': r'#summary \g<subst>\n<wiki:toc max_depth="2" />',
197 'date': r'===== \g<subst> =====',
198 'author': gwiki_author,
199
200 'figure': gwiki_figure,
201 'movie': default_movie,
202 'comment': '<wiki:comment> %s </wiki:comment>',
203 'abstract': r'\n*\g<type>.* \g<text>\g<rest>',
204 }
205
206 CODE['gwiki'] = gwiki_code
207 from html import html_table
208
209 TABLE['gwiki'] = gwiki_table
210
211
212 LIST['gwiki'] = {
213 'itemize': {'begin': '\n', 'item': '*', 'end': '\n\n'},
214 'enumerate': {'begin': '\n', 'item': '#', 'end': '\n\n'},
215 'description': {'begin': '\n', 'item': '* %s ', 'end': '\n\n'},
216 'separator': '\n'}
217
218
219
220
221
222
223
224 ARGLIST['gwiki'] = {
225 'parameter': '*argument*',
226 'keyword': '*keyword argument*',
227 'return': '*return value(s)*',
228 'instance variable': '*instance variable*',
229 'class variable': '*class variable*',
230 'module variable': '*module variable*',
231 }
232
233 FIGURE_EXT['gwiki'] = ('.png', '.gif', '.jpg', '.jpeg')
234 CROSS_REFS['gwiki'] = gwiki_ref_and_label
235 from plaintext import plain_index_bib
236 EXERCISE['gwiki'] = plain_exercise
237 INDEX_BIB['gwiki'] = plain_index_bib
238
239
240 INTRO['gwiki'] = ''
241
242