Example #1
0
    def render_partial(self, node):
        """Utility: Render a lone doctree node."""
        if node is None:
            return {'fragment': ''}
        doc = new_document(b'<partial node>')
        doc.append(node)

        if self._publisher is None:
            self._publisher = Publisher(
                source_class=DocTreeInput,
                destination_class=StringOutput)
            self._publisher.set_components('standalone',
                                           'restructuredtext', 'pseudoxml')

        pub = self._publisher

        pub.reader = DoctreeReader()
        pub.writer = MarkdownWriter(self)
        pub.process_programmatic_settings(
            None, {}, None)
        #pub.process_programmatic_settings(
        #    None, {'output_encoding': 'unicode'}, None)
        pub.set_source(doc, None)
        pub.set_destination(None, None)
        pub.publish()
        return pub.writer.parts
Example #2
0
 def render_partial(self, node):
     """Utility: Render a lone doctree node."""
     doc = new_document('<partial node>')
     doc.append(node)
     return publish_parts(doc,
                          source_class=DocTreeInput,
                          reader=DoctreeReader(),
                          writer=HTMLWriter(self),
                          settings_overrides={'output_encoding': 'unicode'})
Example #3
0
def render_map(builder, node):
    if node:
        doc = new_document(b('<partial node>'))
        doc.append(node)

        publisher = Publisher( source_class = DocTreeInput, destination_class=StringOutput)
        publisher.set_components('standalone', 'restructuredtext', 'pseudoxml')
        publisher.reader = DoctreeReader()
        publisher.writer = DitaMapWriter(builder)
        publisher.process_programmatic_settings(None, {'output_encoding': 'utf-8'}, None)
        publisher.set_source(doc, None)
        publisher.set_destination(None, None)
        publisher.publish()
        return publisher.writer.output
    output = XML_HEAD
    output += u"<map></map>"
    return output
Example #4
0
def aFunction(string):
    source = StringInput(string, encoding='utf-8')
    docname = "fake"
    __app.env.temp_data['docname'] = docname

    settings = frontend.OptionParser((Writer, )).get_default_values()
    settings.tab_width = 8
    settings.pep_references = False
    settings.rfc_references = False
    settings.env = __app.env

    reader = DoctreeReader()
    parser = parsers.get_parser_class("rst")()

    docu = utils.new_document(source.source_path, settings)
    parser.parse(source.read(), docu)
    __app.builder.prepare_writing((docname, ))
    return __app.builder.write_doc(docname, docu).destination
Example #5
0
def env_updated(app, env):
    config = app.builder.config

    doctree = env.get_doctree(config.master_doc)
    from sphinx import addnodes

    toctrees = []
    for toctreenode in doctree.traverse(addnodes.toctree):
        toctree = env.resolve_toctree(config.master_doc, app.builder, 
            toctreenode, prune = False, includehidden = True, maxdepth = 0,
            collapse = False)
        toctrees.append(toctree)

    if not toctrees:
        toc = None
    else:
        toc = toctrees[0]
        for toctree in toctrees[1:]:
            toc.extend(toctree.children)

    # toc = env.get_toctree_for(config.master_doc, app.builder, False)

    node = toc

    doc = new_document(b('<partial node>'))
    doc.append(node)

    pub = Publisher(
            source_class = DocTreeInput,
            destination_class=StringOutput)

    pub.set_components('standalone', 'restructuredtext', 'pseudoxml')

    pub.reader = DoctreeReader()
    pub.writer = Writer()
    pub.writer.format = 'pseudoxml'
    pub.process_programmatic_settings(
        None, {'output_encoding': 'unicode'}, None)
    pub.set_source(doc, None)
    pub.set_destination(None, None)
    pub.publish()

    import xml.etree.cElementTree as ET
    from cStringIO import StringIO

    #out = re.sub(r'^<!DOCTYPE[^>]*>\s*', '<?xml version="1.0"?>', pub.writer.output)
    out = pub.writer.output.encode('utf-8').replace(' encoding="unicode"', ' encoding="utf-8"')

    #pprint.pprint(out)

    doctree = ET.fromstring(out)

    #pprint.pprint(doctree)
    #pprint.pprint(dir(doctree))
    if hasattr(doctree, 'getroot'):
        doctree = doctree.getroot()
    #root = doctree.getroot()

    fuzzy_find_entries = []
    docs = {}

    def indexentries(entry, links, cap = ''):
        if links:
            fuzzy_find_entries.append(dict(
                href = links[0][1],
                name = entry,
                path = "index/%s/%s" % (char,cap),
                info = "INDEX",
                detail = '',
            ))

            for i, (ismain, link) in enumerate(links[1:], start=1):
                doclink = link.split('#', 1)[0]
                docname = docs.get(doclink, i)
                fuzzy_find_entries.append(dict(
                    href = link,
                    name = "%s (%s)" % (entry, docname),
                    path = "index/%s/%s" % (char, cap),
                    info = "INDEX",
                    detail = '',
                ))
            return ''
        else:
            return entry

    if app.config.html_findanything_add_topics:
        if hasattr(doctree, 'iter'):
            references = doctree.iter('reference')
        else:
            references = doctree.getiterator('reference')

    #            fuzzy_find_entries.append(dict(
    #                    href = link,
    #                    name = entry,
    #                    path = "index/%s/" % char,
    #                    info = "INDEX",
    #                    detail = '',
    #                ))

        refset = set()

        for ref in references:
            refuri = ref.attrib['refuri']

            docs[refuri] = ref.text
            path = "/"
            if "/" in refuri:
                path = refuri.rsplit('/', 1)[0]+"/"

            if '#' in refuri:
                docname = docs.get(refuri.split('#', 1)[0])
                if docname:
                    path += docname + "/"
                info = 'SECTION'
            else:
                info = 'PAGE'

            e = dict(
                    href = ref.attrib['refuri'],
                    name = ref.text,
                    info = info,
                    path = path,
                    detail = '',
                )

            refid = "%(href)s^%(path)s^%(name)s^%(info)s" % e

            if refid in refset: continue

            refset.add(refid)

            fuzzy_find_entries.append(e)


    if app.config.html_findanything_add_indexentries:
        genindex = env.create_index(app.builder)

        for char,char_list in genindex:
            for entry, (links, subitems) in char_list:
                cap = indexentries(entry, links)
                if subitems:
                    if cap: cap += '/'
                    for subname, sublinks in subitems:
                        indexentries(subname, sublinks, cap=cap)
    
    s = json.dumps(fuzzy_find_entries)

    static_dir = os.path.join(app.builder.outdir, '_static')

    if not os.path.exists(static_dir):
        os.makedirs(static_dir)

    with open(os.path.join(static_dir, 'fuzzyindex.js'), 'wb') as f:
        f.write("DOCUMENTATION_OPTIONS.FIND_ANYTHING_ENTRIES = %s;" % s);


        if app.config.html_findanything_use_cached_hits:
            f.write("DOCUMENTATION_OPTIONS.FIND_ANYTHING_USE_CACHED_HITS = true;")

        f.write("DOCUMENTATION_OPTIONS.FIND_ANYTHING_WIDTH = '%s';" 
            % app.config.html_findanything_width);