6
6
import frontmatter
7
7
import markdown
8
8
from bs4 import BeautifulSoup
9
- from mdx_wikilink_plus .mdx_wikilink_plus import WikiLinkPlusExtension
10
9
from mkdocs .config import config_options
11
10
from mkdocs .plugins import BasePlugin
12
11
from mkdocs_callouts .plugin import CalloutsPlugin
13
12
from custom_attributes .plugin import convert_text_attributes
14
13
import logging
15
14
16
- from mkdocs_embed_file_plugins .src .links_correction import convert_links_if_markdown , mini_ez_links
17
- from mkdocs_embed_file_plugins .src .search_quote import search_file_in_documentation , search_in_file
15
+ from mkdocs_embed_file_plugins .src .links_correction import (
16
+ convert_links_if_markdown ,
17
+ mini_ez_links ,
18
+ )
19
+ from mkdocs_embed_file_plugins .src .search_quote import (
20
+ search_file_in_documentation ,
21
+ search_in_file ,
22
+ )
18
23
from mkdocs_embed_file_plugins .src .utils import create_link , strip_comments
19
24
20
25
21
26
def cite (
22
- md_link_path , link , soup , citation_part , config ,
23
- callouts , custom_attr , msg
24
- ) -> BeautifulSoup :
27
+ md_link_path , link , soup , citation_part , config , callouts , custom_attr , msg
28
+ ) -> BeautifulSoup :
25
29
"""Append the content of the founded file to the original file.
26
30
27
31
Args:
@@ -35,186 +39,188 @@ def cite(
35
39
msg (str): Message to be displayed if the file is not found.
36
40
Returns: updated HTML
37
41
"""
38
- docs = config [' docs_dir' ]
39
- url = config [' site_url' ]
42
+ docs = config [" docs_dir" ]
43
+ url = config [" site_url" ]
40
44
41
45
md_config = {
42
- 'mdx_wikilink_plus' : {
43
- 'base_url' : (docs , url , md_link_path ),
44
- 'build_url' : mini_ez_links ,
45
- 'image_class' : 'wikilink' ,
46
- }
46
+ "mdx_wikilink_plus" : {
47
+ "base_url" : (docs , url , md_link_path ),
48
+ "build_url" : mini_ez_links ,
49
+ "image_class" : "wikilink" ,
47
50
}
51
+ }
48
52
new_uri = str (md_link_path ).replace (str (docs ), str (url ))
49
- new_uri = new_uri .replace (' \\ ' , '/' )
50
- new_uri = new_uri .replace (' .md' , '/' )
51
- new_uri = new_uri .replace ('//' , '/' )
52
- new_uri = re .sub (' https?:\\ /' , ' \\ g<0>/' , new_uri )
53
- new_uri = new_uri .replace (' /index/' , '/' )
54
- input_file = codecs .open (str (md_link_path ), mode = 'r' , encoding = ' utf-8' )
53
+ new_uri = new_uri .replace (" \\ " , "/" )
54
+ new_uri = new_uri .replace (" .md" , "/" )
55
+ new_uri = new_uri .replace ("//" , "/" )
56
+ new_uri = re .sub (" https?:\\ /" , " \\ g<0>/" , new_uri )
57
+ new_uri = new_uri .replace (" /index/" , "/" )
58
+ input_file = codecs .open (str (md_link_path ), mode = "r" , encoding = " utf-8" )
55
59
text = input_file .read ()
56
60
57
61
contents = frontmatter .loads (text ).content
58
62
quote = search_in_file (citation_part , contents )
59
63
tooltip_template = (
60
- "<div class='citation'> <a href='"
61
- + str (link ['src' ])
62
- + "' class='link_citation'><i class='fas fa-link'></i> </a>"
63
- + str (link ['alt' ]) + ' not exists.'
64
- + '</div>'
64
+ "<div class='citation'> <a href='"
65
+ + str (link ["src" ])
66
+ + "' class='link_citation'><i class='fas fa-link'></i> </a>"
67
+ + str (link ["alt" ])
68
+ + " not exists."
69
+ + "</div>"
65
70
)
66
- if len (quote ) > 0 :
67
- if callouts :
71
+ if len (quote ) > 0 :
72
+ if callouts :
68
73
quote = CalloutsPlugin ().on_page_markdown (quote , None , None , None )
69
- if len (custom_attr ) > 0 :
70
- config_attr = {
71
- 'file' : custom_attr ,
72
- 'docs_dir' : docs
73
- }
74
+ if len (custom_attr ) > 0 :
75
+ config_attr = {"file" : custom_attr , "docs_dir" : docs }
74
76
quote = convert_text_attributes (quote , config_attr )
75
77
quote = convert_links_if_markdown (quote , (docs , url , md_link_path ))
76
78
quote = strip_comments (quote )
77
- md_extensions = config [' markdown_extensions' ]
78
- md_extensions .append (' mdx_wikilink_plus' )
79
+ md_extensions = config [" markdown_extensions" ]
80
+ md_extensions .append (" mdx_wikilink_plus" )
79
81
html = markdown .markdown (
80
- quote ,
81
- extensions = md_extensions ,
82
- extension_configs = md_config
83
- )
84
- link_soup = BeautifulSoup (html , 'html.parser' )
85
- if link_soup :
82
+ quote , extensions = md_extensions , extension_configs = md_config
83
+ )
84
+ link_soup = BeautifulSoup (html , "html.parser" )
85
+ if link_soup :
86
86
tooltip_template = (
87
- "<div class='citation'> <a href='"
88
- + str (new_uri )
89
- + "' class='link_citation'><i class='fas fa-link'></i> </a>"
90
- + str (link_soup ).replace (
87
+ "<div class='citation'> <a href='"
88
+ + str (new_uri )
89
+ + "' class='link_citation'><i class='fas fa-link'></i> </a>"
90
+ + str (link_soup ).replace (
91
91
'!<img class="wikilink' , '<img class="wikilink'
92
- )
93
- + ' </div>'
92
+ )
93
+ + " </div>"
94
94
)
95
95
new_soup = str (soup ).replace (str (link ), str (tooltip_template ))
96
- soup = BeautifulSoup (new_soup , ' html.parser' )
96
+ soup = BeautifulSoup (new_soup , " html.parser" )
97
97
return soup
98
- else :
99
- log = logging .getLogger (' mkdocs.plugins.' + __name__ )
98
+ else :
99
+ log = logging .getLogger (" mkdocs.plugins." + __name__ )
100
100
log .info (
101
- ' [EMBED FILE PLUGIN] CITATION NOT FOUND : ' +
102
- unquote (citation_part ) +
103
- ' for : ' +
104
- str (md_link_path ) +
105
- ' with link: ' +
106
- str (link ) +
107
- ' and new_uri: ' +
108
- str (new_uri ) +
109
- ' and quote: ' +
110
- str (quote )
111
- )
101
+ " [EMBED FILE PLUGIN] CITATION NOT FOUND : "
102
+ + unquote (citation_part )
103
+ + " for : "
104
+ + str (md_link_path )
105
+ + " with link: "
106
+ + str (link )
107
+ + " and new_uri: "
108
+ + str (new_uri )
109
+ + " and quote: "
110
+ + str (quote )
111
+ )
112
112
return tooltip_not_found (link , soup , msg )
113
113
114
114
115
- def tooltip_not_found (link , soup , msg ) -> BeautifulSoup :
115
+ def tooltip_not_found (link , soup , msg ) -> BeautifulSoup :
116
116
tooltip_template = (
117
- "<div class='citation'> <a class='link_citation'><i class='fas fa-link'></i> </a>"
118
- + '<p style="text-align: center; display: block"><i class="not_found">' +
119
- str (link ['alt' ]) + f'</i> { msg } </p>'
120
- + '</div>'
117
+ "<div class='citation'> <a class='link_citation'><i class='fas fa-link'></i> </a>"
118
+ + '<p style="text-align: center; display: block"><i class="not_found">'
119
+ + str (link ["alt" ])
120
+ + f"</i> { msg } </p>"
121
+ + "</div>"
121
122
)
122
123
new_soup = str (soup ).replace (str (link ), str (tooltip_template ))
123
- soup = BeautifulSoup (new_soup , ' html.parser' )
124
+ soup = BeautifulSoup (new_soup , " html.parser" )
124
125
return soup
125
126
126
127
127
- class EmbedFile (BasePlugin ) :
128
+ class EmbedFile (BasePlugin ):
128
129
config_scheme = (
129
- (' callouts' , config_options .Type (bool , default = False )),
130
- (' custom-attributes' , config_options .Type (str , default = '' )),
131
- (' language_message' , config_options .Type (str , default = ' file not exists' )),
132
- )
130
+ (" callouts" , config_options .Type (bool , default = False )),
131
+ (" custom-attributes" , config_options .Type (str , default = "" )),
132
+ (" language_message" , config_options .Type (str , default = " file not exists" )),
133
+ )
133
134
134
- def __init__ (self ) :
135
+ def __init__ (self ):
135
136
self .enabled = True
136
137
self .total_time = 0
137
138
138
- def on_post_page (self , output_content , page , config ) -> str :
139
- soup = BeautifulSoup (output_content , ' html.parser' )
140
- docs = Path (config [' docs_dir' ])
141
- md_link_path = ''
142
- callout = self .config [' callouts' ]
143
- language_message = self .config [' language_message' ]
139
+ def on_post_page (self , output_content , page , config ) -> str :
140
+ soup = BeautifulSoup (output_content , " html.parser" )
141
+ docs = Path (config [" docs_dir" ])
142
+ md_link_path = ""
143
+ callout = self .config [" callouts" ]
144
+ language_message = self .config [" language_message" ]
144
145
for link in soup .findAll (
145
- ' img' ,
146
- src = lambda src : src is not None
147
- and ' favicon' not in src
148
- and not src .endswith ((' png' , ' jpg' , ' jpeg' , ' gif' , ' svg' ))
149
- and not ' www' in src
150
- and not ' http' in src
151
- and not ' ://' in src ,
152
- ) :
153
- if len (link [' src' ]) > 0 :
154
- if link [' src' ].startswith ('./' ) :
146
+ " img" ,
147
+ src = lambda src : src is not None
148
+ and " favicon" not in src
149
+ and not src .endswith ((" png" , " jpg" , " jpeg" , " gif" , " svg" ))
150
+ and " www" not in src
151
+ and " http" not in src
152
+ and " ://" not in src ,
153
+ ) :
154
+ if len (link [" src" ]) > 0 :
155
+ if link [" src" ].startswith ("./" ) :
155
156
md_link_path = page .file .abs_src_path
156
- elif link [' src' ][0 ] == '.' : # relative links
157
- md_src = create_link (unquote (link [' src' ]))
157
+ elif link [" src" ][0 ] == "." : # relative links
158
+ md_src = create_link (unquote (link [" src" ]))
158
159
md_link_path = Path (
159
- os .path .dirname (page .file .abs_src_path ), md_src
160
- ).resolve ()
161
- md_link_path = re .sub (
162
- r'[\/\\]?#(.*)$' , '' , str (md_link_path )
163
- )
164
- if not os .path .isfile (md_link_path ) :
160
+ os .path .dirname (page .file .abs_src_path ), md_src
161
+ ).resolve ()
162
+ md_link_path = re .sub (r"[\/\\]?#(.*)$" , "" , str (md_link_path ))
163
+ if not os .path .isfile (md_link_path ):
165
164
md_link_path = search_file_in_documentation (
166
- md_link_path , docs
167
- )
165
+ md_link_path , docs , docs
166
+ )
168
167
169
- elif link ['src' ][0 ] == '/' :
170
- md_src_path = create_link (unquote (link ['src' ]))
171
- md_link_path = os .path .join (
172
- config ['docs_dir' ], md_src_path
173
- )
168
+ elif link ["src" ][0 ] == "/" :
169
+ md_src_path = create_link (unquote (link ["src" ]))
170
+ md_link_path = os .path .join (config ["docs_dir" ], md_src_path )
174
171
md_link_path = Path (unquote (md_link_path )).resolve ()
175
172
176
- elif link [' src' ][0 ] != '#' :
177
- md_src_path = create_link (unquote (link [' src' ]))
173
+ elif link [" src" ][0 ] != "#" :
174
+ md_src_path = create_link (unquote (link [" src" ]))
178
175
md_link_path = os .path .join (
179
- os .path .dirname (page .file .abs_src_path ), md_src_path
180
- )
181
- md_link_path = re .sub (
182
- r'/#(.*).md$' , '.md' , str (md_link_path )
183
- )
176
+ os .path .dirname (page .file .abs_src_path ), md_src_path
177
+ )
178
+ md_link_path = re .sub (r"/#(.*).md$" , ".md" , str (md_link_path ))
184
179
md_link_path = Path (unquote (md_link_path )).resolve ()
185
180
186
- else :
187
- md_src_path = create_link (unquote (link [' src' ]))
181
+ else :
182
+ md_src_path = create_link (unquote (link [" src" ]))
188
183
md_link_path = os .path .join (
189
- os .path .dirname (page .file .abs_src_path ), md_src_path
190
- )
184
+ os .path .dirname (page .file .abs_src_path ), md_src_path
185
+ )
191
186
md_link_path = Path (unquote (md_link_path )).resolve ()
192
- if md_link_path == 0 :
187
+ if md_link_path == 0 :
193
188
soup = tooltip_not_found (link , soup , language_message )
194
- if (md_link_path != '' or md_link_path ==
195
- 0 ) and len (link ['src' ]) > 0 :
196
- if '#' in link .get ('alt' , '' ) :
189
+ if (md_link_path != "" or md_link_path == 0 ) and len (link ["src" ]) > 0 :
190
+ if "#" in link .get ("alt" , "" ):
197
191
# heading
198
- citation_part = re .sub (' ^(.*)#' , '#' , link [' alt' ])
199
- elif '#' in link .get (' src' , '' ) :
200
- citation_part = re .sub (' ^(.*)#' , '#' , unquote (link [' src' ]))
201
- else :
202
- citation_part = link .get (' alt' , False )
203
- if citation_part :
192
+ citation_part = re .sub (" ^(.*)#" , "#" , link [" alt" ])
193
+ elif "#" in link .get (" src" , "" ) :
194
+ citation_part = re .sub (" ^(.*)#" , "#" , unquote (link [" src" ]))
195
+ else :
196
+ citation_part = link .get (" alt" , False )
197
+ if citation_part :
204
198
md_link_path = Path (str (md_link_path ))
205
199
206
- if os .path .isfile (md_link_path ) :
200
+ if os .path .isfile (md_link_path ):
207
201
soup = cite (
208
- md_link_path , link , soup ,
209
- citation_part , config , callout , self .config ['custom-attributes' ], language_message
210
- )
211
- else :
202
+ md_link_path ,
203
+ link ,
204
+ soup ,
205
+ citation_part ,
206
+ config ,
207
+ callout ,
208
+ self .config ["custom-attributes" ],
209
+ language_message ,
210
+ )
211
+ else :
212
212
link_found = search_file_in_documentation (
213
- md_link_path , docs
214
- )
215
- if link_found != 0 :
213
+ md_link_path , docs , docs
214
+ )
215
+ if link_found != 0 :
216
216
soup = cite (
217
- link_found , link , soup ,
218
- citation_part , config , callout , self .config ['custom-attributes' ], language_message
219
- )
217
+ link_found ,
218
+ link ,
219
+ soup ,
220
+ citation_part ,
221
+ config ,
222
+ callout ,
223
+ self .config ["custom-attributes" ],
224
+ language_message ,
225
+ )
220
226
return str (soup )
0 commit comments