#94 Add source info to term node

closed-fixed
nobody
None
5
2014-08-17
2012-07-25
Kouhei Sutou
No

I'm using Docutils via Sphinx.
I'm also using Sphinx's i18n feature.
Sphinx's i18n feature has a problem.

Summary: Sphinx doesn't use term in definition list as translation target message.

Reason:
Sphinx extract translation target message from parsed doctree. Sphinx uses nodes that are instance of nodes.TextElement and have source information as translation target messages:
https://bitbucket.org/birkenfeld/sphinx/src/348224ae1fd5/sphinx/util/nodes.py#cl-46

Docutils parses term as nodes.term (that is instace of nodes.TextElement) but it doesn't have source information.

Here is a sample .rst:

term-word
description-paragraph

"description-paragraph" is used as translation target message but "term-word" isn't used.

Solution:
Could you set source information to term node? I attach a patch to do it.

Discussion

  • Günter Milde
    Günter Milde
    2012-08-13

    A more local patch would be:

    --- states.py (Revision 7485)
    +++ states.py (Arbeitskopie)
    @@ -2783,6 +2783,8 @@
    assert len(lines) == 1
    text_nodes, messages = self.inline_text(lines[0], lineno)
    term_node = nodes.term()
    + (term_node.source,
    + term_node.line) = self.state_machine.get_source_and_line(lineno)
    node_list = [term_node]
    for i in range(len(text_nodes)):
    node = text_nodes[i]

    +1 no change to "Text.term" method arguments - robust and backwards compatible.
    -1 additional call to self.state_machine.get_source_and_line()

    Why do you also add

    term_node.rawsource = lines[0]

    in your patch and are you sure this is the right thing?
    (All other occurences of rawsource use rawsource = unescape(string[matchstart:textend], 1))

     
  • Kouhei Sutou
    Kouhei Sutou
    2012-08-14

    > A more local patch would be:
    (snip)

    Should I recreate and attach my patch like the patch? If you mean that I'll do it.

    > Why do you also add
    >
    > term_node.rawsource = lines[0]
    >
    > in your patch and are you sure this is the right thing?
    > (All other occurences of rawsource use rawsource =
    > unescape(string[matchstart:textend], 1))

    Setting rawsource is the right thing. But missing unescape is not the right thing.
    rawsource is needed for Sphinx. Sphinx's i18n feature uses rawsource for translation target message:
    https://bitbucket.org/birkenfeld/sphinx/src/348224ae1fd5/sphinx/util/nodes.py#cl-55

     
  • Günter Milde
    Günter Milde
    2012-08-16

    I applied the updated patch to the SVN repo. Please test if it works as expected

    (also with non-trivial terms like::

    *hello* brave_ new
    world
    ).

     
  • Günter Milde
    Günter Milde
    2012-08-16

    • status: open --> closed-fixed
     
  • Kouhei Sutou
    Kouhei Sutou
    2012-08-17

    Thanks!
    I've confirmed that trunk works well with Sphinx!