11import logging
2- from mkdocs_markmap .extension import MarkmapExtension
3- import re
42from pathlib import Path
3+ import re
54from typing import Dict , Tuple
65
76from bs4 import BeautifulSoup , ResultSet , Tag
8- from mkdocs .plugins import BasePlugin
9- from mkdocs .structure .pages import Page
7+
108from mkdocs .config .base import Config , load_config
119from mkdocs .config .config_options import Type as PluginType
10+ from mkdocs .plugins import BasePlugin
11+ from mkdocs .structure .pages import Page
12+ from mkdocs_markmap .extension import MarkmapExtension
1213
1314from .defaults import MARKMAP
1415from .utils import download
1516
1617
17- log = logging .getLogger (' mkdocs.markmap' )
18+ log = logging .getLogger (" mkdocs.markmap" )
1819
1920
20- STATICS_PATH : Path = Path (__file__ ).parent / 'static_files'
21- STYLE_PATH : Path = STATICS_PATH / 'mkdocs-markmap.css'
22- 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"
2326
2427
2528class MarkmapPlugin (BasePlugin ):
@@ -28,12 +31,12 @@ class MarkmapPlugin(BasePlugin):
2831 """
2932 config_scheme : Tuple [Tuple [str , PluginType ]] = (
3033 * (
31- (f' { name } _version' , PluginType (str , default = module .version ))
34+ (VERSION_KEY . format ( name = name ) , PluginType (str , default = module .version ))
3235 for name , module in MARKMAP .items ()
3336 ),
34- (' base_path' , PluginType (str , default = ' docs' )),
35- (' encoding' , PluginType (str , default = ' utf-8' )),
36- (' 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" )),
3740 )
3841
3942 def __init__ (self ):
@@ -44,96 +47,86 @@ def markmap(self) -> Dict[str, str]:
4447 """
4548 Provides all markmap libraries defined in mkdocs.yml (if any)
4649 """
47- if self ._markmap is None :
48- extra_javascript = self .config .get ('extra_javascript' , [])
50+ if self ._markmap is None :
4951 self ._markmap : Dict [str , str ] = {}
50- for uri in extra_javascript :
51- for name in MARKMAP .keys ():
52- if f'markmap-{ name } ' in uri .lower ():
53- self ._markmap [name ] = uri
54-
5552 for name , module in MARKMAP .items ():
56- if name not in self ._markmap :
57- 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 ) ])
5855
5956 return self ._markmap
6057
6158 def _load_scripts (self , soup : BeautifulSoup , script_base_url : str , js_path : Path ) -> None :
6259 for script_url in self .markmap .values ():
6360 if script_url .lower ().startswith ("http" ):
6461 try :
65- src : str = script_base_url + download (js_path , script_url )
62+ src : str = script_base_url + download (js_path , script_url , extname = ".js" )
6663 except Exception as e :
67- log .error (f' unable to download script: { script_url } ' )
64+ log .error (f" unable to download script: { script_url } " )
6865 src = script_url
6966
7067 else :
7168 log .info (f"static script detected: { script_url } " )
7269 src = script_url
73-
74- script : Tag = soup .new_tag (' script' , src = src , type = ' text/javascript' )
70+
71+ script : Tag = soup .new_tag (" script" , src = src , type = " text/javascript" )
7572 soup .head .append (script )
7673
7774 @staticmethod
7875 def _add_statics (soup : BeautifulSoup ):
7976 statics = (
80- (STYLE_PATH , ' style' , ' text/css' , ' head' ),
81- (SCRIPT_PATH , ' script' , ' text/javascript' , ' body' ),
77+ (STYLE_PATH , " style" , " text/css" , " head" ),
78+ (SCRIPT_PATH , " script" , " text/javascript" , " body" ),
8279 )
8380
8481 for path , tag_name , text_type , attribute in statics :
8582 tag : Tag = soup .new_tag (tag_name , type = text_type )
86- with open (path , 'r' ) as fp :
83+ with open (path , "r" ) as fp :
8784 tag .string = fp .read ()
88- getattr (soup , attribute ).append (tag )
85+ getattr (soup , attribute ).append (tag )
8986
9087 def on_config (self , config : Config ) -> Config :
91- config [' markdown_extensions' ].append (' markmap' )
92- config [' mdx_configs' ][ ' markmap' ] = {
88+ config [" markdown_extensions" ].append (" markmap" )
89+ config [" mdx_configs" ][ " markmap" ] = {
9390 key : value
94- for key , value in config [' plugins' ].get (' markmap' ).config .items ()
91+ for key , value in config [" plugins" ].get (" markmap" ).config .items ()
9592 if key in MarkmapExtension .config_defaults
9693 }
97- self .config [' extra_javascript' ] = config .get (' extra_javascript' , [])
94+ self .config [" extra_javascript" ] = config .get (" extra_javascript" , [])
9895
9996 return config
10097
10198 def on_post_page (self , html : str , page : Page , config : Config , ** kwargs ) -> str :
102- if not getattr (page , ' _found_markmap' , False ):
99+ if not getattr (page , " _found_markmap" , False ):
103100 log .info (f"no markmap found: { page .file .name } " )
104101 return html
105102
106- soup : BeautifulSoup = BeautifulSoup (html , ' html.parser' )
107- script_base_url : str = re .sub (r' [^/]+?/' , ' ../' , re .sub (r' /+?' , '/' , page .url )) + ' js/'
108- 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"
109106 self ._load_scripts (soup , script_base_url , js_path )
110107 self ._add_statics (soup )
111108
112109 return str (soup )
113110
114111 def on_page_content (self , html : str , page : Page , ** kwargs ) -> str :
115- soup : BeautifulSoup = BeautifulSoup (html , ' html.parser' )
116- markmaps : ResultSet = soup .find_all (class_ = ' language-markmap' )
117- 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 ))
118115
119116 for index , markmap in enumerate (markmaps ):
120117 markmap : Tag
121- tag_id : str = f' markmap-{ index } '
118+ tag_id : str = f" markmap-{ index } "
122119 pre : Tag
123120 code : Tag
124- if markmap .name == ' pre' :
121+ if markmap .name == " pre" :
125122 pre = markmap
126- code = markmap .findChild (' code' )
123+ code = markmap .findChild (" code" )
127124 else :
128125 pre = markmap .parent
129126 code = markmap
130- pre .name = 'div'
131- pre ['class' ] = pre .get ('class' , []) + ['mkdocs-markmap' ]
132- pre ['data-markdown' ] = code .text .replace ('\n ' , ' ' )
133- code .replaceWith (soup .new_tag (
134- 'svg' ,
135- id = tag_id ,
136- attrs = {'class' : 'markmap' },
137- ))
127+ pre .name = "div"
128+ pre ["class" ] = pre .get ("class" , []) + ["mkdocs-markmap" ]
129+ code .name = "script"
130+ code ["type" ] = "text/template"
138131
139132 return str (soup )
0 commit comments