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

Source Code for Module doconce.mwiki

  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   
20 -def mwiki_code(filestr, format):
21 # Supported programming languages: 22 # http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi#Supported_languages 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
45 -def mwiki_figure(m):
46 filename = m.group('filename') 47 if not os.path.isfile(filename): 48 raise IOError('no figure file %s' % filename) 49 50 basename = os.path.basename(filename) 51 stem, ext = os.path.splitext(basename) 52 root, ext = os.path.splitext(filename) 53 if not ext in '.png .gif .jpg .jpeg'.split(): 54 # try to convert image file to PNG, using 55 # convert from ImageMagick: 56 cmd = 'convert %s png:%s' % (filename, root+'.png') 57 failure, output = commands.getstatusoutput(cmd) 58 if failure: 59 print '\n**** Warning: could not run', cmd 60 print 'Convert %s to PNG format manually' % filename 61 sys.exit(1) 62 filename = root + '.png' 63 caption = m.group('caption') 64 # keep label if it's there: 65 caption = re.sub(r'label\{(.+?)\}', '(\g<1>)', caption) 66 67 print """ 68 NOTE: Upload image file %s to the Wiki* site 69 (see http://en.wikipedia.org/wiki/Special:Upload for Wikipedia) 70 """ % filename 71 72 result = r""" 73 [[File:%s|frame|alt=%s|%s]] 74 """ % (filename, filename, caption) 75 return result
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 # no authors: 101 return '' 102 text = '\n\nBy ' + authors + '\n\n' 103 # we skip institutions in mwiki 104 return text
105
106 -def mwiki_ref_and_label(section_label2title, format, filestr):
107 # .... see section ref{my:sec} is replaced by 108 # see the section "...section heading..." 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 # remove label{...} from output 117 filestr = re.sub(r'label\{.+?\}', '', filestr) # all the remaining 118 119 # anchors in titles do not work... 120 121 # replace all references to sections: 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 # replace remaining ref{x} as x 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 # all arguments are dicts and accept in-place modifications (extensions) 150 151 FILENAME_EXTENSION['mwiki'] = '.mwiki' # output file extension 152 BLANKLINE['mwiki'] = '\n' 153 154 # replacement patterns for substitutions of inline tags 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, #r'===== \g<name>, \g<institution> =====', 175 # 'figure': r'<\g<filename>>', 176 'figure': mwiki_figure, 177 'movie': default_movie, # will not work for HTML movie player 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 # native list: 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'} # problem: requires ** and ## at level 2 etc. 192 # Try this! 193 LIST['mwiki'] = LIST['html'] 194 195 196 # how to typeset description lists for function arguments, return 197 # values, and module/class variables: 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 # document start: 214 INTRO['mwiki'] = ''
215