Skip to content

Commit c1acaca

Browse files
authored
[BugFix] Fix URL anchor formats in generated library docs (#36)
1 parent 918ebcd commit c1acaca

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

docs/.sphinx/rf_libraries/convert_libdoc_to_md.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import logging
88
import os
99
import re
10+
import urllib.parse
1011
from enum import Enum
1112
from pathlib import Path
1213
from textwrap import dedent
14+
from bs4 import BeautifulSoup
1315
from generate_library_libdoc import generate_libdoc_for_abstract_libraries
1416
from robot.libdoc import libdoc
1517

@@ -59,7 +61,7 @@ def convert_json_to_markdown(json_file, markdown_file):
5961
raise ValueError("Title not found in JSON data") from exc
6062

6163
content = [f"# {title}\n\n"]
62-
description = data.get("doc", "").strip()
64+
description = fix_link_anchors(data.get("doc", "").strip())
6365
if description:
6466
content.append(f"{description}\n\n")
6567

@@ -82,7 +84,9 @@ def convert_json_to_markdown(json_file, markdown_file):
8284
# Write section title
8385
# h2 title in contents panel
8486
content.append(f"### {keyword['name']}\n\n")
85-
content.append(f"{keyword['doc']}\n\n")
87+
# Write keyword description and fix URL-encoded anchors
88+
keyword_doc = fix_link_anchors(keyword['doc'])
89+
content.append(f"{keyword_doc}\n\n")
8690

8791
if (
8892
"returnType" in keyword
@@ -133,6 +137,26 @@ def convert_json_to_markdown(json_file, markdown_file):
133137
md.write("<hr style=\"border:1px solid grey\">\n\n")
134138

135139

140+
def fix_link_anchors(html_text):
141+
"""
142+
Replace special characters in the URL anchors in the given HTML text with dashes
143+
"""
144+
soup = BeautifulSoup(html_text, 'html.parser')
145+
for link in soup.find_all('a', href=True):
146+
href = link['href']
147+
if '#' in href:
148+
# Split anchor part in URL and clean up decoded text
149+
parts = href.rsplit('#', 1)
150+
if len(parts) == 2:
151+
url_part, anchor_part = parts
152+
if anchor_part.strip():
153+
decoded = urllib.parse.unquote(anchor_part)
154+
clean = re.sub(r'[^a-zA-Z0-9]+', '-', decoded.lower()).strip('-')
155+
if clean:
156+
link['href'] = f'{url_part}#{clean}' if url_part else f'#{clean}'
157+
return str(soup)
158+
159+
136160
def generate_libdoc(file: Path, output_file: Path):
137161
"""
138162
Simply run `libdoc` on the file.
@@ -239,7 +263,7 @@ def main():
239263
os.makedirs(target_dir)
240264

241265
if target is RobotFile.LIBRARY and not (target_dir / "index.md").exists():
242-
with open(target_dir / "index.md", "w") as index_file:
266+
with open(target_dir / "index.md", "w", encoding="utf-8") as index_file:
243267
platform_name = re.sub(r'_+', ' ', target_dir.stem.strip('_'))
244268
if platform_name == "libraries":
245269
platform_name = "base"

0 commit comments

Comments
 (0)