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