GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Reader/hebrewreader.py at master · HebrewTools/Reader Expand file tree/ Copy pathhebrewreader.pyMore file actions executable file ·343 lines (283 loc) · 12.3 KB / Copy pathhebrewreader.pyFile metadata and controlsexecutable file ·343 lines (283 loc) · 12.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 #!/usr/bin/env python3 from argparse import ArgumentParser, FileType, RawTextHelpFormatter import os import re from shutil import copyfile import subprocess import sys import tempfile import textwrap from tf.fabric import Fabric from common import DATADIR, FEATURES, load_verse_nodes, verses_in_passage, load_data import minitf PASSAGE_RGX = ( r'^(?P<book>(?:\d )?[a-zA-Z ]+) ' r'(?P<startchap>\d+)(?::(?P<startverse>\d+))?' r'(?:-(?P<endref>(?P<endchap>\d+)(?::(?P<endverse>\d+))?|(?:book)?end))?$' ) def parse_passage(passage): verse_nodes = load_verse_nodes() match = re.match(PASSAGE_RGX, passage) if match is None: match = {'book': passage, 'startchap': 1, 'startverse': 1, 'endchap': None, 'endverse': None, 'endref': 'bookend'} else: match = match.groupdict() match['book'] = match['book'].replace(' ', '_') match['startchap'] = int(match['startchap']) if match['startverse'] is not None: match['startverse'] = int(match['startverse']) if match['endchap'] is not None: match['endchap'] = int(match['endchap']) if match['endverse'] is not None: match['endverse'] = int(match['endverse']) try: if match['endchap'] is None: if match['startverse'] is None and match['endref'] is None: match['endref'] = 'end' else: match['endchap'] = match['startchap'] match['endverse'] = match['startverse'] elif match['endverse'] is None: match['endverse'] = len(verse_nodes[match['book']][match['endchap']]) if match['startverse'] is None: match['startverse'] = 1 if match['endref'] == 'end': match['endchap'] = match['startchap'] match['endverse'] = len(verse_nodes[match['book']][match['endchap']]) elif match['endref'] == 'bookend': match['endchap'] = len(verse_nodes[match['book']]) match['endverse'] = len(verse_nodes[match['book']][match['endchap']]) except: raise ValueError('Could not find reference "{}"'.format(passage)) match.pop('endref') return match def fix_trailer(trailer, templates): return trailer\ .replace('\n', '')\ .replace('\u05e1', templates['setuma'])\ .replace('\u05e4', templates['petucha']) def fix_gloss(gloss, templates): if gloss == 'i': return 'I' return re.sub(r'<(.*)>', templates['meta_gloss'], gloss) def get_passage_and_words(passage, api, templates, separate_chapters=True, verse_nos=True): verse_nodes = load_verse_nodes() text = [] words = set() last_chapter = None for verse in verses_in_passage(passage): if verse[1] != last_chapter: last_chapter = verse[1] if separate_chapters: text.append('\n') node = verse_nodes[verse[0]][verse[1]][verse[2]] wordnodes = api.L.d(node, otype='word') thistext = '' if verse_nos: if verse[2] == 1: thistext += templates['chapno'] % verse[1] thistext += templates['verseno'] % verse[2] + ' ' thiswords = [] for word in wordnodes: thiswords.append( api.F.g_word_utf8.v(word) + fix_trailer(api.F.trailer_utf8.v(word), templates)) lex = api.L.u(word, otype='lex')[0] words.add((api.F.lex_utf8.v(word), api.F.voc_lex_utf8.v(lex), fix_gloss(api.F.gloss.v(lex), templates))) thistext += ''.join(thiswords) text.append(thistext) return text, sorted(words) def generate_txt(passages, include_voca, combine_voca, txt): voca = set() templates = { 'chapno': '%d:', 'verseno': '%d', 'setuma': 'ס', 'petucha': 'פ', 'meta_gloss': r'<\1>', } first = True for passage_text in passages: passage = parse_passage(passage_text) if not first: txt.write('\n\n') first = False try: api = load_data(passage) text, words = get_passage_and_words(passage, api, templates) except: raise ValueError('Could not find reference "{}"'.format(passage_text)) passage_pretty = '{} {}:{} - {}:{}'.format( passage['book'].replace('_', ' '), passage['startchap'], passage['startverse'], passage['endchap'], passage['endverse']) txt.write(passage_pretty + '\n'.join(text)) if not include_voca: continue if combine_voca: voca.update(words) else: txt.write('\n\n' + '\n'.join('%s: %s' % (lex,gloss) for _, lex, gloss in words)) if include_voca and combine_voca: txt.write('\n\n' + '\n'.join('%s: %s' % (lex,gloss) for _, lex, gloss in sorted(voca))) if txt != sys.stdout: txt.close() return txt.name def generate_tex(passages, include_voca, combine_voca, clearpage_before_voca, large_text, larger_text, tex, templates): tex.write(templates['pre']) if large_text: tex.write('\\largetexttrue\n') if larger_text: tex.write('\\largertexttrue\n') voca = set() text_templates = { 'chapno': r'\rdrchap{%d}', 'verseno': r'\rdrverse{%d}', 'setuma': r'\setuma{}', 'petucha': r'\petucha{}', 'meta_gloss': r'\\textit{\1}', } for passage_text in passages: passage = parse_passage(passage_text) passage_pretty = '{} {}:{} - {}:{}'.format( passage['book'].replace('_', ' '), passage['startchap'], passage['startverse'], passage['endchap'], passage['endverse']) tex.write(r'\def\thepassage{%s}' % passage_pretty) try: api = load_data(passage) text, words = get_passage_and_words(passage, api, text_templates) except: raise ValueError('Could not find reference "{}"'.format(passage_text)) tex.write('\n\n' + templates['pretext']) tex.write('\n'.join(text)) tex.write('\n' + templates['posttext']) if not include_voca: continue if combine_voca: voca.update(words) else: if clearpage_before_voca: tex.write('\n\n\\clearpage') tex.write('\n\n' + templates['prevoca']) tex.write('\\\\\n'.join(r'{\hebrewfont\vocafontsize\RL{%s}} \begin{english}%s\end{english}' % (lex,gloss) for _, lex, gloss in words)) tex.write('\n' + templates['postvoca']) if include_voca and combine_voca: if clearpage_before_voca: tex.write('\n\n\\clearpage') tex.write('\n\n' + templates['prevoca']) tex.write('\\\\\n'.join(r'{\hebrewfont\RL{%s}} \begin{english}%s\end{english}' % (lex,gloss) for _, lex, gloss in sorted(voca))) tex.write('\n' + templates['postvoca']) tex.write(templates['post']) tex.close() return tex.name def generate_pdf(passages, include_voca, combine_voca, clearpage_before_voca, large_text, larger_text, tex, pdf, templates, quiet=False): tex = generate_tex(passages, include_voca, combine_voca, clearpage_before_voca, large_text, larger_text, tex, templates) path, filename = os.path.split(pdf) jobname, _ = os.path.splitext(filename) cmd = ['xelatex'] if path != '': cmd.append('-output-directory') cmd.append(path) cmd.append('-jobname') cmd.append(jobname) cmd.append(tex) if quiet: null = open(os.devnull, 'wb') subprocess.call(cmd, stdout=null, stderr=null) else: subprocess.run(cmd) return tex, pdf def main(): parser = ArgumentParser( description='LaTeX reader generator for Biblical Hebrew', formatter_class=RawTextHelpFormatter) p_tex = parser.add_argument_group('TeX template options') p_tex.add_argument('--pre-tex', type=FileType('r', encoding='utf-8'), metavar='FILE', default=open('pre.tex', encoding='utf-8'), help='TeX file to prepend to output') p_tex.add_argument('--post-tex', type=FileType('r', encoding='utf-8'), metavar='FILE', default=open('post.tex', encoding='utf-8'), help='TeX file to append to output') p_tex.add_argument('--pre-text-tex', type=FileType('r', encoding='utf-8'), metavar='FILE', default=open('pretext.tex', encoding='utf-8'), help='TeX file to prepend to texts') p_tex.add_argument('--post-text-tex', type=FileType('r', encoding='utf-8'), metavar='FILE', default=open('posttext.tex', encoding='utf-8'), help='TeX file to append to texts') p_tex.add_argument('--pre-voca-tex', type=FileType('r', encoding='utf-8'), metavar='FILE', default=open('prevoca.tex', encoding='utf-8'), help='TeX file to prepend to word list') p_tex.add_argument('--post-voca-tex', type=FileType('r', encoding='utf-8'), metavar='FILE', default=open('postvoca.tex', encoding='utf-8'), help='TeX file to append to word list') p_output = parser.add_argument_group('Output options') p_output.add_argument('--txt', type=FileType('w', encoding='utf-8'), metavar='FILE', help='File to write plain text output to') p_output.add_argument('--tex', type=FileType('w', encoding='utf-8'), metavar='FILE', help='File to write XeLaTeX output to') p_output.add_argument('--pdf', metavar='FILE', help='The output PDF file') p_misc = parser.add_argument_group('Miscellaneous options') p_misc.add_argument('--large-text', dest='large_text', action='store_true', help='Use large font and more line spacing for text') p_misc.add_argument('--exclude-voca', dest='include_voca', action='store_false', help='Do not generate any vocabulary lists') p_misc.add_argument('--combine-voca', action='store_true', help='Use one vocabulary list for all passages') p_misc.add_argument('--clearpage-before-voca', action='store_true', help='Start a new page before vocabulary lists') parser.add_argument('passages', metavar='PASSAGE', nargs='*', help=textwrap.dedent('''\ The passages to include. Examples of correct input are:\n - Psalms 1 - Exodus 3:15 - Genesis 1-2:3 - Genesis 2:4-11 (NB: the 11 is the chapter!) - 1 Kings 17:7-end - Job 38:1-bookend''')) args = parser.parse_args() if args.pdf is None and args.txt is None and args.tex is None: print('At least one of --txt, --tex, or --pdf must be given.') sys.exit(1) if args.tex is None and args.pdf is not None: tex = tempfile.mkstemp(suffix='.tex', prefix='reader') args.tex = open(tex[1], 'w', encoding='utf-8') print('Loading data...') load_verse_nodes() if len(args.passages) == 0: print('No passages given, not doing anything') return print('Generating reader...') try: if args.txt is not None: file = generate_txt(args.passages, args.include_voca, args.combine_voca, args.txt) if file == '<stdout>': print() else: print('Plain text written to', file) templates = {} if args.tex is not None: templates['pre'] = args.pre_tex.read() templates['post'] = args.post_tex.read() templates['pretext'] = args.pre_text_tex.read() templates['posttext'] = args.post_text_tex.read() templates['prevoca'] = args.pre_voca_tex.read() templates['postvoca'] = args.post_voca_tex.read() if args.pdf is not None: tex, pdf = generate_pdf(args.passages, args.include_voca, args.combine_voca, args.clearpage_before_voca, args.large_text, False, args.tex, args.pdf, templates) print('XeLaTeX written to', tex) print('PDF written to', pdf) elif args.tex is not None: tex = generate_tex(args.passages, args.include_voca, args.combine_voca, args.clearpage_before_voca, args.large_text, False, args.tex, templates) print('XeLaTeX written to', tex) except Exception as e: print(e) sys.exit(1) if __name__ == '__main__': main() |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |