22import logging
33import typing as t
44from copy import deepcopy
5- from pathlib import Path
5+ from pprint import pformat
66
77from bs4 import BeautifulSoup
8+ from pathlibfs import Path
89
910logger = logging .getLogger (__name__ )
1011
1112
1213@dataclasses .dataclass
1314class HTMLImage :
1415 alt : str
15- src : str
16+ src : Path
1617
1718
1819class HTMLImageTranslator :
@@ -21,19 +22,18 @@ class HTMLImageTranslator:
2122 After that, replace URLs in HTML document.
2223 """
2324
24- def __init__ (self , html : str , source_path : t . Union [ str , Path ] , uploader : t .Optional [t .Callable ] = None ):
25+ def __init__ (self , html : str , source_path : Path , uploader : t .Optional [t .Callable ] = None ):
2526 self .html_in : str = html
2627 self .html_out : t .Optional [str ] = None
27- self .source_path = source_path
28+ self .source = source_path
2829 self .uploader = uploader
2930 self .images_in : t .List [HTMLImage ] = []
3031 self .images_local : t .List [HTMLImage ] = []
3132 self .images_remote : t .List [HTMLImage ] = []
3233
3334 def __str__ (self ):
34- return (
35- f"HTMLImageTranslator:\n in: { self .images_in } \n local: { self .images_local } \n remote: { self .images_remote } "
36- )
35+ info = {"source" : self .source , "in" : self .images_in , "local" : self .images_local , "remote" : self .images_remote }
36+ return f"HTMLImageTranslator:\n { pformat (info )} "
3737
3838 def discover (self ):
3939 self .scan ().resolve ()
@@ -59,9 +59,10 @@ def resolve(self) -> "HTMLImageTranslator":
5959 """
6060 Process discovered image elements, computing effective paths.
6161 """
62- if self .source_path is None :
62+ if self .source is None :
63+ logger .warning ("No resolving without source path" )
6364 return self
64- parent_path = Path ( self .source_path )
65+ parent_path = self .source
6566 if parent_path .is_file ():
6667 parent_path = parent_path .parent
6768 self .images_local = []
@@ -74,7 +75,7 @@ def resolve(self) -> "HTMLImageTranslator":
7475
7576 # Relative paths are relative to the original document.
7677 else :
77- image_new .src = str ( Path ( parent_path ) / image .src )
78+ image_new .src = parent_path / image .src
7879 self .images_local .append (image_new )
7980 return self
8081
@@ -86,7 +87,7 @@ def upload(self) -> "HTMLImageTranslator":
8687 logger .warning ("No upload without uploader" )
8788 return self
8889 for image_local in self .images_local :
89- hs_file = self .uploader (source = image_local .src , name = Path ( image_local .src ) .name )
90+ hs_file = self .uploader (source = image_local .src , name = image_local .src .name )
9091 image_url = hs_file .url
9192 image_remote : HTMLImage = deepcopy (image_local )
9293 image_remote .src = image_url
0 commit comments