summaryrefslogtreecommitdiff
path: root/genpdf.py
blob: f508c52d8543ec65a3c4578c7d6b97ac2aa50b67 (plain)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Parse HTML output to generate a PDF.

This is surprisingly adequate PDF generation for Dercuano.
Missing pieces include:

- a layout engine capable of handling varying font sizes in a line
- proper indentation for lists (<ul>, <ol>)
- tables
- JS tables of contents for individual notes
- chronological ordering
- maybe making the output file less than 12.4 megabytes?? not using
  base85 would fucking help
- colored titles
- hyphenation and justification
- properly making the first part of a link a link when it crosses pages;
  instead the first part ends up at the bottom of the next page
- URLs for external links?

It also takes over 7 minutes to run on my netbook and generates a 3764-page PDF,
so maybe some kind of output caching system would be useful.

The codepoint coverage thing may be a bit tricky.  Really we probably need to
extract the coverage information from the fonts rather than guessing at them;
this seems to provide roughly the right information:

    >>> from reportlab.pdfbase.ttfonts import TTFontFile
    >>> f
    'etbook/et-book-display-italic-old-style-figures.ttf'
    >>> itf = TTFontFile(f)
    >>> itf.charToGlyph
    {0: 1, 8192: 198, 8194: 200, 8195: 201, 8196: 202, 8197: 203,
    8198: 204, 8193: 199, 8200: 206, 8201: 207, 8202: 208, 13: 2,
    8208: 209, 8209: 210, 8210: 211, 8211: 212, 8212: 213, 8216: 214,
    ...
    >>> sorted(itf.charToGlyph.keys())
    [0, 13, 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,
    ...
    124, 125, 126, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
    ...

Also the TTFont has a .face property which inherits from TTFontFile and
thus has ``charToGlyph`` too.  And TTFont has a .stringWidth method too.

No idea how to tell what the coverage of the built-in Courier is,
though.  Maybe I should just use whatever Android uses instead of
Courier?

I'm thinking that high-level fonts will really be "font cascades", like in
CSS, and a box styled with a particular cascade will get broken up into
one or more boxes each styled with a particular low-level font.
To allow ligatures and kerning to work, we want to avoid breaking it up
into more boxes than needed.  <https://gankra.github.io/blah/text-hates-you/>
goes into details on the complexities involved and advises you to just use
HarfBuzz.

"""
from __future__ import print_function

import cgitb
from collections import OrderedDict
import errno
import os
import re
import sys
try:
    from urllib import unquote
    from urlparse import urlparse
except ImportError:
    from urllib.parse import unquote, urlparse
import xml.etree.cElementTree as ET

from reportlab.pdfgen.canvas import Canvas
import reportlab.lib.pagesizes
from reportlab.lib.colors import toColor
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.cidfonts import UnicodeCIDFont


def style_override(style, prop, val):
    rv = style.copy()
    rv[prop] = val
    return rv

class Cascade:
    """A font cascade is similar to a font (modulo point size) with fallback.

    For code points not in the most preferred font, it falls back to
    less-preferred fonts, finally using the fallback font if nothing
    else has it.  To do this, it must be able to figure out if a font
    has a given code point or not, which I don't know how to do for
    arbitrary Reportlab fonts.  But I do know how to do it for
    TrueType fonts, so those are the ones you can use in the cascade
    list.

    """
    def __init__(self, fonts, fallback):
        """Parameter ``fonts`` is a TTFont list, which have .face.charToGlyph.
        Parameter ``fallback`` can be any Reportlab font; it will be used
        peremptorily for any codepoint not found in ``fonts``.
        """
        self.fonts = fonts
        self.fallback = fallback
        self.default_postscript_font = (fonts[0] if fonts else
                                        fallback).fontName
        self.string_cache = {}
        self.char_cache = {}
        self.width_cache = {}

    def register(self):
        for font in self.fonts:
            pdfmetrics.registerFont(font)

        if self.fallback:
            pdfmetrics.registerFont(self.fallback)

    def find_fonts(self, string):
        "Map a string of chars to (char, font) pairs."
        char_cache = self.char_cache
        for c in string:
            if c in char_cache:
                yield c, char_cache[c]
                continue
            
            for f in self.fonts:
                if ord(c) in f.face.charToGlyph:
                    if len(char_cache) > 1024:
                        char_cache.clear()
                    char_cache[c] = f
                    yield c, f
                    break
            else:
                yield c, self.fallback

    def _map(self, string):
        last_font = object()
        rv = []
        for c, f in self.find_fonts(string):
            if f == last_font:
                chars.append(c)
            else:
                chars = [c]
                rv.append((f, chars))
                last_font = f

        return tuple((f, u''.join(chars)) for f, chars in rv)

    def map(self, string):
        "Map a string to a tuple of (font, substring) pairs."
        if string not in self.string_cache:
            if len(self.string_cache) > 1024:
                self.string_cache.clear()
            self.string_cache[string] = self._map(string)
        return self.string_cache[string]

    def width(self, string, size):
        "Equivalent of font.stringWidth or canvas.stringWidth."
        k = string, size
        if k not in self.width_cache:
            if len(self.width_cache) > 4096:
                self.width_cache.clear()
            self.width_cache[k] = sum(f.stringWidth(s, size)
                                      for f, s in self.map(string))
        return self.width_cache[k]

    def text_out(self, textobject, style, string):
        "Equivalent of Textobject.text_out."
        for f, s in self.map(string):
            #print(u"Setting '%s' in %s" % (s, f.fontName))
            substyle = style_override(style, 'postscript-font', f.fontName)
            textobject.text_out(substyle, s)
    

roman = 'et-book-roman'
italic = 'et-book-italic'
bold = 'et-book-bold'
lmtlc = 'lmtlc'
lmtlco = 'lmtlc-Oblique'
# see dercuano-hand-computers for the origins of these numbers
em = 12
# this is too thin: pagesize = (24 * em, 60 * em)
# This is too wide (35 em x 50 em) and doesn't give enough zoom options:
#pagesize = reportlab.lib.pagesizes.A5
# Hopefully this is a Goldilocks size:
pagesize = (29 * em, 66 * em)
left_margin = top_margin = bottom_margin = right_margin = 0.5 * em

def load_fonts(path):
    stsong = UnicodeCIDFont('STSong-Light')

    mypath = os.path.dirname(os.path.abspath(__file__))
    flmtlc = TTFont(lmtlc, mypath + '/LMMonoLtCond10-Regular.ttf')
    flmtlco = TTFont(lmtlco, mypath + '/LMMonoLtCond10-Oblique.ttf')

    freefont = mypath + '/freefont-built'
    
    free = {}
    for face in ['MonoBoldOblique', 'MonoBold', 'MonoOblique', 'Mono',
                 'SerifBoldItalic', 'SerifBold', 'SerifItalic', 'Serif']:
        free[face] = TTFont('Free' + face, freefont + '/Free' + face + '.ttf')

    dejavu = {}
    dejavupath = mypath + '/dejavu-built'
    for face in (['SansMono' + suffix
                  for suffix in ['', '-Bold', '-Oblique', '-BoldOblique']]
                 + ['Serif' + suffix
                    for suffix in ['', '-Bold', '-Italic', '-BoldItalic']]):
        dejavu[face] = TTFont('DejaVu' + face,
                              dejavupath + '/DejaVu' + face + '.ttf')

    liabilities = path + '/liabilities'
    etbookroman = TTFont(roman,
                          liabilities + '/et-book-roman-old-style-figures.ttf')
    etbookitalic = TTFont(italic,
                liabilities + '/et-book-display-italic-old-style-figures.ttf')
    etbookbold = TTFont(bold, liabilities + '/et-book-bold-line-figures.ttf')

    rv = {
        'serif': Cascade([etbookroman, free['Serif'], dejavu['Serif']], stsong),
        'serif-italic': Cascade([etbookitalic, free['SerifItalic'],
                                 dejavu['Serif-Italic']], stsong),
        'serif-bold': Cascade([etbookbold, free['SerifBold'],
                               dejavu['Serif-Bold']], stsong),
        'serif-bold-italic': Cascade([free['SerifBoldItalic'],
                                      dejavu['Serif-BoldItalic']], stsong),
        'fixed': Cascade([flmtlc, free['Mono'], dejavu['SansMono']], stsong),
        'fixed-oblique': Cascade([flmtlco, free['MonoOblique'],
                                  dejavu['SansMono-Oblique']], stsong),
        'fixed-bold': Cascade([free['MonoBold'], dejavu['SansMono-Bold']],
                              stsong),
        'fixed-bold-oblique': Cascade([free['MonoBoldOblique'],
                                       dejavu['SansMono-BoldOblique']], stsong),
    }

    for cascade in rv:
        rv[cascade].register()

    return rv


class Textobject:
    def __init__(self, canvas, x, y, style):
        self.c = canvas
        self.x = x
        self.y = y
        self.font = style['postscript-font'], style['font-size']

    def start_page(self, style):
        self.font = style['postscript-font'], style['font-size']
        self.c.setFont(*self.font)
        self.c.setFillColor(toColor(style['color']))
        self.t = self.c.beginText(self.x, self.y)
        self.tfont = self.font
        self.color = style['color']
        self.drawn_anything = False

    def newline(self, style, extra_skip):
        self.t.textLine()
        y = self.t.getY() - extra_skip
        if y < bottom_margin:
            self.end_page()
            self.start_page(style)
        else:
            # XXX starting so many new texts and setting so many new
            # fonts is adding over a megabyte to the PDF file: 12.37
            # MB vs. 13.44 MB
            self.flush()
            self.font = style['postscript-font'], style['font-size']
            self.c.setFillColor(toColor(style['color']))
            self.c.setFont(*self.font)
            self.t = self.c.beginText(self.x, y)
            self.tfont = self.font
            self.color = style['color']
            self.drawn_anything = False

    def flush(self):
        if self.drawn_anything:
            self.c.drawText(self.t)
        del self.t, self.tfont, self.drawn_anything, self.color

    def end_page(self):
        self.flush()
        self.c.showPage()

    def text_out(self, style, text):
        self.font = style['postscript-font'], style['font-size']
        if self.tfont != self.font:
            self.t.setFont(*self.font)
            self.tfont = self.font

        if self.color != style['color']:
            self.t.setFillColor(toColor(style['color']))
            self.color = style['color']

        self.t.textOut(text)
        self.drawn_anything = True

    def get_x(self):
        return self.t.getX()

    def get_y(self):
        return self.t.getY()

    def set_rise(self, rise):
        return self.t.setRise(rise)

def resolve_link(corpus, url):
    while url.startswith('../'):
        url = url[3:]
    return 'bookmark' if url in corpus else 'URL', url

def add_link(c, box, link):
    if not link:
        return
    link_type, link_value = link
    if link_type == 'bookmark':
        # Though other viewers ignore it, pdf.js displays the first
        # argument in a popup.  Maybe look for @title?
        c.linkRect(link_value, link_value, box, thickness=0.1,
                       color=toColor('#9999ff'))
    else:
        c.linkURL(link_value, box, thickness=0.1, color=toColor('#ccccff'))

def chop(cascade, font_size, string, max_width):
    """Cut `string` into a left half that fits within max_width,
    and a possibly empty right half that didn't."""
    # Half-open interval for cut point: a:b.  len(string) is valid
    # (and most common!) cut point, thus b is initially len(string)+1.
    a, b = 0, len(string)+1
    while a < b - 1:
        c = a + (b - a) // 2
        if cascade.width(string[:c], font_size) <= max_width:
            a = c
        else:
            b = c

    return string[:a], string[a:]

def text_out(fonts, t, style, s):
    fonts[style['font-family']].text_out(t, style, s)

# Abbreviations not to put an extra space after
abbrevs = {'Mr.', 'vs.', 'cf.', 'Jr.', 'pp.', 'Ms.', 'Dr.', 'p.', 'St.', '(St.'}
spacepunct = tuple('.?!:;')

def start_box(x, y, font_size):
    return [x - font_size * 0.1, y - font_size * 0.35, x, y + font_size * 0.85]

def render_text(c, t, text, style, fonts, abbrevs=abbrevs, spacepunct=spacepunct):
    max_x = pagesize[0] - right_margin
    pre = style['white-space'] == 'pre'
    words = (re.split('([^\n]+\n)', text) if pre else
             re.split('[ \n\r\t]+', text))
    x, y = t.get_x(), t.get_y()
    font_family = style['font-family']
    font = fonts[font_family]
    font_size = style['font-size']
    box = start_box(x, y, font_size)
    newline_style = style_override(style, 'postscript-font',
                                   font.default_postscript_font)
    for wi, word in enumerate(words):
        ends_in_newline = word.endswith('\n')   # for <pre>
        if ends_in_newline:
            word = word[:-1]

        width = font.width(word, font_size)
        x = t.get_x()
        if x + width > max_x:
            t.newline(newline_style, 0)
            add_link(c, box, style['link destination'])
            x, y = t.get_x(), t.get_y()
            box = start_box(x, y, font_size)

        # chop up words too wide for lines
        while word and x + width > max_x:
            left, right = chop(font, font_size, word, max_x - x)
            text_out(fonts, t, style, left)
            x = t.get_x()
            box[2] = x + font_size * 0.1
            add_link(c, box, style['link destination'])
            # Add circles to indicate a broken line
            c.circle(x + font_size * .2, y + font_size * .3, font_size/6)

            t.newline(newline_style, 0)
            x, y = t.get_x(), t.get_y()
            c.circle(x - font_size * .2, y + font_size * .3, font_size/6)
            box = start_box(x, y, font_size)
            word = right
            width = font.width(word, font_size)

        # The last word in a string shouldn't have a space after it.
        # (re.split will give us an empty last word if the string ends
        # in whitespace.)  I tried emitting the space as a separate
        # textOut in order to share the caches across the width and
        # text_out operations, but that made the program slower, not
        # faster, presumably because of the larger number of textOut
        # operations.
        if wi < len(words) - 1 and not pre:
            word = word + ('  ' if (word[-1:] in spacepunct
                                    and word not in abbrevs) else ' ')

        text_out(fonts, t, style, word)
        if ends_in_newline:
            t.newline(newline_style, 0)

        x = t.get_x()
        box[2] = x + font_size * 0.1

    add_link(c, box, style['link destination'])

block_fonts = {
    'p': (None, None),
    'h1': ('serif', 2*em),
    'h2': ('serif', 1.59*em),
    'h3': ('serif', 1.26*em),
    'h4': ('serif-bold', 1.1*em),
    'h5': ('serif-bold', 1*em),
    'h6': ('serif-bold-italic', 1*em),
    'li': (None, None),
    'div': (None, None),
    # With Latin Modern Mono Light Condensed, I need just over 28 ems for 80
    # columns, so font-size:98% seems about right.
    'pre': ('fixed', 0.98*em),
    'br': (None, None),
    # Actual table support would be a lot better obviously but until then:
    'tr': (None, None),
    'blockquote': (None, 0.8409*em),  # sqrt(sqrt(2))
    'ul': (None, None),
    'ol': (None, None),
    }

extra_padding_ems_above = dict(h2=0.5, h3=0.5, h4=0.5, h5=0.5, h6=0.5, pre=1,
                               blockquote=0.5)

block_colors = {
    'h1': 'rgba(136,0,0,.794)',
    'h2': 'rgba(136,0,0,.794)',
    'h3': 'rgba(136,0,0,.794)',
    'h4': 'rgba(136,0,0,.794)',
    'h5': 'rgba(136,0,0,.794)',
    'h6': 'rgba(136,0,0,.794)',
}

italicize_table = {
    'serif': 'serif-italic',
    'serif-italic': 'serif-italic',
    'serif-bold': 'serif-bold-italic',
    'serif-bold-italic': 'serif-bold-italic',
    'fixed': 'fixed-oblique',
    'fixed-oblique': 'fixed-oblique',
    'fixed-bold': 'fixed-bold-oblique',
    'fixed-bold-oblique': 'fixed-bold-oblique',
}

def italicize(font):
    return italicize_table[font[0]], font[1]

codify_table = {
    'serif': 'fixed',
    'serif-italic': 'fixed-oblique',
    'serif-bold': 'fixed-bold',
    'serif-bold-italic': 'fixed-bold-oblique',
    'fixed': 'fixed',
    'fixed-oblique': 'fixed-oblique',
    'fixed-bold': 'fixed-bold',
    'fixed-bold-oblique': 'fixed-bold-oblique',
}

def codify(font):
    return codify_table[font[0]], font[1]

embolden_table = {
    'serif': 'serif-bold',
    'serif-italic': 'serif-bold-italic',
    'serif-bold': 'serif-bold',
    'serif-bold-italic': 'serif-bold-italic',
    'fixed': 'fixed',
    'fixed-oblique': 'fixed-bold-oblique',
    'fixed-bold': 'fixed-bold',
    'fixed-bold-oblique': 'fixed-bold-oblique',
}

def embolden(font):
    return embolden_table[font[0]], font[1]

inline_fonts = {'i': italicize,
                'em': italicize,
                'code': codify,
                'b': embolden,
                'strong': embolden,
                }

def get_link(node):
    url = node.get('href') if node.tag == 'a' else None
    return None if url is None else unquote(url)

def push_style(stack, current_style, prop, value):
    stack.append(('restore', (prop, current_style[prop])))
    current_style[prop] = value

def render(pagenos, corpus, bookmark, c, xml, fonts):
    print("PDFing", bookmark)
    c.bookmarkPage(bookmark, fit='XYZ')  # `fit` to suppress zooming out to whole page
    pagenos[bookmark] = c.getPageNumber()
    title = bookmark
    current_style = {
        'font-family': 'serif',
        'font-size': 1*em,
        'link destination': None,
        'white-space': 'normal',
        'color': 'black',
    }

    start_page_style = style_override(current_style, 'postscript-font',
        fonts[current_style['font-family']].default_postscript_font)
    t = Textobject(c, left_margin, pagesize[1]-top_margin-em, start_page_style)
    t.start_page(start_page_style)
    stack = [('element', xml)]
    top_of_block = True
    while stack:
        kind, obj = stack.pop()
        if kind == 'element':
            if obj.tail is not None:
                stack.append(('text', obj.tail))

            # Ignore whitespace so newlines don't clear top_of_block:
            if obj.text and not obj.text.strip():
                obj.text = None

            if obj.tag in block_fonts:
                font_family, font_size = block_fonts[obj.tag]

                size_override = re.search(r'font-size:\s*([\d.]+)em',
                                          obj.get('style', ''))
                if size_override:
                    font_size = (current_style['font-size']
                                 * float(size_override.group(1)))

                if not top_of_block:
                    new_font_size = (font_size if font_size is not None else
                                     current_style['font-size'])
                    newline_style = style_override(current_style,
                                                   'postscript-font',
                        fonts[current_style['font-family']]
                            .default_postscript_font)
                    skip = ( new_font_size - 1*em
                           + new_font_size * extra_padding_ems_above.get(obj.tag, 0)
                           )
                    t.newline(newline_style, extra_skip=skip)

                if font_family is not None:  # inherit
                    push_style(stack, current_style, 'font-family', font_family)
                if font_size is not None:    # inherit
                    push_style(stack, current_style, 'font-size', font_size)
                if obj.tag in block_colors:
                    push_style(stack, current_style, 'color',
                               block_colors[obj.tag])
                was_top_of_block = top_of_block
                top_of_block = (obj.tag in ('li', 'blockquote'))
            else:
                top_of_block = False

            if obj.tag == 'p':
                # paragraph indent
                if not was_top_of_block:
                    text_out(fonts, t, current_style, ' ' * 4)
            elif obj.tag == 'li':
                text_out(fonts, t, current_style, u'• ')
            elif obj.tag == 'pre':
                push_style(stack, current_style, 'white-space', 'pre')

            if obj.tag in inline_fonts:
                # XXX maybe refactor how these are specced
                font = inline_fonts[obj.tag]((current_style['font-family'],
                                              current_style['font-size']))
                push_style(stack, current_style, 'font-family', font[0])
                push_style(stack, current_style, 'font-size', font[1])

            if obj.tag in ('sup', 'sub'):
                font_size = current_style['font-size']
                prev_rise = t._rise if hasattr(t, '_rise') else 0
                # XXX this `prev_rise +` doesn't seem to be working
                rise = prev_rise + font_size * (+0.5 if obj.tag == 'sup' else
                                                -0.25)
                # XXX this is inappropriate if we end in a different line
                stack.append(('clear_rise', (-rise - prev_rise, prev_rise)))
                t.set_rise(rise)
                push_style(stack, current_style, 'font-size',
                           max(0.2*em, 0.7071 * font_size))

            if obj.tag == 'title':
                title = re.compile(r'\s*Dercuano\s*$').sub('', obj.text).strip()
            if get_link(obj):
                link_dest = resolve_link(corpus, get_link(obj))
                push_style(stack, current_style, 'link destination', link_dest)

                if link_dest[0] == 'bookmark':
                    stack.append(('text',
                            ' (p. %s)' % pagenos.get(link_dest[1], '???')))

            if obj.text is not None and obj.tag not in ('title', 'script', 'style'):
                render_text(c, t, obj.text, current_style, fonts)
                top_of_block = False

            for kid in reversed(list(obj)):
                stack.append(('element', kid))

        elif kind == 'text':
            render_text(c, t, obj, current_style, fonts)
            top_of_block = False
        elif kind == 'clear_rise':
            # workaround for a bug in Reportlab (?) where y coord got horked
            t.set_rise(obj[0])
            t.set_rise(obj[1])
        else:
            assert kind == 'restore'
            prop, val = obj
            current_style[prop] = val
            top_of_block = False

    t.end_page()
    c.addOutlineEntry(title, bookmark, level=0)

def read_plaintext(bookmarkname, filename):
    root = ET.XML('<html><title></title>'
                 + '<body><pre></pre></body>'
                 + '</html>')
    root[0].text = bookmarkname
    body = root[1]
    assert body.tag == 'body'
    for paragraph in open(filename).read().decode('utf-8').split('\n\n'):
        ptag = ET.Element('p')
        ptag.text = paragraph
        body.append(ptag)

    return root

def descendant_nodes(etnode):
    "ElementTree nodes excluding the node itself.  Probably ET has a method for this..."
    for kid in etnode:
        yield kid
        for descendant in descendant_nodes(kid):
            yield descendant

def unique_ahref_urls(etnode):
    "Delivers URLs in document order."
    seen = set()
    for node in descendant_nodes(etnode):
        href = node.get('href')
        if node.tag == 'a' and href:
            url = unquote(href)
            seen.add(url)
            yield url

def parse_html(filename):
    try:
        tree = ET.parse(filename)
        return tree.getroot()
    except Exception:
        print("parse error on", filename + ":", sys.exc_info()[1])
        try:
            # Although the above chews through all of Dercuano in 1.3
            # seconds on this netbook, it fails to parse 3% of the
            # notes because they have things like raw HTML blocks in
            # them, which Python Markdown doesn't XMLify (e.g., ``<tr>
            # <td>1 <td>0.4%``.)  So we preprocess everything
            # with HTML Tidy.  sgmllib and htmllib are
            # removed in Python 3, and HTMLParser (html.parser) is a
            # tag-soup parser.
            import tidylib
            xml = tidylib.tidy_document(open(filename).read(),
                                        {'input-encoding': 'utf8',
                                         'output-encoding': 'utf8',
                                         'numeric-entities': True})[0]
            root = ET.fromstring(xml)
            # remove XML namespace prefixes:
            deprefixnodes = [root]
            while deprefixnodes:
                node = deprefixnodes.pop()
                deprefixnodes.extend(list(node))
                node.tag = re.compile('{.*}').sub('', node.tag)
            print("recovered using tidylib")
            return root
        except Exception:
            print("tidylib failed too:", sys.exc_info()[1])
            return ET.fromstring('<html>XML parse failure</html>')


class Pagenos:
    def __init__(self, filename):
        self.filename = filename
        self.pagenos = {}

    def load(self):
        try:
            f = open(self.filename)
        except IOError as e:
            if e.errno == errno.ENOENT:
                return
            raise

        with f:
            for line in f:
                pageno, bookmark = line.split()
                self.pagenos[bookmark] = int(pageno)

    def save(self):
        with open(self.filename + '.tmp', 'w') as f:
            for bookmark, pageno in self.pagenos.items():
                f.write('%d %s\n' % (pageno, bookmark))
            f.flush()
            os.fsync(f.fileno())

        os.rename(self.filename + '.tmp', self.filename)

    def get(self, bookmark, default):
        return self.pagenos.get(bookmark, default)

    def __setitem__(self, bookmark, pageno):
        assert len(bookmark.split()) == 1
        self.pagenos[bookmark] = pageno


def main(path):
    fast = False
    fonts = load_fonts(path)

    pdf_filename = 'dercuano.tmp.fast.pdf' if fast else 'dercuano.tmp.pdf'
    canvas = Canvas(pdf_filename, invariant=True, pageCompression=True,
                    pagesize=pagesize)
    # Avoid horking our page number database with fast debugging runs
    pagenos = (Pagenos('dercuano.fast.pagenos') if fast else
               Pagenos('dercuano.tmp.pagenos'))
    pagenos.load()

    # pdf.js, gv, MuPDF Mini, and Evince display this:
    canvas.setTitle('Dercuano ' + os.path.basename(path)
                    + ', by Kragen Javier Sitaker, 2019')
    canvas.setAuthor('Kragen Javier Sitaker')

    index_filename = path + '/index.html'
    corpus = OrderedDict()
    corpus['index.html'] = index_filename
    plaintexts = set()

    index_node = parse_html(index_filename)
    for i, relative_url in enumerate(unique_ahref_urls(index_node)):
        if urlparse(relative_url).path.startswith('/'):
            print('not a relative URL', relative_url)
            # Thaaat's not a *relative* URL!
            continue

        if not fast or i % 20 == 1:
            corpus[relative_url] = path + '/' + relative_url
            if not relative_url.endswith('.html'):
                plaintexts.add(relative_url)

    for i, bookmarkname in enumerate(corpus):
        sys.stdout.write('%d/%d ' % (i, len(corpus)))
        filename = corpus[bookmarkname]

        if bookmarkname in plaintexts:
            root = read_plaintext(bookmarkname, filename)
        else:
            root = parse_html(filename)

        render(pagenos, corpus, bookmarkname, canvas, root, fonts)

    pagenos.save()
    canvas.save()

if __name__ == '__main__':
    cgitb.enable(format='text')
    main(sys.argv[1])