Skip to content

Commit dfba03f

Browse files
committed
Raise error in tags plugin when tags file does not exist
1 parent 6322ece commit dfba03f

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

material/plugins/tags/plugin.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919
# IN THE SOFTWARE.
2020

21+
import logging
22+
import sys
23+
2124
from collections import defaultdict
2225
from markdown.extensions.toc import slugify
2326
from mkdocs import utils
27+
from mkdocs.commands.build import DuplicateFilter
2428
from mkdocs.config.config_options import Type
29+
from mkdocs.exceptions import ConfigurationError
2530
from mkdocs.plugins import BasePlugin
2631

2732
# -----------------------------------------------------------------------------
@@ -59,6 +64,11 @@ def on_nav(self, nav, files, **kwargs):
5964
file = self.config.get("tags_file")
6065
if file:
6166
self.tags_file = files.get_file_from_path(file)
67+
if not self.tags_file:
68+
log.error(f"Configuration error: {file} doesn't exist.")
69+
sys.exit()
70+
71+
# Add tags file to files
6272
files.append(self.tags_file)
6373

6474
# Build and render tags index page
@@ -93,7 +103,7 @@ def __render_tag_index(self, markdown):
93103

94104
# Render the given tag and links to all pages with occurrences
95105
def __render_tag_links(self, tag, pages):
96-
content = ["## <span class=\"md-tag\">{}</span>".format(tag), ""]
106+
content = [f"## <span class=\"md-tag\">{tag}</span>", ""]
97107
for page in pages:
98108
url = utils.get_relative_url(
99109
page.file.src_path,
@@ -113,5 +123,13 @@ def __render_tag(self, tag):
113123
return dict(name = tag)
114124
else:
115125
url = self.tags_file.url
116-
url += "#{}".format(self.slugify(tag))
126+
url += f"#{self.slugify(tag)}"
117127
return dict(name = tag, url = url)
128+
129+
# -----------------------------------------------------------------------------
130+
# Data
131+
# -----------------------------------------------------------------------------
132+
133+
# Set up logging
134+
log = logging.getLogger("mkdocs")
135+
log.addFilter(DuplicateFilter())

src/plugins/tags/plugin.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919
# IN THE SOFTWARE.
2020

21+
import logging
22+
import sys
23+
2124
from collections import defaultdict
2225
from markdown.extensions.toc import slugify
2326
from mkdocs import utils
27+
from mkdocs.commands.build import DuplicateFilter
2428
from mkdocs.config.config_options import Type
29+
from mkdocs.exceptions import ConfigurationError
2530
from mkdocs.plugins import BasePlugin
2631

2732
# -----------------------------------------------------------------------------
@@ -59,6 +64,11 @@ def on_nav(self, nav, files, **kwargs):
5964
file = self.config.get("tags_file")
6065
if file:
6166
self.tags_file = files.get_file_from_path(file)
67+
if not self.tags_file:
68+
log.error(f"Configuration error: {file} doesn't exist.")
69+
sys.exit()
70+
71+
# Add tags file to files
6272
files.append(self.tags_file)
6373

6474
# Build and render tags index page
@@ -93,7 +103,7 @@ def __render_tag_index(self, markdown):
93103

94104
# Render the given tag and links to all pages with occurrences
95105
def __render_tag_links(self, tag, pages):
96-
content = ["## <span class=\"md-tag\">{}</span>".format(tag), ""]
106+
content = [f"## <span class=\"md-tag\">{tag}</span>", ""]
97107
for page in pages:
98108
url = utils.get_relative_url(
99109
page.file.src_path,
@@ -113,5 +123,13 @@ def __render_tag(self, tag):
113123
return dict(name = tag)
114124
else:
115125
url = self.tags_file.url
116-
url += "#{}".format(self.slugify(tag))
126+
url += f"#{self.slugify(tag)}"
117127
return dict(name = tag, url = url)
128+
129+
# -----------------------------------------------------------------------------
130+
# Data
131+
# -----------------------------------------------------------------------------
132+
133+
# Set up logging
134+
log = logging.getLogger("mkdocs")
135+
log.addFilter(DuplicateFilter())

0 commit comments

Comments
 (0)