Skip to content

Commit fe64590

Browse files
committed
Libdoc: Fix local references in args/return/raises docs
Also add explicit test that local references cannot be used with other keywords. Fixes #5810.
1 parent 1502341 commit fe64590

4 files changed

Lines changed: 68 additions & 16 deletions

File tree

‎atest/robot/libdoc/markdown_format.robot‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,32 @@ Automatic reference targets
2323
... and <a href="#type-list" title="&quot;list&quot; type">list</a>.
2424
... model=${MODEL}[keywords][2]
2525

26-
Custom references defined in introduction work also with keywords
27-
Custom references defined in introduction like <a href="http://example.com" title="An &quot;example&quot;!">reference</a> work too!
26+
Custom references defined globally and locally work in keyword docs
27+
Custom references defined in introduction like <a href="http://example.com" title="An &quot;example&quot;!">reference</a> and locally
28+
... like <a href="http://robotframework.org">local</a> work too!
2829
... model=${MODEL}[keywords][2]
2930

31+
Links in argument docs
32+
Arg with links to <a href="#Introduction" title="&quot;Introduction&quot; section">introduction</a> and <a href="#Admonitions" title="&quot;Admonitions&quot; keyword">Admonitions</a>.
33+
... model=${MODEL}[keywords][2][args][0]
34+
Arg with <a href="http://example.com" title="An &quot;example&quot;!">global</a> and <a href="http://robotframework.org">local</a> reference.
35+
... model=${MODEL}[keywords][2][args][1]
36+
37+
Links in return docs
38+
Return with links to <a href="#Introduction" title="&quot;Introduction&quot; section">introduction</a> and <a href="#Admonitions" title="&quot;Admonitions&quot; keyword">Admonitions</a>
39+
... and <a href="http://example.com" title="An &quot;example&quot;!">global</a> and <a href="http://robotframework.org">local</a> reference.
40+
... model=${MODEL}[keywords][2][returnDoc]
41+
42+
Links in exception docs
43+
Error with links to <a href="#Introduction" title="&quot;Introduction&quot; section">introduction</a> and <a href="#Admonitions" title="&quot;Admonitions&quot; keyword">Admonitions</a>.
44+
... model=${MODEL}[keywords][2][raises][E]
45+
Error with <a href="http://example.com" title="An &quot;example&quot;!">global</a> and <a href="http://robotframework.org">local</a> reference.
46+
... model=${MODEL}[keywords][2][raises][F]
47+
48+
Local references do not work with other keywords
49+
[local] references do not work with other keywords.
50+
... model=${MODEL}[keywords][3]
51+
3052
Unordered lists
3153
<ul>\n<li>First unordered item.</li>\n<li>Second item.</li>\n</ul>
3254
... model=${MODEL}[keywords][1]
@@ -85,7 +107,7 @@ Tables
85107
... </tr>
86108
... </tbody>
87109
... </table>
88-
... model=${MODEL}[keywords][4]
110+
... model=${MODEL}[keywords][5]
89111
<table>
90112
... <thead>
91113
... <tr>
@@ -102,18 +124,18 @@ Tables
102124
... </tr>
103125
... </tbody>
104126
... </table>
105-
... model=${MODEL}[keywords][4]
127+
... model=${MODEL}[keywords][5]
106128

107129
Syntax highlighting
108130
<div class="code"><pre><span></span><code><span class="gh">*** Test Cases ***</span>
109131
... <span class="gu">Example</span>
110132
... <span class="p"> \ \ \ </span><span class="nf">Keyword</span><span class="p"> \ \ \ </span><span class="s">arg</span>
111133
... </code></pre></div>
112-
... model=${MODEL}[keywords][3]
134+
... model=${MODEL}[keywords][4]
113135
<span class="c1"># This is comment in code, not a Markdown header!</span>
114-
... model=${MODEL}[keywords][3]
136+
... model=${MODEL}[keywords][4]
115137
<div class="code"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="s2">
116-
... model=${MODEL}[keywords][3]
138+
... model=${MODEL}[keywords][4]
117139

