summaryrefslogtreecommitdiff
path: root/sphinx/writers/websupport.py
blob: 6d255e64c6316afa7bcdaafa5dc24cae7b5c65e4 (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
# -*- coding: utf-8 -*-
"""
    sphinx.writers.websupport
    ~~~~~~~~~~~~~~~~~~~~~~~~~

    docutils writers handling Sphinx' custom nodes.

    :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

from sphinx.writers.html import HTMLTranslator
from sphinx.websupport.document import Document

class WebSupportTranslator(HTMLTranslator):
    """
    Our custom HTML translator.
    """
    commentable_nodes = ['bullet_list', 'paragraph', 'desc']

    def __init__(self, builder, *args, **kwargs):
        HTMLTranslator.__init__(self, builder, *args, **kwargs)
        self.init_support()

    def init_support(self):
        self.support_document = Document()
        self.in_commentable = False
        self.current_id = 0
        
    def dispatch_visit(self, node):
        if node.__class__.__name__ in self.commentable_nodes:
            self.handle_visit_commentable(node)
        HTMLTranslator.dispatch_visit(self, node)

    def dispatch_departure(self, node):
        HTMLTranslator.dispatch_departure(self, node)
        if node.__class__.__name__ in self.commentable_nodes:
            self.handle_depart_commentable(node)

    def handle_visit_commentable(self, node):
        # If we are already recording a commentable slice we don't do
        # anything. We can't handle nesting.
        if not self.in_commentable:
            self.support_document.add_slice(''.join(self.body))
            node.commented = self.in_commentable = True
            self.body = []
        else:
            node.commented = False

    def handle_depart_commentable(self, node):
        assert(self.in_commentable)
        if node.commented:
            slice_id = '%s-%s' % (self.builder.docname, self.current_id)
            self.current_id += 1

            body = ''.join(self.body)
            self.support_document.add_slice(body, slice_id, commentable=True)

            self.in_commentable = False
            self.body = []

    def depart_document(self, node):
        assert(not self.in_commentable)
        self.support_document.add_slice(''.join(self.body))