Package doconce :: Module pandoc
[hide private]
[frames] | no frames]

Source Code for Module doconce.pandoc

  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 # List authors on multiple lines 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
26 -def pandoc_code(filestr, format):
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 # (the "python" typesetting is neutral if the text 33 # does not parse as python) 34 35 # Code blocks apply the ~~~~~ delimiter, with blank lines before 36 # and after (alternative: indent code 4 spaces - not preferred) 37 for key in defs: 38 language = defs[key] 39 replacement = '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.%s}\n' % defs[key] 40 #replacement = '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.%s ,numberLines}\n' % defs[key] # enable line numbering 41 #filestr = re.sub(r'^!bc\s+%s\s*\n' % key, 42 # replacement, filestr, flags=re.MULTILINE) 43 cpattern = re.compile(r'^!bc\s+%s\s*\n' % key, flags=re.MULTILINE) 44 filestr = cpattern.sub(replacement, filestr) 45 46 # any !bc with/without argument becomes a cod (python) block: 47 #replacement = '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.%s}' % defs['cod'] 48 # or no, we then just skip any specification 49 replacement = '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' 50 #filestr = re.sub(r'^!bc.+\n', replacement, filestr, flags=re.MULTILINE) 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 # pandoc supports plain LaTeX, just remove !bt and !et 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
61 -def pandoc_table(table):
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 # Slight modification of rst_table 74 # Here is the pandoc table format 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 = '' # '\n' 82 for i, row in enumerate(table['rows']): 83 #s += ' ' # indentation of tables 84 if row == ['horizontal rule'] and i > 0 and i < len(table['rows'])-1: 85 # No horizontal rule at the top and bottom, just after heading 86 for w in column_width: 87 s += '-'*w + ' ' 88 else: 89 # check if this is a headline between two horizontal rules: 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
107 -def pandoc_ref_and_label(section_label2title, format, filestr):
108 # .... see section ref{my:sec} is replaced by 109 # see the section "...section heading..." 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 # Remove label{...} from output (when only label{} on a line, remove 118 # the newline too, leave label in figure captions, and remove all the rest) 119 #filestr = re.sub(r'^label\{.+?\}\s*$', '', filestr, flags=re.MULTILINE) 120 cpattern = re.compile(r'^label\{.+?\}\s*$', flags=re.MULTILINE) 121 filestr = cpattern.sub('', filestr) 122 #filestr = re.sub(r'^(FIGURE:.+)label\{(.+?)\}', '\g<1>{\g<2>}', filestr, flags=re.MULTILINE) 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) # all the remaining 126 127 # Replace all references to sections. Pandoc needs a coding of 128 # the section header as link. 129 def title2pandoc(title): 130 # http://johnmacfarlane.net/pandoc/README.html 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
155 -def pandoc_index_bib(filestr, index, citations, bibfile):
156 # pandoc citations are of the form 157 # bla-bla, see [@Smith04, ch. 1; @Langtangen_2008] 158 # Method: cite{..} -> [...], doconce.py has already fixed @ and ; 159 filestr = re.sub(r'cite\{(.+?)\}', r'[\g<1>]', filestr) 160 161 #filestr = re.sub(r'^BIBFILE:.+$', bibtext, filestr, flags=re.MULTILINE) 162 cpattern = re.compile(r'^BIBFILE:.+$', flags=re.MULTILINE) 163 filestr = cpattern.sub('# References', filestr) 164 165 # pandoc does not support index entries, 166 # remove all index entries 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 # all arguments are dicts and accept in-place modifications (extensions) 185 186 FILENAME_EXTENSION['pandoc'] = '.mkd' 187 BLANKLINE['pandoc'] = '\n' 188 # replacement patterns for substitutions of inline tags 189 INLINE_TAGS_SUBST['pandoc'] = { 190 'math': None, # indicates no substitution, leave as is 191 'math2': r'\g<begin>$\g<latexmath>$\g<end>', 192 'emphasize': None, 193 'bold': None, 194 'figure': r'![\g<caption>](\g<filename>)', 195 #'movie': default_movie, 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 # "Reference links" in pandoc are not yet supported 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>* ', # extra blank 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 # no return, rely on in-place modification of dictionaries 238