mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Switched to creating a list of html slices
This commit is contained in:
parent
0e26a6d46c
commit
777386ef56
@ -26,7 +26,7 @@ class WebSupportBuilder(PickleHTMLBuilder):
|
||||
self.translator_class = WebSupportTranslator
|
||||
|
||||
def write_doc(self, docname, doctree):
|
||||
# The translator needs the docuname to generate ids.
|
||||
# The translator needs the docname to generate ids.
|
||||
self.docname = docname
|
||||
PickleHTMLBuilder.write_doc(self, docname, doctree)
|
||||
|
||||
@ -38,10 +38,8 @@ class WebSupportBuilder(PickleHTMLBuilder):
|
||||
self.app.emit('html-page-context', pagename, ctx)
|
||||
|
||||
# Instead of pickling ctx as PickleHTMLBuilder does, we
|
||||
# create a Document object and pickle that.
|
||||
# have created a Document object and pickle that.
|
||||
document = self.docwriter.visitor.support_document
|
||||
document.body = ctx['body'] if 'body' in ctx else ''
|
||||
document.title = ctx['title'] if 'title' in ctx else ''
|
||||
|
||||
doc_filename = path.join(self.outdir,
|
||||
os_path(pagename) + self.out_suffix)
|
||||
|
@ -12,17 +12,14 @@
|
||||
import cPickle as pickle
|
||||
from os import path
|
||||
|
||||
from jinja2 import Template
|
||||
|
||||
from sphinx.application import Sphinx
|
||||
|
||||
class WebSupport(object):
|
||||
|
||||
def init(self, srcdir, outdir='', comment_html=''):
|
||||
def init(self, srcdir, outdir=''):
|
||||
self.srcdir = srcdir
|
||||
self.outdir = outdir or path.join(self.srcdir, '_build',
|
||||
'websupport')
|
||||
self.comment_template = Template(comment_html)
|
||||
|
||||
def build(self, **kwargs):
|
||||
doctreedir = kwargs.pop('doctreedir',
|
||||
@ -35,6 +32,4 @@ class WebSupport(object):
|
||||
infilename = path.join(self.outdir, docname + '.fpickle')
|
||||
f = open(infilename, 'rb')
|
||||
document = pickle.load(f)
|
||||
# The document renders the comment_template.
|
||||
document.comment_template = self.comment_template
|
||||
return document
|
||||
|
@ -18,20 +18,14 @@ from sphinx import addnodes
|
||||
class Document(object):
|
||||
"""A single Document such as 'index'."""
|
||||
def __init__(self):
|
||||
self.commentable_nodes = []
|
||||
self.template = None
|
||||
self.slices = []
|
||||
|
||||
def add_commentable(self, node_id, rst_source=''):
|
||||
node = CommentableNode(node_id, rst_source)
|
||||
def add_slice(self, html, id=None, commentable=False):
|
||||
slice = HTMLSlice(html, id, commentable)
|
||||
self.slices.append(slice)
|
||||
|
||||
def render_comment(self, id):
|
||||
return self.comment_template.render(id=id)
|
||||
|
||||
def render_html(self, comments=False):
|
||||
template = Template(self.body)
|
||||
return template.render(render_comment=self.render_comment)
|
||||
|
||||
class CommentableNode(object):
|
||||
def __init__(self, id, rst_source=''):
|
||||
class HTMLSlice(object):
|
||||
def __init__(self, html, id, commentable):
|
||||
self.html = html
|
||||
self.id = id
|
||||
self.rst_source=''
|
||||
self.commentable = commentable
|
||||
|
@ -18,12 +18,27 @@ class WebSupportTranslator(HTMLTranslator):
|
||||
"""
|
||||
def __init__(self, builder, *args, **kwargs):
|
||||
HTMLTranslator.__init__(self, builder, *args, **kwargs)
|
||||
self.init_support()
|
||||
|
||||
def init_support(self):
|
||||
self.support_document = Document()
|
||||
self.current_id = 0
|
||||
|
||||
def handle_visit_commentable(self, node):
|
||||
self.support_document.add_slice(''.join(self.body))
|
||||
self.body = []
|
||||
|
||||
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
|
||||
|
||||
def visit_paragraph(self, node):
|
||||
HTMLTranslator.visit_paragraph(self, node)
|
||||
self.handle_visit_commentable(node)
|
||||
|
||||
def depart_paragraph(self, node):
|
||||
HTMLTranslator.depart_paragraph(self, node)
|
||||
self.support_document.add_commentable(self.current_id)
|
||||
self.body.append("{{ render_comment('%s-p%s') }}" %
|
||||
(self.builder.docname, self.current_id))
|
||||
self.current_id += 1
|
||||
self.handle_depart_commentable(node)
|
||||
|
Loading…
Reference in New Issue
Block a user