Skip to content

Commit

Permalink
Raise error in tags plugin when tags file does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Mar 26, 2022
1 parent 6322ece commit dfba03f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
22 changes: 20 additions & 2 deletions material/plugins/tags/plugin.py
Expand Up @@ -18,10 +18,15 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import logging
import sys

from collections import defaultdict
from markdown.extensions.toc import slugify
from mkdocs import utils
from mkdocs.commands.build import DuplicateFilter
from mkdocs.config.config_options import Type
from mkdocs.exceptions import ConfigurationError
from mkdocs.plugins import BasePlugin

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -59,6 +64,11 @@ def on_nav(self, nav, files, **kwargs):
file = self.config.get("tags_file")
if file:
self.tags_file = files.get_file_from_path(file)
if not self.tags_file:
log.error(f"Configuration error: {file} doesn't exist.")
sys.exit()

# Add tags file to files
files.append(self.tags_file)

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

# Render the given tag and links to all pages with occurrences
def __render_tag_links(self, tag, pages):
content = ["## <span class=\"md-tag\">{}</span>".format(tag), ""]
content = [f"## <span class=\"md-tag\">{tag}</span>", ""]
for page in pages:
url = utils.get_relative_url(
page.file.src_path,
Expand All @@ -113,5 +123,13 @@ def __render_tag(self, tag):
return dict(name = tag)
else:
url = self.tags_file.url
url += "#{}".format(self.slugify(tag))
url += f"#{self.slugify(tag)}"
return dict(name = tag, url = url)

# -----------------------------------------------------------------------------
# Data
# -----------------------------------------------------------------------------

# Set up logging
log = logging.getLogger("mkdocs")
log.addFilter(DuplicateFilter())
22 changes: 20 additions & 2 deletions src/plugins/tags/plugin.py
Expand Up @@ -18,10 +18,15 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import logging
import sys

from collections import defaultdict
from markdown.extensions.toc import slugify
from mkdocs import utils
from mkdocs.commands.build import DuplicateFilter
from mkdocs.config.config_options import Type
from mkdocs.exceptions import ConfigurationError
from mkdocs.plugins import BasePlugin

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -59,6 +64,11 @@ def on_nav(self, nav, files, **kwargs):
file = self.config.get("tags_file")
if file:
self.tags_file = files.get_file_from_path(file)
if not self.tags_file:
log.error(f"Configuration error: {file} doesn't exist.")
sys.exit()

# Add tags file to files
files.append(self.tags_file)

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

# Render the given tag and links to all pages with occurrences
def __render_tag_links(self, tag, pages):
content = ["## <span class=\"md-tag\">{}</span>".format(tag), ""]
content = [f"## <span class=\"md-tag\">{tag}</span>", ""]
for page in pages:
url = utils.get_relative_url(
page.file.src_path,
Expand All @@ -113,5 +123,13 @@ def __render_tag(self, tag):
return dict(name = tag)
else:
url = self.tags_file.url
url += "#{}".format(self.slugify(tag))
url += f"#{self.slugify(tag)}"
return dict(name = tag, url = url)

# -----------------------------------------------------------------------------
# Data
# -----------------------------------------------------------------------------

# Set up logging
log = logging.getLogger("mkdocs")
log.addFilter(DuplicateFilter())

0 comments on commit dfba03f

Please sign in to comment.