#! /usr/bin/env python3 import re block1_re = re.compile(r'{\\(?P[\w*]+) (?P[^{}]+)}') block2_re = re.compile(r'\\(?P[\w*]+)(\[[^]]+\])?{(?P[^{}]*)}') chapauth = None chapimg = None display = True pfx = '' def texsub(m): global pfx, display cmd = m.group('cmd') txt = m.group('txt') if cmd == 'em': return '%s' % txt elif cmd == 'bf': return '%s' % txt elif cmd == 'sf': return '%s' % txt elif cmd == 'sc': return '%s' % txt elif cmd == 'rm': return '%s' % txt elif cmd == 'url': return '%s' % (txt, txt) elif cmd == 'begin': if txt in ('center',): return elif txt in ('quotation', 'quote'): pfx = '> ' elif txt == 'textblock': display = False else: print(cmd, txt) raise TypeError(cmd) elif cmd == 'end': if txt == 'textblock': display = True else: pfx = '' return '' elif cmd in ('include', 'chapter', 'chapimg', 'chapauth', 'illustration', 'scriptsize', 'section*', 'part'): return '#%s %s' % (cmd, txt) elif cmd in ('pagenumbering', 'includegraphics', 'newcommand', 'hbox'): return '' elif cmd in ('TeX', 'LaTeX'): return cmd else: print(cmd, txt) raise TypeError(cmd) decor_stack = [] decor_re = re.compile(r'({\\(?P\w\w) ?|})') def decorsub(m): cmd = m.group('cmd') if not cmd: if not decor_stack: return m.group(0) cmd = decor_stack.pop() return '' % cmd else: if cmd == 'bf': cmd = 'strong' elif cmd == 'em': pass elif cmd == 'sc': decor_stack.append('span') return '' else: raise TypeError(cmd) decor_stack.append(cmd) return '<%s>' % cmd def art(artist, url, title=None): alt = title or ("Artwork by %s" % artist) print('
') print('%s' % (url, alt)) if title: atxt = '%s by %s' % (title, artist) else: atxt = alt print('

%s

' % (atxt)) print('
') outbuf = '' def outline(l): global chapimg, chapauth, outbuf l = l.strip() if not l: print(outbuf) elif l[0] == '%': return l = l.replace(r'\'e', 'é') l = l.replace(r'\,c', 'ç') l = l.replace(r'\'n', 'ń') l = l.replace("''", '”') l = l.replace("``", '“') l = l.replace("'", '’') l = l.replace("`", '‘') l = l.replace('---', '—') l = l.replace('--', '–') l = l.replace('\\\\', '
') l = l.replace('\_', '_') l = l.replace('\#', '#') l = l.replace('\$', '$') l = l.replace('\ ', ' ') l = l.replace(r'\-', '') l = l.replace(r'\~n', 'ñ') l = l.replace(r'{\ldots}', '…') l = l.replace(r'\ldots', '…') l = l.replace(r'\copyright', '©') l = block1_re.sub(texsub, l) l = block2_re.sub(texsub, l) l = l.replace('{}', '') l = decor_re.sub(decorsub, l) if not l: return if l[0] == '#': if l.startswith('#include'): include(l[9:] + '.tex') elif l.startswith('#chapimg'): chapimg = l[9:-1].split('{') elif l.startswith('#chapauth'): chapauth = l[10:] elif l.startswith('#chapter'): print('

%s

' % l[9:]) if chapauth: print('

by %s

' % chapauth) chapauth = None if chapimg: art(chapimg[0], chapimg[1]) chapimg = None elif l.startswith('#part'): print('

%s

' % l[6:]) elif l.startswith('#illustration'): artist, title, url = l[14:].split('{') title = title[:-1] url = url[:-1] art(artist, url, title) elif l.startswith('#section*'): print('

%s

' % l[10:]) else: print('<--! %s -->' % l) elif l[0] == '\\': what = l[1:5].lower() if what in ('bigs', 'vfil'): print('
') elif what == 'newp': print('
') elif what == 'noin': print(l[10:]) elif what in ('hbox', 'inde', 'tabl', 'appe', 'page', 'list'): pass elif l[1:9] == 'maketitl': print('

Horrors 2

') print('

The Something Awful Forums

') else: print('================= %r' % what) elif display: print('%s%s' % (pfx, l)) def include(fn): if fn == 'praise.tex': return f = open(fn) for l in f: outline(l) f = open('horrors2.ltx') # skip LaTeX crap for l in f: if l.startswith('\\begin{document'): break print(''' Horrors 2 ''') for l in f: outline(l) print(''' ''')