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

Source Code for Module doconce.cwiki

  1  """ 
  2  Creole Wiki (as used on bitbucket.org). 
  3  See http://www.wikicreole.org/wiki/Creole1.0 for syntax. 
  4  """ 
  5  # Simple edit of gwiki.py 
  6   
  7  import re, os, commands, sys 
  8  from common import default_movie, plain_exercise 
  9   
10 -def cwiki_code(filestr, format):
11 c = re.compile(r'^!bc(.*?)\n', re.MULTILINE) 12 filestr = c.sub(r'{{{\n', filestr) 13 filestr = re.sub(r'!ec\n', r'}}}\n', filestr) 14 c = re.compile(r'^!bt\n', re.MULTILINE) 15 filestr = c.sub(r'{{{\n', filestr) 16 filestr = re.sub(r'!et\n', r'}}}\n', filestr) 17 return filestr
18
19 -def cwiki_figure(m):
20 filename = m.group('filename') 21 if not os.path.isfile(filename): 22 raise IOError('no figure file %s' % filename) 23 24 basename = os.path.basename(filename) 25 stem, ext = os.path.splitext(basename) 26 root, ext = os.path.splitext(filename) 27 if not ext in '.png .gif .jpg .jpeg'.split(): 28 # try to convert image file to PNG, using 29 # convert from ImageMagick: 30 cmd = 'convert %s png:%s' % (filename, root+'.png') 31 failure, output = commands.getstatusoutput(cmd) 32 if failure: 33 print '\n**** Warning: could not run', cmd 34 print 'Convert %s to PNG format manually' % filename 35 sys.exit(1) 36 filename = root + '.png' 37 caption = m.group('caption') 38 # keep label if it's there: 39 caption = re.sub(r'label\{(.+?)\}', '(\g<1>)', caption) 40 41 result = r"""{{%s|%s}}""" % (filename, caption) 42 return result
43 44 from common import table_analysis 45
46 -def cwiki_table(table):
47 """Native Creole wiki table.""" 48 # add 2 chars for column width since we add boldface _..._ 49 # in headlines: 50 column_width = [c+2 for c in table_analysis(table['rows'])] 51 52 s = '\n' 53 for i, row in enumerate(table['rows']): 54 if row == ['horizontal rule']: 55 continue 56 if i == 1 and \ 57 table['rows'][i-1] == ['horizontal rule'] and \ 58 table['rows'][i+1] == ['horizontal rule']: 59 headline = True 60 else: 61 headline = False 62 63 for column, w in zip(row, column_width): 64 if headline: 65 c = '=%s ' % ((column).ljust(w)) 66 else: 67 c = ' %s ' % column.ljust(w) 68 s += ' | %s ' % c 69 s += ' |\n' 70 s += '\n\n' 71 return s
72
73 -def cwiki_author(authors_and_institutions, auth2index, 74 inst2index, index2inst, auth2email):
75 76 authors = [] 77 for author, i, email in authors_and_institutions: 78 if email is None: 79 email_text = '' 80 else: 81 name, adr = email.split('@') 82 email_text = ' (%s at %s)' % (name, adr) 83 authors.append('_%s_%s' % (author, email_text)) 84 85 if len(authors) == 1: 86 authors = authors[0] 87 elif len(authors) == 2: 88 authors = authors[0] + ' and ' + authors[1] 89 elif len(authors) > 2: 90 authors[-1] = 'and ' + authors[-1] 91 authors = ', '.join(authors) 92 else: 93 # no authors: 94 return '' 95 text = '\n\nBy ' + authors + '\n\n' 96 # we skip institutions in Creole wiki 97 return text
98
99 -def cwiki_ref_and_label(section_label2title, format, filestr):
100 # .... see section ref{my:sec} is replaced by 101 # see the section "...section heading..." 102 pattern = r'[Ss]ection(s?)\s+ref\{' 103 replacement = r'the section\g<1> ref{' 104 filestr = re.sub(pattern, replacement, filestr) 105 pattern = r'[Cc]hapter(s?)\s+ref\{' 106 replacement = r'the chapter\g<1> ref{' 107 filestr = re.sub(pattern, replacement, filestr) 108 109 # remove label{...} from output 110 filestr = re.sub(r'label\{.+?\}', '', filestr) # all the remaining 111 112 # anchors in titles do not work... 113 114 # replace all references to sections: 115 for label in section_label2title: 116 title = section_label2title[label] 117 filestr = filestr.replace('ref{%s}' % label, 118 '[#%s]' % title.replace(' ', '_')) 119 120 from common import ref2equations 121 filestr = ref2equations(filestr) 122 123 # replace remaining ref{x} as x 124 filestr = re.sub(r'ref\{(.+?)\}', '\g<1>', filestr) 125 126 return filestr
127 128
129 -def define(FILENAME_EXTENSION, 130 BLANKLINE, 131 INLINE_TAGS_SUBST, 132 CODE, 133 LIST, 134 ARGLIST, 135 TABLE, 136 EXERCISE, 137 FIGURE_EXT, 138 CROSS_REFS, 139 INDEX_BIB, 140 INTRO, 141 OUTRO):
142 # all arguments are dicts and accept in-place modifications (extensions) 143 144 FILENAME_EXTENSION['cwiki'] = '.cwiki' # output file extension 145 BLANKLINE['cwiki'] = '\n' 146 147 # replacement patterns for substitutions of inline tags 148 INLINE_TAGS_SUBST['cwiki'] = { 149 # use verbatim mode for math: 150 'math': r'\g<begin>{{{\g<subst>}}}\g<end>', 151 'math2': r'\g<begin>\g<puretext>\g<end>', 152 'emphasize': r'\g<begin>//\g<subst>//\g<end>', 153 'bold': r'\g<begin>**\g<subst>**\g<end>', 154 'verbatim': r'\g<begin>{{{\g<subst>}}}\g<end>', 155 'linkURL': r'\g<begin>[[\g<url>|\g<link>]]\g<end>', 156 'linkURL2': r'[[\g<url>|\g<link>]]', 157 'linkURL3': r'[[\g<url>|\g<link>]]', 158 'linkURL2v': r'[[\g<url>|{{{\g<link>}}}]]', 159 'linkURL3v': r'[[\g<url>|{{{\g<link>}}}]]', 160 'plainURL': r'[[\g<url>]]', 161 'chapter': '\n\n\n' + r'= \g<subst> =\n', 162 'section': '\n\n\n' + r'= \g<subst> =\n', 163 'subsection': '\n\n' + r'== \g<subst> ==\n', 164 'subsubsection': '\n' + r'=== \g<subst> ===\n', 165 # 'section': r'++++ \g<subst> ++++', 166 # 'subsection': r'++++++ \g<subst> ++++++', 167 # 'subsubsection': r'++++++++ \g<subst> ++++++++', 168 'paragraph': r'//\g<subst>// ', 169 'title': r'#summary \g<subst>\n<wiki:toc max_depth="2" />', 170 'date': r'=== \g<subst> ===', 171 'author': cwiki_author, #r'===== \g<name>, \g<institution> =====', 172 # 'figure': r'<\g<filename>>', 173 'figure': cwiki_figure, 174 'movie': default_movie, # will not work for HTML movie player 175 'comment': '<wiki:comment> %s </wiki:comment>', 176 'abstract': r'\n//\g<type>.// \g<text>\g<rest>', 177 } 178 179 CODE['cwiki'] = cwiki_code 180 #from html import html_table 181 #TABLE['cwiki'] = html_table 182 TABLE['cwiki'] = cwiki_table 183 184 # native list: 185 LIST['cwiki'] = { 186 'itemize': {'begin': '\n', 'item': '*', 'end': '\n\n'}, 187 'enumerate': {'begin': '\n', 'item': '#', 'end': '\n\n'}, 188 'description': {'begin': '\n', 'item': '* %s ', 'end': '\n\n'}, 189 'separator': '\n'} 190 # (the \n\n for end is a hack because doconce.py avoids writing 191 # newline at the end of lists until the next paragraph is hit) 192 #LIST['cwiki'] = LIST['HTML'] # does not work well 193 194 195 # how to typeset description lists for function arguments, return 196 # values, and module/class variables: 197 ARGLIST['cwiki'] = { 198 'parameter': '*argument*', 199 'keyword': '*keyword argument*', 200 'return': '*return value(s)*', 201 'instance variable': '*instance variable*', 202 'class variable': '*class variable*', 203 'module variable': '*module variable*', 204 } 205 206 FIGURE_EXT['cwiki'] = ('.png', '.gif', '.jpg', '.jpeg') 207 CROSS_REFS['cwiki'] = cwiki_ref_and_label 208 from plaintext import plain_index_bib 209 EXERCISE['cwiki'] = plain_exercise 210 INDEX_BIB['cwiki'] = plain_index_bib 211 212 # document start: 213 INTRO['cwiki'] = ''
214 #INTRO['cwiki'] = '#summary YourOneLineSummary\n<wiki:toc max_depth="1" />\n' 215