1 """
2 Warning: This translater should be complete
3 (no "reference links", though), but it is
4 not tested.
5
6 See http://johnmacfarlane.net/pandoc/README.html
7 for syntax.
8 """
9
10 import re, sys
11 from common import default_movie, plain_exercise, table_analysis
12 from html import html_movie
13
14 -def pandoc_author(authors_and_institutions, auth2index,
15 inst2index, index2inst, auth2email):
16
17 authors = []
18 for author, i, e in authors_and_institutions:
19 if i is None:
20 authors.append(author)
21 else:
22 authors.append(author + ' at ' + ' and '.join(i))
23 authors = '% ' + '; '.join(authors) + '\n'
24 return authors
25
27 defs = dict(cod='Python', pycod='Python', cppcod='Cpp',
28 fcod='Fortran', ccod='C',
29 pro='Python', pypro='Python', cpppro='Cpp',
30 fpro='Fortran', cpro='C',
31 sys='Bash', dat='Python')
32
33
34
35
36
37 for key in defs:
38 language = defs[key]
39 replacement = '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.%s}\n' % defs[key]
40
41
42
43 cpattern = re.compile(r'^!bc\s+%s\s*\n' % key, flags=re.MULTILINE)
44 filestr = cpattern.sub(replacement, filestr)
45
46
47
48
49 replacement = '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
50
51 cpattern = re.compile(r'^!bc.+$', flags=re.MULTILINE)
52 filestr = cpattern.sub(replacement, filestr)
53
54 filestr = re.sub(r'!ec *\n', '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n', filestr)
55
56 filestr = re.sub(r'!bt *\n', '\n\n', filestr)
57 filestr = re.sub(r'!et *\n', '\n\n', filestr)
58
59 return filestr
60
62 """
63
64 Simple markdown tables look like this::
65
66 Left Right Center Default
67 ------- ------ ---------- -------
68 12 12 12 12
69 123 123 123 123
70 1 1 1 1
71
72 """
73
74
75
76 column_width = table_analysis(table['rows'])
77 ncolumns = len(column_width)
78 column_spec = table.get('columns_align', 'c'*ncolumns).replace('|', '')
79 heading_spec = table.get('headings_align', 'c'*ncolumns).replace('|', '')
80 a2py = {'r': 'rjust', 'l': 'ljust', 'c': 'center'}
81 s = ''
82 for i, row in enumerate(table['rows']):
83
84 if row == ['horizontal rule'] and i > 0 and i < len(table['rows'])-1:
85
86 for w in column_width:
87 s += '-'*w + ' '
88 else:
89
90 if i == 1 and \
91 table['rows'][i-1] == ['horizontal rule'] and \
92 table['rows'][i+1] == ['horizontal rule']:
93 headline = True
94 else:
95 headline = False
96
97 for w, c, ha, ca in \
98 zip(column_width, row, heading_spec, column_spec):
99 if headline:
100 s += getattr(c, a2py[ha])(w) + ' '
101 elif row != ['horizontal rule']:
102 s += getattr(c, a2py[ca])(w) + ' '
103 s += '\n'
104 s += '\n'
105 return s
106
108
109
110 pattern = r'[Ss]ection(s?)\s+ref\{'
111 replacement = r'the section\g<1> ref{'
112 filestr = re.sub(pattern, replacement, filestr)
113 pattern = r'[Cc]hapter(s?)\s+ref\{'
114 replacement = r'the chapter\g<1> ref{'
115 filestr = re.sub(pattern, replacement, filestr)
116
117
118
119
120 cpattern = re.compile(r'^label\{.+?\}\s*$', flags=re.MULTILINE)
121 filestr = cpattern.sub('', filestr)
122
123 cpattern = re.compile(r'^(FIGURE:.+)label\{(.+?)\}', flags=re.MULTILINE)
124 filestr = cpattern.sub('\g<1>{\g<2>}', filestr)
125 filestr = re.sub(r'label\{.+?\}', '', filestr)
126
127
128
129 def title2pandoc(title):
130
131 for c in ('?', ';', ':'):
132 title = title.replace(c, '')
133 title = title.replace(' ', '-').strip()
134 start = 0
135 for i in range(len(title)):
136 if title[i].isalpha():
137 start = i
138 title = title[start:]
139 title = title.lower()
140 if not title:
141 title = 'section'
142 return title
143
144 for label in section_label2title:
145 filestr = filestr.replace('ref{%s}' % label,
146 '[%s](#%s)' % (section_label2title[label],
147 title2pandoc(section_label2title[label])))
148
149 from common import ref2equations
150 filestr = ref2equations(filestr)
151
152 return filestr
153
154
156
157
158
159 filestr = re.sub(r'cite\{(.+?)\}', r'[\g<1>]', filestr)
160
161
162 cpattern = re.compile(r'^BIBFILE:.+$', flags=re.MULTILINE)
163 filestr = cpattern.sub('# References', filestr)
164
165
166
167
168 filestr = re.sub(r'idx\{.+?\}' + '\n?', '', filestr)
169 return filestr
170
171 -def define(FILENAME_EXTENSION,
172 BLANKLINE,
173 INLINE_TAGS_SUBST,
174 CODE,
175 LIST,
176 ARGLIST,
177 TABLE,
178 EXERCISE,
179 FIGURE_EXT,
180 CROSS_REFS,
181 INDEX_BIB,
182 INTRO,
183 OUTRO):
184
185
186 FILENAME_EXTENSION['pandoc'] = '.mkd'
187 BLANKLINE['pandoc'] = '\n'
188
189 INLINE_TAGS_SUBST['pandoc'] = {
190 'math': None,
191 'math2': r'\g<begin>$\g<latexmath>$\g<end>',
192 'emphasize': None,
193 'bold': None,
194 'figure': r'',
195
196 'movie': html_movie,
197 'verbatim': None,
198 'linkURL': r'\g<begin>\g<link> (\g<url>)\g<end>',
199 'linkURL2': r'[\g<link>](\g<url>)',
200 'linkURL3': r'[\g<link>](\g<url>)',
201 'linkURL2v': r'[`\g<link>`](\g<url>)',
202 'linkURL3v': r'[`\g<link>`](\g<url>)',
203 'plainURL': r'<\g<url>>',
204
205 'title': r'% \g<subst>',
206 'author': pandoc_author,
207 'date': '% \g<subst>\n',
208 'chapter': lambda m: r'\g<subst>\n%s' % ('%'*len(m.group('subst').decode('utf-8'))),
209 'section': lambda m: r'\g<subst>\n%s' % ('='*len(m.group('subst').decode('utf-8'))),
210 'subsection': lambda m: r'\g<subst>\n%s' % ('-'*len(m.group('subst').decode('utf-8'))),
211 'subsubsection': lambda m: r'\g<subst>\n%s' % ('~'*len(m.group('subst').decode('utf-8'))),
212 'paragraph': r'*\g<subst>* ',
213 'abstract': r'*\g<type>.* \g<text> '
214 }
215
216 CODE['pandoc'] = pandoc_code
217 from common import DEFAULT_ARGLIST
218 ARGLIST['pandoc'] = DEFAULT_ARGLIST
219 LIST['pandoc'] = {
220 'itemize':
221 {'begin': '', 'item': '*', 'end': '\n'},
222
223 'enumerate':
224 {'begin': '', 'item': '%d.', 'end': '\n'},
225
226 'description':
227 {'begin': '', 'item': '%s\n : ', 'end': '\n'},
228
229 'separator': '\n',
230 }
231 CROSS_REFS['pandoc'] = pandoc_ref_and_label
232
233 TABLE['pandoc'] = pandoc_table
234 INDEX_BIB['pandoc'] = pandoc_index_bib
235 EXERCISE['pandoc'] = plain_exercise
236
237
238