getnikola / plugins

Extra plugins for Nikola
https://plugins.getnikola.com/
MIT License
59 stars 95 forks source link

[sphinx_roles] ref role not working : ref label is missing #295

Closed matclab closed 5 years ago

matclab commented 5 years ago

I'm writing a document with the following code:

balbla :ref:`formal verification` blabla

.. _formal verification:

Formal Verification in a Nutshell
=================================

And nikola says : ERROR: rest: [posts/2019-01/post.rst:89] ref label formal verification is missing or not immediately before figure or section.

Doing some others tries, and adding:

:ref:`sec-introduction`

.. _sec:introduction:

Introduction
==========

gives the following stack trace:

PythonAction Error
Traceback (most recent call last):
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/doit/action.py", line 424, in execute
    returned_value = self.py_callable(*self.args, **kwargs)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/nikola/post.py", line 652, in compile
    lang)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/nikola/plugins/compile/rest/__init__.py", line 152, in compile
    output, error_level, deps, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/nikola/plugins/compile/rest/__init__.py", line 136, in compile_string
    new_data, settings_overrides=settings_overrides, logger=self.logger, source_path=source_path, l_add_ln=add_ln, transforms=self.site.rst_transforms)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/nikola/plugins/compile/rest/__init__.py", line 345, in rst2html
    pub.publish(enable_exit_status=enable_exit_status)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/core.py", line 217, in publish
    self.settings)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/readers/__init__.py", line 72, in read
    self.parse()
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/readers/__init__.py", line 78, in parse
    self.parser.parse(self.input, document)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/__init__.py", line 191, in parse
    self.statemachine.run(inputlines, document, inliner=self.inliner)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 171, in run
    input_source=document['source'])
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/statemachine.py", line 239, in run
    context, state, transitions)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/statemachine.py", line 460, in check_line
    return method(match, context, next_state)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 2753, in underline
    self.section(title, source, style, lineno - 1, messages)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 327, in section
    self.new_subsection(title, lineno, messages)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 395, in new_subsection
    node=section_node, match_titles=True)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 282, in nested_parse
    node=node, match_titles=match_titles)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 196, in run
    results = StateMachineWS.run(self, input_lines, input_offset)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/statemachine.py", line 239, in run
    context, state, transitions)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/statemachine.py", line 460, in check_line
    return method(match, context, next_state)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 2767, in text
    paragraph, literalnext = self.paragraph(lines, startline)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 418, in paragraph
    textnodes, messages = self.inline_text(text, lineno)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 427, in inline_text
    return self.inliner.parse(text, lineno, self.memo, self.parent)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 645, in parse
    lineno)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 787, in interpreted_or_phrase_ref
    lineno)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/parsers/rst/states.py", line 871, in interpreted
    nodes, messages2 = role_fn(role, rawsource, text, lineno, self)
  File "/homecrypt/clabaut/systerel/dt/tickets/079_blog/nikola/blogsysterel/plugins/sphinx_roles/sphinx_roles.py", line 525, in ref_role
    inliner.document.walk(visitor)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/nodes.py", line 138, in walk
    if child.walk(visitor):
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/nodes.py", line 138, in walk
    if child.walk(visitor):
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/nodes.py", line 130, in walk
    visitor.dispatch_visit(self)
  File "/home/clabaut/.virtualenvs/dt079/lib/python3.7/site-packages/docutils/nodes.py", line 1882, in dispatch_visit
    return method(node)
  File "/homecrypt/clabaut/systerel/dt/tickets/079_blog/nikola/blogsysterel/plugins/sphinx_roles/sphinx_roles.py", line 513, in visit_target
    next_sib = sibs[sibs.index(node) + 1]
IndexError: list index out of range

########################################
ralsina commented 5 years ago

The example in the sphinx docs works:

.. _my-reference-label:

Section to cross-reference
--------------------------

This is the text of the section.

It refers to the section itself, see :ref:`my-reference-label`.

Produces this:

