wal-o-mat
wal-o-mat

Reputation: 7344

restructuredtext: use directives for metadata

I'm writing a simple webpage generator based on restructuredtext and I'd like to put tags into the document, like this.

=====
Title
=====

:author: Me
:tags: foo, bar

Here we go ...

What I want now:

So I'd like to write compatible restructuredtext in case it's being compiled with something different than my program.

Can someone give me a hint? I found this one here http://svn.python.org/projects/external/docutils-0.6/docutils/examples.py showing in the internals method how to obtain the document (and therefore the dom tree), but is this the best way to go or would a regex based approach (find lines, remove them) be a lot easier? Working with the tree would also involve the conversion tree → document and so on.

Upvotes: 1

Views: 597

Answers (3)

G. Milde
G. Milde

Reputation: 862

In not too ancient Docutils, custom docinfo fields get the key as class value:

    <docinfo>
        <author>
            Me
        <field classes="tags">
            <field_name>
                tags
            <field_body>
                <paragraph>
                    foo, bar

If the class value is unique, you may use the strip-elements-with-classes configuration setting to remove it. (Warning: all elements containing the same class value will be removed, too, the remaining doctree may be invalid...)

Alternatively, you may use a CSS rule (or LaTeX preamble code) to prevent displaying the "tag" field.

The extraction (and a more failproof suppression) could be achieved in a Docutils transform. https://docutils.sourceforge.io/docs/peps/pep-0258.html#transformer https://docutils.sourceforge.io/docs/api/transforms.html

Unfortunately, the Docutils documentation has no guide for creating and using custom transforms. However, the docutils/transforms/*.py sources provide examples for extracting info from the doctree and moving/deleting/modifying nodes.

Upvotes: 0

wal-o-mat
wal-o-mat

Reputation: 7344

I think I have a nice solution for both problems. First, the core.py file in the docutils distribution shows how to obtain the doctree and how to write it (using a html writer for instance), see publish_from_doctree and publish_doctree. Then, there is docutils.nodes.SparseNodeVisitor which one can subclass and overwrite methods like visit_field to manipulate the document tree in various ways.

Upvotes: 2

Hans Then
Hans Then

Reputation: 11322

There are tools that can do this for you. See http://docutils.sourceforge.net/docs/user/links.html

Upvotes: 1

Related Questions