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

Source Code for Module doconce.plaintext

  1   
  2  import re, sys 
  3  from common import default_movie, plain_exercise 
  4   
5 -def plain_author(authors_and_institutions, auth2index, 6 inst2index, index2inst, auth2email):
7 text = '\n' 8 for author in auth2index: 9 email = auth2email[author] 10 email_text = '' if email is None else '(%s)' % email 11 text += ' '.join([author, str(auth2index[author]), email_text]) + '\n' 12 text += '\n' 13 for index in index2inst: 14 text += '[%d] %s\n' % (index, index2inst[index]) 15 text += '\n' 16 return text
17
18 -def plain_ref_and_label(section_label2title, format, filestr):
19 # .... see section ref{my:sec} is replaced by 20 # see the section "...section heading..." 21 pattern = r'[Ss]ection(s?)\s+ref\{' 22 replacement = r'the section\g<1> ref{' 23 filestr = re.sub(pattern, replacement, filestr) 24 pattern = r'[Cc]hapter(s?)\s+ref\{' 25 replacement = r'the chapter\g<1> ref{' 26 filestr = re.sub(pattern, replacement, filestr) 27 28 # remove label{...} from output (when only label{} on a line, remove 29 # the newline too, leave label in figure captions, and remove all the rest) 30 #filestr = re.sub(r'^label\{.+?\}\s*$', '', filestr, flags=re.MULTILINE) 31 cpattern = re.compile(r'^label\{.+?\}\s*$', flags=re.MULTILINE) 32 filestr = cpattern.sub('', filestr) 33 #filestr = re.sub(r'^(FIGURE:.+)label\{(.+?)\}', '\g<1>{\g<2>}', filestr, flags=re.MULTILINE) 34 cpattern = re.compile(r'^(FIGURE:.+)label\{(.+?)\}', flags=re.MULTILINE) 35 filestr = cpattern.sub('\g<1>{\g<2>}', filestr) 36 filestr = re.sub(r'label\{.+?\}', '', filestr) # all the remaining 37 38 # replace all references to sections: 39 for label in section_label2title: 40 filestr = filestr.replace('ref{%s}' % label, 41 '"%s"' % section_label2title[label]) 42 43 from common import ref2equations 44 filestr = ref2equations(filestr) 45 46 return filestr
47
48 -def bibdict2doconcelist(pyfile, citations):
49 """Transform dict with bibliography to a doconce ordered list.""" 50 f = open(pyfile, 'r') 51 bibstr = f.read() 52 try: 53 bibdict = eval(bibstr) 54 except: 55 print 'Error in Python dictionary for bibliography in', pyfile 56 sys.exit(1) 57 text = '\n\n======= Bibliography =======\n\n' 58 for label in citations: 59 # remove newlines in reference data: 60 text += ' o ' + ' '.join(bibdict[label].splitlines()) + '\n' 61 text += '\n\n' 62 return text
63
64 -def plain_index_bib(filestr, index, citations, bibfile):
65 for label in citations: 66 filestr = filestr.replace('cite{%s}' % label, 67 '[%d]' % citations[label]) 68 if 'py' in bibfile: 69 bibtext = bibdict2doconcelist(bibfile['py'], citations) 70 #filestr = re.sub(r'^BIBFILE:.+$', bibtext, filestr, flags=re.MULTILINE) 71 cpattern = re.compile(r'^BIBFILE:.+$', flags=re.MULTILINE) 72 filestr = cpattern.sub(bibtext, filestr) 73 74 # remove all index entries: 75 filestr = re.sub(r'idx\{.+?\}\n?', '', filestr) 76 # no index since line numbers from the .do.txt (in index dict) 77 # never correspond to the output format file 78 #filestr += '\n\n======= Index =======\n\n' 79 #for word in index: 80 # filestr + = '%s, line %s\n' % (word, ', '.join(index[word])) 81 82 return filestr
83
84 -def define(FILENAME_EXTENSION, 85 BLANKLINE, 86 INLINE_TAGS_SUBST, 87 CODE, 88 LIST, 89 ARGLIST, 90 TABLE, 91 EXERCISE, 92 FIGURE_EXT, 93 CROSS_REFS, 94 INDEX_BIB, 95 INTRO, 96 OUTRO):
97 # all arguments are dicts and accept in-place modifications (extensions) 98 99 FILENAME_EXTENSION['plain'] = '.txt' 100 BLANKLINE['plain'] = '\n' 101 # replacement patterns for substitutions of inline tags 102 INLINE_TAGS_SUBST['plain'] = { 103 'math': r'\g<begin>\g<subst>\g<end>', # drop $ signs 104 'math2': r'\g<begin>\g<puretext>\g<end>', 105 'emphasize': None, 106 'bold': None, 107 'figure': None, 108 'movie': default_movie, 109 'verbatim': r'\g<begin>\g<subst>\g<end>', # no ` chars 110 'linkURL': r'\g<begin>\g<link> (\g<url>)\g<end>', 111 'linkURL2': r'\g<link> (\g<url>)', 112 'linkURL3': r'\g<link> (\g<url>)', 113 'linkURL2v': r'\g<link> (\g<url>)', 114 'linkURL3v': r'\g<link> (\g<url>)', 115 'plainURL': r'\g<url>', 116 'title': r'======= \g<subst> =======\n', # doconce top section, to be substituted later 117 'author': plain_author, 118 'date': r'\nDate: \g<subst>\n', 119 'chapter': lambda m: r'\g<subst>\n%s' % ('%'*len(m.group('subst').decode('utf-8'))), 120 'section': lambda m: r'\g<subst>\n%s' % ('='*len(m.group('subst').decode('utf-8'))), 121 'subsection': lambda m: r'\g<subst>\n%s' % ('-'*len(m.group('subst').decode('utf-8'))), 122 'subsubsection': lambda m: r'\g<subst>\n%s' % ('~'*len(m.group('subst').decode('utf-8'))), 123 'paragraph': r'*\g<subst>* ', # extra blank 124 'abstract': r'\n*\g<type>.* \g<text>\g<rest>', 125 } 126 127 from rst import rst_code 128 CODE['plain'] = rst_code 129 from common import DEFAULT_ARGLIST 130 ARGLIST['plain'] = DEFAULT_ARGLIST 131 LIST['plain'] = { 132 'itemize': 133 {'begin': '', 'item': '*', 'end': '\n'}, 134 135 'enumerate': 136 {'begin': '', 'item': '%d.', 'end': '\n'}, 137 138 'description': 139 {'begin': '', 'item': '%s', 'end': '\n'}, 140 141 'separator': '\n', 142 } 143 CROSS_REFS['plain'] = plain_ref_and_label 144 from rst import rst_table 145 TABLE['plain'] = rst_table 146 EXERCISE['plain'] = plain_exercise 147 #TABLE['plain'] = plain_table 148 INDEX_BIB['plain'] = plain_index_bib
149 150 # no return, rely on in-place modification of dictionaries 151