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

Source Code for Module doconce.gwiki

  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   
11 -def gwiki_code(filestr, format):
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
20 -def gwiki_figure(m):
21 filename = m.group('filename') 22 if not os.path.isfile(filename): 23 raise IOError('no figure file %s' % filename) 24 25 basename = os.path.basename(filename) 26 stem, ext = os.path.splitext(basename) 27 root, ext = os.path.splitext(filename) 28 if not ext in '.png .gif .jpg .jpeg'.split(): 29 # try to convert image file to PNG, using 30 # convert from ImageMagick: 31 cmd = 'convert %s png:%s' % (filename, root+'.png') 32 failure, output = commands.getstatusoutput(cmd) 33 if failure: 34 print '\n**** Warning: could not run', cmd 35 print 'Convert %s to PNG format manually' % filename 36 sys.exit(1) 37 filename = root + '.png' 38 caption = m.group('caption') 39 # keep label if it's there: 40 caption = re.sub(r'label\{(.+?)\}', '(\g<1>)', caption) 41 42 print """ 43 NOTE: Place %s at some place on the web and edit the 44 .gwiki page, either manually (seach for 'Figure: ') 45 or use the doconce script: 46 doconce gwiki_figsubst.py mydoc.gwiki URL 47 """ % filename 48 49 result = r""" 50 51 --------------------------------------------------------------- 52 53 Figure: %s 54 55 (the URL of the image file %s must be inserted here) 56 57 <wiki:comment> 58 Put the figure file %s on the web (e.g., as part of the 59 googlecode repository) and substitute the line above with the URL. 60 </wiki:comment> 61 --------------------------------------------------------------- 62 63 """ % (caption, filename, filename) 64 return result
65 66 from common import table_analysis 67
68 -def gwiki_table(table):
69 """Native gwiki table.""" 70 # add 2 chars for column width since we add boldface _..._ 71 # in headlines: 72 column_width = [c+2 for c in table_analysis(table['rows'])] 73 74 # Does column and heading alignment matter? 75 # Not according to http://code.google.com/p/support/wiki/WikiSyntax#Tables 76 # but it is possible to use HTML code in gwiki (i.e., html_table) 77 # (think this was tried without success...) 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 # no authors: 121 return '' 122 text = '\n\nBy ' + authors + '\n\n' 123 # we skip institutions in gwiki 124 return text
125
126 -def gwiki_ref_and_label(section_label2title, format, filestr):
127 # .... see section ref{my:sec} is replaced by 128 # see the section "...section heading..." 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 # remove label{...} from output 137 filestr = re.sub(r'label\{.+?\}', '', filestr) # all the remaining 138 139 # anchors in titles do not work... 140 141 # replace all references to sections: 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 # replace remaining ref{x} as x 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 # all arguments are dicts and accept in-place modifications (extensions) 170 171 FILENAME_EXTENSION['gwiki'] = '.gwiki' # output file extension 172 BLANKLINE['gwiki'] = '\n' 173 174 # replacement patterns for substitutions of inline tags 175 INLINE_TAGS_SUBST['gwiki'] = { 176 # use verbatim mode for math: 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 # 'section': r'++++ \g<subst> ++++', 193 # 'subsection': r'++++++ \g<subst> ++++++', 194 # 'subsubsection': r'++++++++ \g<subst> ++++++++', 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, #r'===== \g<name>, \g<institution> =====', 199 # 'figure': r'<\g<filename>>', 200 'figure': gwiki_figure, 201 'movie': default_movie, # will not work for HTML movie player 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 #TABLE['gwiki'] = html_table 209 TABLE['gwiki'] = gwiki_table 210 211 # native list: 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 # (the \n\n for end is a hack because doconce.py avoids writing 218 # newline at the end of lists until the next paragraph is hit) 219 #LIST['gwiki'] = LIST['HTML'] # does not work well 220 221 222 # how to typeset description lists for function arguments, return 223 # values, and module/class variables: 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 # document start: 240 INTRO['gwiki'] = ''
241 #INTRO['gwiki'] = '#summary YourOneLineSummary\n<wiki:toc max_depth="1" />\n' 242