@@ -63,23 +63,28 @@ def convert(docstring: str, docformat: str, source_file: Path | None) -> str:
6363
6464
6565def embed_images (docstring : str , source_file : Path ) -> str :
66+ def local_image_to_data_uri (href : str ) -> str :
67+ image_path = source_file .parent / href
68+ image_data = image_path .read_bytes ()
69+ image_mime = mimetypes .guess_type (image_path )[0 ]
70+ image_data_b64 = base64 .b64encode (image_data ).decode ()
71+ return f"data:{ image_mime } ;base64,{ image_data_b64 } "
72+
6673 def embed_local_image (m : re .Match ) -> str :
67- image_path = source_file .parent / m ["href" ]
6874 try :
69- image_data = image_path .read_bytes ()
70- image_mime = mimetypes .guess_type (image_path )[0 ]
75+ href = local_image_to_data_uri (m ["href" ])
7176 except Exception :
7277 return m [0 ]
7378 else :
74- data = base64 . b64encode ( image_data ). decode ()
75- return f"![ { m [ 'alt' ] } ](data: { image_mime } ;base64, { data } )"
76-
77- return re . sub (
78- r"!\[\s*(?P<alt> .*?) \s*]\(\s*(?P<href>.+?)\s*\)" ,
79- embed_local_image ,
80- docstring ,
81- )
82- # TODO: Could probably do more here, e.g. support rST or raw HTML replacements.
79+ return m [ "before" ] + href + m [ "after" ]
80+
81+ # TODO: Could probably do more here, e.g. support rST replacements.
82+ for regex in [
83+ r"(?P<before> !\[\s*.*?\s*]\(\s*) (?P<href>.+?)(?P<after> \s*\) )" ,
84+ r"""(?P<before>src=['"])(?P<href>.+?)(?P<after>['"])""" ,
85+ ]:
86+ docstring = re . sub ( regex , embed_local_image , docstring )
87+ return docstring
8388
8489
8590def google (docstring : str ) -> str :
0 commit comments