11import re
22
3- from mkdocs .plugins import BasePlugin , get_plugin_logger
3+ from mkdocs .plugins import BasePlugin , get_plugin_logger
44from mkdocs .config import config_options
55from mkdocs .exceptions import ConfigurationError
66
1212
1313
1414class TableReaderPlugin (BasePlugin ):
15-
1615 config_scheme = (
1716 ("data_path" , config_options .Type (str , default = "." )),
1817 ("allow_missing_files" , config_options .Type (bool , default = False )),
19- ("select_readers" , config_options .ListOfItems (config_options .Choice (list (READERS .keys ())), default = list (READERS .keys ()))),
18+ (
19+ "select_readers" ,
20+ config_options .ListOfItems (
21+ config_options .Choice (list (READERS .keys ())),
22+ default = list (READERS .keys ()),
23+ ),
24+ ),
2025 )
2126
2227 def on_config (self , config , ** kwargs ):
@@ -30,34 +35,47 @@ def on_config(self, config, **kwargs):
3035 Config
3136 """
3237 if "search_page_directory" in self .config :
33- logger .warning ("[table-reader]: The 'search_page_directory' configuration option is deprecated, it will always be searched. Please remove it from your mkdocs.yml." )
38+ logger .warning (
39+ "[table-reader]: The 'search_page_directory' configuration option is deprecated, it will always be searched. Please remove it from your mkdocs.yml."
40+ )
3441 if "base_path" in self .config :
35- logger .warning ("[table-reader]: The 'base_path' configuration option is deprecated. Both the config_dir and docs_dir will be searched. Please remove it from your mkdocs.yml." )
36-
37- self .readers = {reader : READERS [reader ].set_config_context (mkdocs_config = config , plugin_config = self .config ) for reader in self .config .get ('select_readers' ) if reader in self .config .get ('select_readers' ,[])}
42+ logger .warning (
43+ "[table-reader]: The 'base_path' configuration option is deprecated. Both the config_dir and docs_dir will be searched. Please remove it from your mkdocs.yml."
44+ )
45+
46+ self .readers = {
47+ reader : READERS [reader ].set_config_context (
48+ mkdocs_config = config , plugin_config = self .config
49+ )
50+ for reader in self .config .get ("select_readers" )
51+ if reader in self .config .get ("select_readers" , [])
52+ }
3853
3954 plugins = [p for p in config .get ("plugins" )]
4055
4156 # Plugins required before table-reader
4257 for post_load_plugin in ["markdownextradata" ]:
4358 if post_load_plugin in plugins :
4459 if plugins .index ("table-reader" ) > plugins .index (post_load_plugin ):
45- raise ConfigurationError (f"[table-reader]: Incompatible plugin order: Define 'table-reader' before '{ post_load_plugin } ' in your mkdocs.yml." )
46-
60+ raise ConfigurationError (
61+ f"[table-reader]: Incompatible plugin order: Define 'table-reader' before '{ post_load_plugin } ' in your mkdocs.yml."
62+ )
63+
4764 # Plugins required after table-reader
4865 for post_load_plugin in ["macros" ]:
4966 if post_load_plugin in plugins :
5067 if plugins .index ("table-reader" ) < plugins .index (post_load_plugin ):
51- raise ConfigurationError (f"[table-reader]: Incompatible plugin order: Define 'table-reader' after '{ post_load_plugin } ' in your mkdocs.yml." )
68+ raise ConfigurationError (
69+ f"[table-reader]: Incompatible plugin order: Define 'table-reader' after '{ post_load_plugin } ' in your mkdocs.yml."
70+ )
5271
5372 if "macros" in config .plugins :
54- config .plugins [' macros' ].macros .update (self .readers )
55- config .plugins [' macros' ].variables [' macros' ].update (self .readers )
56- config .plugins [' macros' ].env .globals .update (self .readers )
73+ config .plugins [" macros" ].macros .update (self .readers )
74+ config .plugins [" macros" ].variables [" macros" ].update (self .readers )
75+ config .plugins [" macros" ].env .globals .update (self .readers )
5776 self .external_jinja_engine = True
5877 else :
5978 self .external_jinja_engine = False
60-
6179
6280 def on_pre_page (self , page , config , ** kwargs ):
6381 """
@@ -102,26 +120,25 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
102120 for reader in self .readers :
103121 function = self .readers [reader ]
104122 # Regex pattern for tags like {{ read_csv(..) }}
105- # match group 0: to extract any leading whitespace
123+ # match group 0: to extract any leading whitespace
106124 # match group 1: to extract the arguments (positional and keywords)
107125 tag_pattern = re .compile (
108126 r"( *)\{\{\s+%s\((.+)\)\s+\}\}" % reader , flags = re .IGNORECASE
109127 )
110128 matches = re .findall (tag_pattern , markdown )
111-
112- for result in matches :
113129
130+ for result in matches :
114131 # Deal with indentation
115132 # So we can fix inserting tables.
116133 # f.e. relevant when used inside content tabs
117134 leading_spaces = result [0 ]
118135
119136 # Safely parse the arguments
120137 pd_args , pd_kwargs = parse_argkwarg (result [1 ])
121-
138+
122139 # Load the table
123140 markdown_table = function (* pd_args , ** pd_kwargs )
124-
141+
125142 # Insert markdown table
126143 # By replacing only the first occurrence of the regex pattern
127144 # You might insert multiple CSVs with a single reader like read_csv
@@ -131,5 +148,3 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
131148 markdown = tag_pattern .sub (markdown_table , markdown , count = 1 )
132149
133150 return markdown
134-
135-
0 commit comments