-
-
Save tony/1a03b7668c9e33672f4465dd63c6076b to your computer and use it in GitHub Desktop.
Trying to get the table of contents from a doctree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import docutils | |
from docutils import io | |
from docutils.core import Publisher, publish_doctree, publish_from_doctree | |
from docutils.transforms.parts import Contents | |
from docutils.writers.html5_polyglot import Writer | |
contents = """ | |
========= | |
Hey world | |
========= | |
what's up | |
--------- | |
ok | |
""" | |
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): | |
""" | |
Set up & run a `Publisher` to render from an existing document | |
tree data structure, for programmatic use with string I/O. Return | |
the encoded string output. | |
Note that document.settings is overridden; if you want to use the settings | |
of the original `document`, pass settings=document.settings. | |
Also, new document.transformer and document.reporter objects are | |
generated. | |
For encoded string output, be sure to set the 'output_encoding' setting to | |
the desired encoding. Set it to 'unicode' for unencoded Unicode string | |
output. Here's one way:: | |
publish_from_doctree( | |
..., settings_overrides={'output_encoding': 'unicode'}) | |
Parameters: `document` is a `docutils.nodes.document` object, an existing | |
document tree. | |
Other parameters: see `publish_programmatically`. | |
""" | |
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 | |
# parts for the contents | |
doc = publish_doctree( | |
source=contents, | |
) | |
publish_from_doctree(doc, writer=Writer()) | |
# from ptpdb import set_trace | |
# set_trace() | |
# from docutils.transforms.peps import Contents | |
doc.transformer.add_transform(Contents) | |
# ok so how do I get the TOC? | |
print(publish_parts_from_doctree(doc, writer=Writer())) | |
just_the_contents = publish_parts_from_doctree( | |
document=doc, | |
writer=Writer(), | |
) | |
print('All I want to have in html_body is: \n%s' % | |
just_the_contents['html_body']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment