Re: publish_parts and table of contents?

Tony Narlock <[email protected]>
Newsgroups gmane.text.docutils.user
Message-ID <CAC8GGzMKx5w4d5AzF3PWkg1SAXydDE6-4=Pje4WKw2nH5PK8pg@mail.gmail.com>
Thank you. This has been educational and shown me things that I didn’t find
obvious.

What if the user doesn’t want to publish CSS from the tree?  Even if using
publish_doctree, I prefer the equivalent to publish_parts fragment and
html_body, usually.

Here is what I did:

def publish_parts_from_doctree(document, destination_path=None,
                               writer=None, writer_name='pseudoxml',
                               settings=None, settings_spec=None,
                               settings_overrides=None, config_section=None,
                               enable_exit_status=False):
    reader = docutils.readers.doctree.Reader(parser_name='null')
    pub = Publisher(reader, None, writer,
                    source=io.DocTreeInput(document),
                    destination_class=io.StringOutput, settings=settings)
    if not writer and writer_name:
        pub.set_writer(writer_name)
    pub.process_programmatic_settings(
        settings_spec, settings_overrides, config_section)
    pub.set_destination(None, destination_path)
    pub.publish(enable_exit_status=enable_exit_status)
    return pub.writer.parts


Is the equivalent of this attainable a simpler way? Is something like this
worth considering as a patch?

On July 24, 2017 at 9:08:59 AM, Guenter Milde via Docutils-users (
[email protected]) wrote:

On 2017-07-22, Tony Narlock wrote:

> I can confirm getting the node information.

> The issue I have is getting the HTML from toc_list.

> The only other problem I have is:

...
> AttributeError: 'bullet_list' object has no attribute
> 'note_transform_message'

You need to pass a complete doctree to publish from doctree but
transforms.Contents.build_contents() returns only a partial doctree.

The example below should get you started.

Günter

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Proof of concept for a front end generating just a toc from an rst source.

import sys
import docutils
from docutils.core import Publisher, publish_doctree, publish_from_doctree
from docutils.transforms.parts import Contents
from docutils import nodes
# from docutils.writers.html5_polyglot import Writer

# Test source as string
sample = """

Sample Title
============

first section
-------------

some text

second section
--------------

more text

this is subsection 2.1
**********************

"""

# Parse sample to a doctree (for later parsing with build_contents())

sample_tree = publish_doctree(source=sample)

# The sample can also be written to supported output formats from the
doctree:
output = publish_from_doctree(sample_tree, writer_name="pseudoxml")
# output = publish_from_doctree(sample_tree, writer_name="latex")
# output = publish_from_doctree(sample_tree, writer_name="html5")

#print output


# Create a new document tree with just the table of contents
# ==========================================================

# document tree template:
toc_tree = nodes.document('', '', source='toc-generator')
toc_tree += nodes.title('', 'Table of Contents')

# Re-use the Contents transform to generate the toc by travelling over the
# doctree of the complete document.

# Set up a Contents instance:
# The Contents transform requires a "pending" startnode and generation
options
# startnode
pending = nodes.pending(Contents, rawsource='')
contents_transform = docutils.transforms.parts.Contents(sample_tree,
pending)
contents_transform.backlinks = False

# run the contents builder and append the result to the template:
toc_topic = nodes.topic(classes=['contents'])
toc_topic += contents_transform.build_contents(sample_tree)

toc_tree += toc_topic

# test

# print toc_tree
output = publish_from_doctree(toc_tree, writer_name="pseudoxml")
output = publish_from_doctree(toc_tree, writer_name="html5")
print output


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Docutils-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/docutils-users

Please use "Reply All" to reply to the list.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.