1
1
__doc__ = """Replace urls across the entire book"""
2
2
3
- from copy import copy
3
+ from copy import copy , deepcopy
4
4
import glob
5
5
import json
6
6
from pathlib import Path
7
7
import sys
8
8
9
- from typing import cast
9
+ from typing import Any , Callable , List , Optional , cast
10
10
from urllib .parse import urlparse
11
11
12
- from libs .utils import Utils , debug as _debug
12
+ from libs .utils import debug as _debug
13
13
from libs .types import MdBook
14
14
15
+ PREPROCESSOR_NAME = "replace-urls"
16
+
15
17
16
18
def debug (* obj ):
17
19
return _debug ("REPLACE-URLS:" , * obj )
@@ -35,25 +37,38 @@ def is_url(url) -> bool:
35
37
return res
36
38
37
39
38
- if __name__ == "__main__" :
40
+ def main () :
39
41
if len (sys .argv ) > 1 :
40
42
if sys .argv [1 ] == "supports" :
41
43
sys .exit (0 )
42
44
context , book = json .load (sys .stdin )
43
45
44
46
book = MdBook (book )
45
47
46
- config = Utils . get_config_from_ctx ( "replace-urls" , context )
48
+ config = context [ "config" ][ "preprocessor" ][ PREPROCESSOR_NAME ]
47
49
if not config :
48
50
print (json .dumps (book ._data ))
49
51
exit (0 )
50
52
elif not isinstance (config , dict ):
51
53
print (json .dumps (book ._data ))
52
54
exit (0 )
53
55
56
+ book_src = cast (str , context ["config" ]["book" ]["src" ])
57
+
58
+ # Prefix to append to replaced urls if output.html.site-url is set and the replacement starts with `/`
59
+ site_url_prefix = ""
60
+ _output_html_site_url = None
61
+ try :
62
+ _aux = context ["config" ]["output" ]["html" ]["site-url" ]
63
+ _output_html_site_url = _aux
64
+ except Exception as _ :
65
+ pass
66
+ site_url_prefix = _output_html_site_url .rstrip ("/" ) if _output_html_site_url else ""
67
+ del _output_html_site_url
68
+
54
69
ignore_paths_list_globs = cast (list [str ], list (config .get ("ignore" ) or []))
55
- ignore_paths : list [str ] = []
56
- root_dir = Path (context ["root" ], context [ "config" ][ "book" ][ "src" ] )
70
+ ignore_paths : List [str ] = list ()
71
+ root_dir = Path (context ["root" ], book_src )
57
72
for p in ignore_paths_list_globs :
58
73
ignore_paths += glob .glob (p , root_dir = root_dir )
59
74
@@ -62,11 +77,20 @@ def is_url(url) -> bool:
62
77
config_mappings : dict = config ["mappings" ]
63
78
64
79
# Get the url mappings
80
+ # If replacement starts with `/`, prepend
65
81
url_mappings : list [tuple [str , str ]] = [
66
82
(k , v )
67
83
for k , v in config_mappings .items ()
68
84
if k not in _IGNORE_STRINGS and is_url (k )
69
85
]
86
+ url_mappings = list (
87
+ map (
88
+ lambda m : (
89
+ (m [0 ], site_url_prefix + m [1 ]) if cast (str , m [1 ]).startswith ("/" ) else m
90
+ ),
91
+ url_mappings ,
92
+ )
93
+ )
70
94
71
95
# Replace the urls
72
96
# book_s = json.dumps(book)
@@ -84,7 +108,10 @@ def is_url(url) -> bool:
84
108
continue
85
109
86
110
for old_url , new_url in url_mappings :
87
- old = copy (section .chapter .content )
88
111
section .chapter .content = section .chapter .content .replace (old_url , new_url )
89
112
90
113
print (json .dumps (book ._data ))
114
+
115
+
116
+ if __name__ == "__main__" :
117
+ main ()
0 commit comments