summaryrefslogtreecommitdiff
path: root/sandbox/mwh/advopostwriter.py
blob: e2f98be0d3357cc22a4054a6ce37204d6f2b4962 (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
from docutils.writers import html4css1
from docutils import nodes

class Writer(html4css1.Writer):
    W_init = html4css1.Writer.__init__
    def __init__(self):
        self.W_init()
        self.translator_class = MyHTMLTranslator

def find_visible_child(node, starting_index, direction):
    if direction == "forwards":
        step = 1
        end = len(node)
    else:
        step = -1
        end = -1
    for i in range(starting_index, end, step):
        if not isinstance(node[i], nodes.Invisible):
            return node[i]
    else:
        return None

class MyHTMLTranslator(html4css1.HTMLTranslator):
    def visit_title(self, node):
        self.body.append(self.starttag(node, 'b', '',
                                       STYLE="font-variant: small-caps"))
        self.context.append('</b>\n')
    def visit_document(self, node):
        pass
    def depart_document(self, node):
        pass
    def visit_paragraph(self, node):
        if isinstance(node.parent, nodes.section):
            index = node.parent.index(node)
            prev = find_visible_child(node.parent, index - 1, "backwards")
            if isinstance(prev, nodes.title):
                self.body.append(self.starttag(node, 'blockquote', '\n'))
            else:
                self.body.append('<p>\n')
        else:
            html4css1.HTMLTranslator.visit_paragraph(self, node)

    def depart_paragraph(self, node):
        if isinstance(node.parent, nodes.section):
            index = node.parent.index(node)
            next = find_visible_child(node.parent, index + 1, "forwards")
            if not next:
                self.body.append('\n</blockquote>\n')
            else:
                self.body.append("\n");
        else:
            # this tends to include "</p>"s which advogato then
            # discards.  I don't care that much...
            html4css1.HTMLTranslator.depart_paragraph(self, node)
        
    def visit_section(self, node):
        pass
    def depart_section(self, node):
        pass