88"""
99
1010import logging
11+ import os
1112from datetime import datetime
1213from fnmatch import fnmatch
1314from pathlib import Path
@@ -60,6 +61,7 @@ class ContribsPlugin(BasePlugin):
6061 ("contributors" , c .Type (list , default = [])),
6162 ("show_last_modified_time" , c .Type (bool , default = True )),
6263 ("show_contributors_title" , c .Type (bool , default = False )),
64+ ("enabled_by_env" , c .Type (str , default = "" )),
6365 ("exclude" , c .Type (list , default = [])),
6466 )
6567
@@ -156,6 +158,11 @@ def _get_last_commit_date(self, page_file: File) -> datetime:
156158 def _set_contributors (self , markdown : str , page : Page ) -> str :
157159 page_file = page .file
158160 last_commit_date = self ._get_last_commit_date (page_file )
161+
162+ if last_commit_date .replace (tzinfo = None ) == datetime .min :
163+ # The page was never committed, skip
164+ return markdown
165+
159166 contributors = self ._get_contributors (page_file )
160167 return (
161168 markdown
@@ -182,7 +189,25 @@ def _is_ignored_page(self, page: Page) -> bool:
182189 for ignored_pattern in self .config ["exclude" ]
183190 )
184191
192+ def _is_enabled_by_env (self ):
193+ """
194+ Returns a value indicating if the plugin is enabled by env variable
195+ (default True if there is no env variable).
196+ If the user specified in the plugin configuration an env variable that controls
197+ if the plugin is enabled, read the env variable by that name and check if its
198+ value is (ci) {"1", "true"} to continue.
199+ """
200+ enabled_by_env = self .config .get ("enabled_by_env" )
201+ if enabled_by_env :
202+ env_var = os .environ .get (enabled_by_env )
203+ if env_var is None :
204+ return False
205+ return env_var .lower () in {"1" , "true" }
206+ return True # enabled since the user did not specify `enabled_by_env` setting
207+
185208 def on_page_markdown (self , markdown , * args , ** kwargs ):
209+ if not self ._is_enabled_by_env ():
210+ return
186211 if self ._is_ignored_page (kwargs ["page" ]):
187212 return markdown
188213 try :
0 commit comments