7
7
import logging
8
8
import os
9
9
import re
10
+ import urllib .parse
10
11
from enum import Enum
11
12
from pathlib import Path
12
13
from textwrap import dedent
14
+ from bs4 import BeautifulSoup
13
15
from generate_library_libdoc import generate_libdoc_for_abstract_libraries
14
16
from robot .libdoc import libdoc
15
17
@@ -59,7 +61,7 @@ def convert_json_to_markdown(json_file, markdown_file):
59
61
raise ValueError ("Title not found in JSON data" ) from exc
60
62
61
63
content = [f"# { title } \n \n " ]
62
- description = data .get ("doc" , "" ).strip ()
64
+ description = fix_link_anchors ( data .get ("doc" , "" ).strip () )
63
65
if description :
64
66
content .append (f"{ description } \n \n " )
65
67
@@ -82,7 +84,9 @@ def convert_json_to_markdown(json_file, markdown_file):
82
84
# Write section title
83
85
# h2 title in contents panel
84
86
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 " )
86
90
87
91
if (
88
92
"returnType" in keyword
@@ -133,6 +137,26 @@ def convert_json_to_markdown(json_file, markdown_file):
133
137
md .write ("<hr style=\" border:1px solid grey\" >\n \n " )
134
138
135
139
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
+
136
160
def generate_libdoc (file : Path , output_file : Path ):
137
161
"""
138
162
Simply run `libdoc` on the file.
@@ -239,7 +263,7 @@ def main():
239
263
os .makedirs (target_dir )
240
264
241
265
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 :
243
267
platform_name = re .sub (r'_+' , ' ' , target_dir .stem .strip ('_' ))
244
268
if platform_name == "libraries" :
245
269
platform_name = "base"
0 commit comments