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