1515from .utils import download
1616
1717
18- log = logging .getLogger (' mkdocs.markmap' )
18+ log = logging .getLogger (" mkdocs.markmap" )
1919
2020
21- STATICS_PATH : Path = Path (__file__ ).parent / 'static_files'
22- STYLE_PATH : Path = STATICS_PATH / 'mkdocs-markmap.css'
23- SCRIPT_PATH : Path = STATICS_PATH / 'mkdocs-markmap.js'
21+ STATICS_PATH : Path = Path (__file__ ).parent / "static_files"
22+ STYLE_PATH : Path = STATICS_PATH / "mkdocs-markmap.css"
23+ SCRIPT_PATH : Path = STATICS_PATH / "mkdocs-markmap.js"
24+
25+ VERSION_KEY = "{name}_version"
2426
2527
2628class MarkmapPlugin (BasePlugin ):
@@ -29,12 +31,12 @@ class MarkmapPlugin(BasePlugin):
2931 """
3032 config_scheme : Tuple [Tuple [str , PluginType ]] = (
3133 * (
32- (f' { name } _version' , PluginType (str , default = module .version ))
34+ (VERSION_KEY . format ( name = name ) , PluginType (str , default = module .version ))
3335 for name , module in MARKMAP .items ()
3436 ),
35- (' base_path' , PluginType (str , default = ' docs' )),
36- (' encoding' , PluginType (str , default = ' utf-8' )),
37- (' file_extension' , PluginType (str , default = ' .mm.md' )),
37+ (" base_path" , PluginType (str , default = " docs" )),
38+ (" encoding" , PluginType (str , default = " utf-8" )),
39+ (" file_extension" , PluginType (str , default = " .mm.md" )),
3840 )
3941
4042 def __init__ (self ):
@@ -48,83 +50,83 @@ def markmap(self) -> Dict[str, str]:
4850 if self ._markmap is None :
4951 self ._markmap : Dict [str , str ] = {}
5052 for name , module in MARKMAP .items ():
51- if self .config [f' { name } _version' ]:
52- self ._markmap [name ] = module .uri .format (self .config [f' { name } _version' ])
53+ if self .config [VERSION_KEY . format ( name = name ) ]:
54+ self ._markmap [name ] = module .uri .format (self .config [VERSION_KEY . format ( name = name ) ])
5355
5456 return self ._markmap
5557
5658 def _load_scripts (self , soup : BeautifulSoup , script_base_url : str , js_path : Path ) -> None :
5759 for script_url in self .markmap .values ():
5860 if script_url .lower ().startswith ("http" ):
5961 try :
60- src : str = script_base_url + download (js_path , script_url , extname = ' .js' )
62+ src : str = script_base_url + download (js_path , script_url , extname = " .js" )
6163 except Exception as e :
62- log .error (f' unable to download script: { script_url } ' )
64+ log .error (f" unable to download script: { script_url } " )
6365 src = script_url
6466
6567 else :
6668 log .info (f"static script detected: { script_url } " )
6769 src = script_url
6870
69- script : Tag = soup .new_tag (' script' , src = src , type = ' text/javascript' )
71+ script : Tag = soup .new_tag (" script" , src = src , type = " text/javascript" )
7072 soup .head .append (script )
7173
7274 @staticmethod
7375 def _add_statics (soup : BeautifulSoup ):
7476 statics = (
75- (STYLE_PATH , ' style' , ' text/css' , ' head' ),
76- (SCRIPT_PATH , ' script' , ' text/javascript' , ' body' ),
77+ (STYLE_PATH , " style" , " text/css" , " head" ),
78+ (SCRIPT_PATH , " script" , " text/javascript" , " body" ),
7779 )
7880
7981 for path , tag_name , text_type , attribute in statics :
8082 tag : Tag = soup .new_tag (tag_name , type = text_type )
81- with open (path , 'r' ) as fp :
83+ with open (path , "r" ) as fp :
8284 tag .string = fp .read ()
8385 getattr (soup , attribute ).append (tag )
8486
8587 def on_config (self , config : Config ) -> Config :
86- config [' markdown_extensions' ].append (' markmap' )
87- config [' mdx_configs' ][ ' markmap' ] = {
88+ config [" markdown_extensions" ].append (" markmap" )
89+ config [" mdx_configs" ][ " markmap" ] = {
8890 key : value
89- for key , value in config [' plugins' ].get (' markmap' ).config .items ()
91+ for key , value in config [" plugins" ].get (" markmap" ).config .items ()
9092 if key in MarkmapExtension .config_defaults
9193 }
92- self .config [' extra_javascript' ] = config .get (' extra_javascript' , [])
94+ self .config [" extra_javascript" ] = config .get (" extra_javascript" , [])
9395
9496 return config
9597
9698 def on_post_page (self , html : str , page : Page , config : Config , ** kwargs ) -> str :
97- if not getattr (page , ' _found_markmap' , False ):
99+ if not getattr (page , " _found_markmap" , False ):
98100 log .info (f"no markmap found: { page .file .name } " )
99101 return html
100102
101- soup : BeautifulSoup = BeautifulSoup (html , ' html.parser' )
102- script_base_url : str = re .sub (r' [^/]+?/' , ' ../' , re .sub (r' /+?' , '/' , page .url )) + ' js/'
103- js_path : Path = Path (config [' site_dir' ]) / 'js'
103+ soup : BeautifulSoup = BeautifulSoup (html , " html.parser" )
104+ script_base_url : str = re .sub (r" [^/]+?/" , " ../" , re .sub (r" /+?" , "/" , page .url )) + " js/"
105+ js_path : Path = Path (config [" site_dir" ]) / "js"
104106 self ._load_scripts (soup , script_base_url , js_path )
105107 self ._add_statics (soup )
106108
107109 return str (soup )
108110
109111 def on_page_content (self , html : str , page : Page , ** kwargs ) -> str :
110- soup : BeautifulSoup = BeautifulSoup (html , ' html.parser' )
111- markmaps : ResultSet = soup .find_all (class_ = ' language-markmap' )
112- setattr (page , ' _found_markmap' , any (markmaps ))
112+ soup : BeautifulSoup = BeautifulSoup (html , " html.parser" )
113+ markmaps : ResultSet = soup .find_all (class_ = " language-markmap" )
114+ setattr (page , " _found_markmap" , any (markmaps ))
113115
114116 for index , markmap in enumerate (markmaps ):
115117 markmap : Tag
116- tag_id : str = f' markmap-{ index } '
118+ tag_id : str = f" markmap-{ index } "
117119 pre : Tag
118120 code : Tag
119- if markmap .name == ' pre' :
121+ if markmap .name == " pre" :
120122 pre = markmap
121- code = markmap .findChild (' code' )
123+ code = markmap .findChild (" code" )
122124 else :
123125 pre = markmap .parent
124126 code = markmap
125- pre .name = ' div'
126- pre [' class' ] = pre .get (' class' , []) + [' mkdocs-markmap' ]
127- code .name = ' script'
128- code [' type' ] = ' text/template'
127+ pre .name = " div"
128+ pre [" class" ] = pre .get (" class" , []) + [" mkdocs-markmap" ]
129+ code .name = " script"
130+ code [" type" ] = " text/template"
129131
130132 return str (soup )
0 commit comments