77import logging
88import os
99import re
10+ import urllib .parse
1011from enum import Enum
1112from pathlib import Path
1213from textwrap import dedent
14+ from bs4 import BeautifulSoup
1315from generate_library_libdoc import generate_libdoc_for_abstract_libraries
1416from 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+
136160def 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