118140
Admonitions
119141
<div class="admonition note">
@@ -235,7 +257,7 @@ Table of contents in keyword documentation
235257
... <li><a href="#where-to-learn-more">Where to learn more?</a></li>
236258
... </ul>
237259
... </div>
238-
... model=${MODEL}[keywords][5]
260+
... model=${MODEL}[keywords][6]
239261

240262
*** Keywords ***
241263
Generate doc using Markdown source
@@ -244,4 +266,9 @@ Generate doc using Markdown source
244266
Doc should contain
245267
[Arguments] @{content} ${model}=${MODEL}
246268
VAR ${content} @{content} separator=\n
247-
Should Contain ${model}[doc] ${content}
269+
IF "doc" in $model
270+
VAR ${doc} ${model}[doc]
271+
ELSE
272+
VAR ${doc} ${model}
273+
END
274+
Should Contain ${doc} ${content}

‎atest/testdata/libdoc/MarkdownFormat.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,29 @@ def references(a: int, b: "str | list[str]"):
8080
like [linking], to keywords like [Admonitions] and to types like [int]
8181
and [list].
8282
83-
Custom references defined in introduction like [reference] work too!
83+
Custom references defined in introduction like [reference] and locally
84+
like [local] work too!
85+
86+
Args:
87+
a: Arg with links to [introduction] and [Admonitions].
88+
b: Arg with [global][reference] and [local] reference.
89+
90+
Returns:
91+
Return with links to [introduction] and [Admonitions]
92+
and [global][reference] and [local] reference.
93+
94+
Raises:
95+
E: Error with links to [introduction] and [Admonitions].
96+
F: Error with [global][reference] and [local] reference.
97+
98+
[local]: http://robotframework.org
8499
"""
85100

86101

102+
def references_2():
103+
"""[local] references do not work with other keywords."""
104+
105+
87106
def admonitions():
88107
"""
89108
> [!note]

‎src/robot/libdocpkg/htmlutils.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ def _get_markdown_toc_tokens(self, toc_tokens) -> "Iterable[tuple[str, str]]":
109109
def html(self, doc: str) -> str:
110110
return self._doc_to_html(doc)
111111

112+
def reset(self):
113+
self._doc_to_html.reset()
114+
112115

113116
class DocToHtml:
114117

@@ -133,6 +136,11 @@ def _get_formatter(self, doc_format: DocFormat) -> Callable[[str], str]:
133136
def __call__(self, doc: str) -> str:
134137
return self.formatter(doc)
135138

139+
def reset(self):
140+
if self._md is not None:
141+
self._md.reset()
142+
self._md.references = self.targets.copy()
143+
136144
def _format_robot(self, doc: str) -> str:
137145
toc = self._get_toc(doc) if "%TOC%" in doc else None
138146
doc = html_format(doc)
@@ -182,7 +190,7 @@ def _format_rest(self, doc: str) -> str:
182190

183191
def _format_markdown(self, doc: str) -> str:
184192
if self._md is None:
185-
self._md = md = Markdown(
193+
self._md = Markdown(
186194
extensions=[
187195
"codehilite",
188196
"fenced_code",
@@ -202,11 +210,8 @@ def _format_markdown(self, doc: str) -> str:
202210
},
203211
output_format="html",
204212
)
205-
else:
206-
md = self._md.reset()
207-
# Initialize references and use NormalizedDict to make lookup case-insensitive.
208-
md.references = self.targets.copy()
209-
return md.convert(doc)
213+
self._md.references = self.targets.copy()
214+
return self._md.convert(doc)
210215

211216
def _handle_backtick_links(self, doc: str) -> str:
212217
return re.sub("`(.+?)`", self._handle_names, doc)

‎src/robot/libdocpkg/model.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def convert_docs_to_html(self):
100100
)
101101
self.doc = formatter.html(self.doc)
102102
for item in self.inits + self.keywords:
103+
formatter.reset()
103104
# If 'short_doc' is not set, it is generated automatically based on 'doc'
104105
# when accessed. Generate and set it to avoid HTML format affecting it.
105106
item.short_doc = item.short_doc

0 commit comments

Comments
 (0)