Discussion:
[Docutils-develop] producing (non local) targets and references
Leo Noordergraaf
2017-03-16 21:23:38 UTC
Permalink
Hi,

Hope I'm at the right place for this question.

I'm developing a new directive to display JSON schemas.
(https://github.com/lnoor/sphinx-jsonschema)

Now I got stuck however.
I turn a schema into a table, that table gets displayed.
I want each schema to be in its own section with the schema title
as the section title.
I also want to add a target so that elsewhere I can add a reference
to this schema.

So I want to create something like:

.. schema_target:

Schema Title
------------
+---/
| /
+===/
| /
...
| /
+---/

...

blabla :ref:`schema_target` blabla


I used the code below to accomplish this.
The table parameter is the generated table.
In the schema '$$target' defines the label for the target.


def _wrap_in_section(self, schema, table):
result = list()
if '$$target' in schema:
anchor = self._normalize_target(schema['$$target'])

if 'title' in schema:
memo = self.state.memo
mylevel = memo.section_level
memo.section_level += 1
section_node = nodes.section()
textnodes, title_messages =
self.state.inline_text(schema['title'], self.lineno)
titlenode = nodes.title(schema['title'], '', *textnodes)
name = normalize_name(titlenode.astext())
section_node['names'].append(name)
if '$$target' in schema:
section_node['ids'].append(anchor)
# target_node = nodes.target()
# target_node.line = self.lineno
# target_node['names'].append(anchor)
# target_node['ids'].append(anchor)
# self.state.document.note_anonymous_target(target_node)
# section_node += target_node

section_node += titlenode
section_node += title_messages
self.state.document.note_implicit_target(section_node, section_node)
section_node += table
memo.section_level = mylevel
result.append(section_node)
return result
return [table]

This generates the section, adds a title and the table to it.
The problem is the commented section, it is supposed to create
the target but whatever I try I can't get it to work.

I've been liberally borrowing from nodes.py as you can see.
The target code I adapted from the Sphinx autosectionlabel.py file.
Perhaps that doesn't mix too well...

Anyway, any help would be appreciated a lot.
Leo

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

Please use "Reply All" to reply to the list.
Guenter Milde
2017-03-16 22:46:21 UTC
Permalink
Post by Leo Noordergraaf
Hi,
Hope I'm at the right place for this question.
I'm developing a new directive to display JSON schemas.
(https://github.com/lnoor/sphinx-jsonschema)
Now I got stuck however.
I turn a schema into a table, that table gets displayed.
I want each schema to be in its own section with the schema title
as the section title.
I also want to add a target so that elsewhere I can add a reference
to this schema.
Schema Title
------------
+---/
| /
+===/
| /
...
| /
+---/
...
blabla :ref:`schema_target` blabla
Transferring this to valid rST:

.. _schema_target:

Schema Title
------------
...

blabla :ref:`schema_target` blabla

and see what this becomes in docutils XML::

<target refid="schema-target"></target>
<section ids="schema-title schema-target"
names="schema\ title schema_target">
<title>Schema Title</title>
<paragraph>…</paragraph>
</section>

The section gets automatically a target id generated from its title.
The additional, manually set target is added as a second id by
transforms.references.PropagageTargets.
Post by Leo Noordergraaf
section_node['ids'].append(anchor)
# target_node = nodes.target()
# target_node.line = self.lineno
# target_node['names'].append(anchor)
# target_node['ids'].append(anchor)
# self.state.document.note_anonymous_target(target_node)
# section_node += target_node
This might help, if you called the PropagageTargets transform later, but it
is simpler:

If you want a second (or custom) target id for the section, don't create a
target node but add it to the section nodes id attribute.

Günter

Loading...