Re: publish_parts and table of contents?
Guenter Milde via Docutils-users <[email protected]>
| Newsgroups | gmane.text.docutils.user |
|---|---|
| Message-ID | <[email protected]> |
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.