<div class="section" id="section-to-cross-reference">
<span id="my-reference-label"></span><h1>Section to cross-reference</h1>
<p>This is the text of the section.</p>
<p>It refers to the section itself, see <a class="reference external" href="#my-reference-label">Section To Cross-Reference</a>.</p>
</div>
ralsina commented 5 years ago

Aha! It only works if you refer to labels defined BEFORE the reference.

So this works:

.. _formal-verification:

Formal Verification in a Nutshell
=================================

asdasd

balbla :ref:`formal-verification` blabla

But this doesn't:

balbla :ref:`formal-verification` blabla

.. _formal-verification:

Formal Verification in a Nutshell
=================================

asdasd

Not sure how to fix it, tho, but will take a look. One possible solution is to just do the links without checking. Worst case you end up with a link pointing at nothing.

matclab commented 5 years ago

Yes, just doing the links would do the trick. Another solution is perhaps to collect all the target for whic links are generated, and all the existing target, and verify at the end that the first set is included in the second one. Though, I'm not sure it will fit the actual visiting algorithm. Thanks a lot for investigating.

ralsina commented 5 years ago

Yes, the thing is that those nodes that have not yet been visited are not there. Fixing that would require some sort of two-pass scan, so just not checking seems to be the only way forward.

ralsina commented 5 years ago

Looking at the actual implementation ... I don't understand it. Yes, I wrote it. I think :-)

Rewriting it in a dumb but foolproof way.

Sigh: inliner.document doesn't have the whole document, it has the document as seen so far which makes some sense, I guess.

But I do need to have seen the whole document in order to resolve the references and replace them with links where the text is the title of the section.

matclab commented 5 years ago

« I don't understand it. Yes, I wrote it. I think »… I know that feeling ;-)

ralsina commented 5 years ago

Solution in place where it will always produce "something" but the title in the ref (if not given explicitly) could be wrong.

matclab commented 5 years ago

I tried with 2693266, and after having deleted the spurious set_trace, it looks like it still fails with the following post:

.. slug: test
.. date: 2019-01-04 12:44:53 UTC+01:00
.. tags: 
.. category: 
.. link: 
.. description: 
.. type: text

balbla :ref:`formal-verification` blabla

.. _formal-verification:

Formal Verification in a Nutshell
=================================

asdasd
ralsina commented 5 years ago

Could you check with current latest revision?

Turns out I was logging the warning in the document, which of course sucked.

On Tue, Jan 8, 2019 at 10:17 AM matclab notifications@github.com wrote:

I tried with 2693266 https://github.com/getnikola/plugins/commit/26932660a6869987294f7f659f0fd4ce3e3f3e50, and after having deleted the spurious set_trace, it looks like it still fails with the following post:

.. slug: test .. date: 2019-01-04 12:44:53 UTC+01:00 .. tags: .. category: .. link: .. description: .. type: text

balbla :ref:formal-verification blabla

.. _formal-verification:

Formal Verification in a Nutshell

asdasd

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/getnikola/plugins/issues/295#issuecomment-452294700, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAGK6EUUFATOin-n2M8-MMcQzr-pBnyks5vBJpegaJpZM4ZzvTo .

matclab commented 5 years ago

Yep, it works. Thanks a lot.

One last thing, according to sphinx documentation I think one could do a reference with the title of a section, like :ref:`Formal Verification in a Nutshell`. However it only works if you do :ref:`formal-verification-in-a-nutshell`. Is it intended ?

Still it is only a minor glitch. Thanks a lot for the work.

ralsina commented 5 years ago

Could be added, but again it would only work reliably for sections defined before the reference.

On second look, I don't see that in the sphinx doc. It seems to support:

:ref:`some-label`
:ref:`A Title <some-label>`

And both should be working now with varying levels of success.

What is supported is

`Section Title`_

But that is just a regular docutils link and should already work.

matclab commented 5 years ago

Ho, I somewhat mixed the two.

anyway thank you again :+1: