|
8 | 8 | """ |
9 | 9 |
|
10 | 10 | import logging |
| 11 | +import os |
11 | 12 | from datetime import datetime |
12 | 13 | from fnmatch import fnmatch |
13 | 14 | from pathlib import Path |
@@ -60,6 +61,7 @@ class ContribsPlugin(BasePlugin): |
60 | 61 | ("contributors", c.Type(list, default=[])), |
61 | 62 | ("show_last_modified_time", c.Type(bool, default=True)), |
62 | 63 | ("show_contributors_title", c.Type(bool, default=False)), |
| 64 | + ("enabled_by_env", c.Type(str, default="")), |
63 | 65 | ("exclude", c.Type(list, default=[])), |
64 | 66 | ) |
65 | 67 |
|
@@ -182,7 +184,25 @@ def _is_ignored_page(self, page: Page) -> bool: |
182 | 184 | for ignored_pattern in self.config["exclude"] |
183 | 185 | ) |
184 | 186 |
|
| 187 | + def _is_enabled_by_env(self): |
| 188 | + """ |
| 189 | + Returns a value indicating if the plugin is enabled by env variable |
| 190 | + (default True if there is no env variable). |
| 191 | + If the user specified in the plugin configuration an env variable that controls |
| 192 | + if the plugin is enabled, read the env variable by that name and check if its |
| 193 | + value is (ci) {"1", "true"} to continue. |
| 194 | + """ |
| 195 | + enabled_by_env = self.config.get("enabled_by_env") |
| 196 | + if enabled_by_env: |
| 197 | + env_var = os.environ.get(enabled_by_env) |
| 198 | + if env_var is None: |
| 199 | + return False |
| 200 | + return env_var.lower() in {"1", "true"} |
| 201 | + return True # enabled since the user did not specify `enabled_by_env` setting |
| 202 | + |
185 | 203 | def on_page_markdown(self, markdown, *args, **kwargs): |
| 204 | + if not self._is_enabled_by_env(): |
| 205 | + return |
186 | 206 | if self._is_ignored_page(kwargs["page"]): |
187 | 207 | return markdown |
188 | 208 | try: |
|
0 commit comments