Slice at bullet_list, desc, or paragraph now.

This commit is contained in:
jacob 2010-05-31 17:12:32 -05:00
parent 777386ef56
commit 9e81b8f003

View File

@ -16,29 +16,46 @@ 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):
self.support_document.add_slice(''.join(self.body))
self.body = []
# 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):
slice_id = '%s-%s' % (self.builder.docname, self.current_id)
self.support_document.add_slice(''.join(self.body),
slice_id, commentable=True)
self.body = []
self.current_id += 1
assert(self.in_commentable)
if node.commented:
slice_id = '%s-%s' % (self.builder.docname, self.current_id)
self.current_id += 1
def visit_paragraph(self, node):
HTMLTranslator.visit_paragraph(self, node)
self.handle_visit_commentable(node)
body = ''.join(self.body)
self.support_document.add_slice(body, slice_id, commentable=True)
self.in_commentable = False
self.body = []
def depart_paragraph(self, node):
HTMLTranslator.depart_paragraph(self, node)
self.handle_depart_commentable(node)