Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/moin/converters/_tests/test_rst_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ def test_field_list(self, input, output):
"`Whitespace is\nnormalized & Case is KEPT.`_",
'<page><body><p><a xlink:href="wiki.local:Whitespace%20is%20normalized%20&amp;%20Case%20is%20KEPT.">Whitespace is\nnormalized &amp; Case is KEPT.</a></p></body></page>',
),
( # in rST, matching the reference text is case insensitive:
"Chapter 1\n===============\n\nA reference to `chapter 1`_.\n",
'<page><body><h outline-level="1">Chapter 1</h><p>A reference to <a xlink:href="wiki.local:#Chapter_1">chapter 1</a>.</p></body></page>',
),
( # check handling of non-ASCII chars:
"τίτλος\n^^^^^^\n\nA reference to `τίτλος`_.\n",
'<page><body><h outline-level="1">τίτλος</h><p>A reference to <a xlink:href="wiki.local:#A.2BA8QDrwPEA7sDvwPC-">τίτλος</a>.</p></body></page>',
),
(
"§ With % strange & siLLY <title>\n"
"--------------------------------\n\n"
"Reference to `§ With % strange\n"
"& siLLY \\<title>`_.\n",
'<page><body><h outline-level="1">§ With % strange &amp; siLLY &lt;title&gt;</h>'
'<p>Reference to <a xlink:href="wiki.local:#A.2BAKc_With_.25_strange_.26_siLLY_.3Ctitle.3E">§ With % strange\n'
"&amp; siLLY &lt;title&gt;</a>.</p></body></page>",
),
(
"http://www.python.org/",
'<page><body><p><a xlink:href="http://www.python.org/">http://www.python.org/</a></p></body></page>',
Expand Down
18 changes: 14 additions & 4 deletions src/moin/converters/rst_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from moin.utils.iri import Iri
from moin.utils.tree import html, moin_page, xlink, xinclude
from moin.utils.mime import Type, type_moin_document
from moin.wikiutil import normalize_pagename
from moin.wikiutil import anchor_name_from_text, normalize_pagename

from . import default_registry
from ._util import allowed_uri_scheme, decode_data, normalize_split_text
Expand Down Expand Up @@ -581,9 +581,18 @@ def visit_reference(self, node):
if not allowed_uri_scheme(refuri):
self.visit_error(node)
return
if refuri == "":
# build a link to a heading or an explicitly defined anchor
refuri = Iri(scheme="wiki.local", fragment=node.attributes["name"].replace(" ", "_"))

if refuri == "" and "refid" in node:
# internal cross-links
refid = node["refid"]
target_node = node.document.ids[refid]
# "refid" works fine with explicit anchors but the IDs given to
# section headings use the normalization function from Moin, not Docutils.
if isinstance(target_node, nodes.section):
title = target_node[0]
refid = anchor_name_from_text(title.astext())
refuri = Iri(scheme="wiki.local", fragment=refid)

if isinstance(refuri, str) and refuri.startswith("http"):
if "://" not in refuri:
refuri = refuri.split(":")[1]
Expand Down Expand Up @@ -789,6 +798,7 @@ class Parser(docutils.parsers.rst.Parser):
without matching target__.

__ https://docutils.sourceforge.io/docs/api/transforms.html
__ https://docutils.sourceforge.io/docs/ref/doctree.html#target
"""

config_section = "MoinMoin parser"
Expand Down