Skip to content

Commit

Permalink
Fix link validation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyLuten committed Feb 1, 2024
1 parent d17c087 commit b53e260
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ You may use the optional `verbose` option to print more information about which

**Default:** `categories`

A string used to both serve as the URL base for the category pages as well as the navigation section that's automatically generated. Since this sting is used in the generated category URL, please ensure that it only contains URL friendly characters.
A string that's used to both serve as the URL base for the category pages as well as the navigation section that's automatically generated. Since this string is used in the generated category URL, please ensure that it only contains URL-friendly characters.

### `section_title`

Expand Down Expand Up @@ -123,6 +123,10 @@ The page identified did not contain a valid categories configuration object. Ple

## Changelog

### 0.5.0

Fixes many build info warnings about absolute paths due to MkDocs' improved validation. Made the links that are generated more correct.

### 0.4.0

Fixes sorting categories, child pages, etc. containing numbers by using natural sorting from the `natsort` package rather than the default sorting function.
Expand Down
32 changes: 18 additions & 14 deletions categories/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class CategoriesPlugin(BasePlugin):
('base_name', config_options.Type(str, default='categories')),
('section_title', config_options.Type(str, default='Categories')),
('category_separator', config_options.Type(str, default='|')),
('debug_fs', config_options.Type(bool, default=False)),
)
log: Logger = getLogger(f'mkdocs.plugins.{__name__}')
categories: dict = {}
Expand All @@ -63,6 +64,9 @@ def on_config(self, _):

def clean_temp_dir(self):
"""Remove the temporary directory after execution."""
if self.config['debug_fs']:
self.log.info("Debugging: Not removing temporary directory.")
return
shutil.rmtree(self.cat_path)

def on_nav(self, nav, **_):
Expand All @@ -79,7 +83,7 @@ def on_nav(self, nav, **_):
break
return nav

def on_build_error(self, _error):
def on_build_error(self, **_):
"""Executed after the build has failed."""
self.categories.clear()
self.pages.clear()
Expand All @@ -94,15 +98,15 @@ def on_post_build(self, **_):

def on_page_markdown(self, markdown: str, *, page: Page, **_):
"""Appends the category links section for a page to the markdown."""
relative_url = get_relative_url(str(self.cat_path), page.file.url)
if page.file.url not in self.pages:
relative_url = get_relative_url(str(self.cat_path), page.file.src_uri)
if page.file.src_uri not in self.pages:
return markdown
links = list(map(
lambda c: (
f"- [{self.categories[c]['name']}]"
f"({relative_url}/{self.categories[c]['slug']}/)"
f"({relative_url}/{self.categories[c]['slug']}.md)"
),
natsorted(self.pages[page.file.url])
natsorted(self.pages[page.file.src_uri])
))
return (
markdown +
Expand Down Expand Up @@ -143,7 +147,7 @@ def register_page(self, cat_path: list[str], page_url: str, page_title: str) ->
category = self.ensure_path(cat_path)
category['pages'].append({
'title': page_title,
'url': f"{page_url}{'' if page_url.endswith('/') else '/'}"
'url': page_url
})

# Each page also stores which categories it belongs to
Expand All @@ -161,21 +165,21 @@ def define_categories(self, files) -> None:
if not isinstance(meta_data['categories'], list):
self.log.error(
'The categories object at %s was not a list, but %s',
str(file.url),
str(file.src_path),
type(meta_data['categories'].__name__)
)
continue
for category in meta_data['categories']:
self.register_page(
str(category).split(self.config['category_separator']),
str(file.url),
str(file.src_path),
get_page_title(source, meta_data)
)

def generate_index(self, config) -> File:
"""Generates a categories index page if the option is set."""
joined = "\n".join(map(
lambda c: f"- [{c['name']}]({str(c['slug'])}/) ({len(c['pages'])})",
lambda c: f"- [{c['name']}](./{str(c['slug'])}.md) ({len(c['pages'])})",
natsorted(self.categories.values(), key=lambda c: c['name'])
))
with open(self.cat_path / 'index.md', mode="w", encoding='utf-8') as file:
Expand All @@ -197,15 +201,15 @@ def render_all_categories_link(self) -> str:
"""Generates a link to the categories index page if the option is set"""
return (
'' if not self.config['generate_index']
else "[All Categories](../)\n\n"
else "[All Categories](./index.md)\n\n"
)

def render_child_categories(self, category: dict) -> tuple[bool, str]:
"""Renders the child categories of a category."""
if len(category['children']) <= 0:
return False, None
joined = "\n".join(map(
lambda c: f"- [{self.categories[c]['name']}](../{self.categories[c]['slug']}/)",
lambda c: f"- [{self.categories[c]['name']}](./{self.categories[c]['slug']}.md)",
natsorted(category['children'])
))
return True, joined
Expand All @@ -215,7 +219,7 @@ def render_category_pages(self, category: dict) -> tuple[bool, str]:
if len(category['pages']) <= 0:
return False, None
joined = "\n".join(map(
lambda p: f"- [{p['title']}](../../{p['url']})",
lambda p: f"- [{p['title']}](../{p['url']})",
natsorted(category['pages'], key=lambda p: p['title'])
))
return True, joined
Expand All @@ -225,7 +229,7 @@ def render_parent_category(self, category: dict) -> str:
if not category['parent']:
return None
parent = self.categories[category['parent']]
return f"[{parent['name']}](../{parent['slug']})"
return f"[{parent['name']}](./{parent['slug']}.md)"

def on_files(self, files, *, config, **_):
"""When MkDocs loads its files, load any defined categories."""
Expand All @@ -240,7 +244,7 @@ def on_files(self, files, *, config, **_):
file.write(''.join([
f"# Category: {category['name']}\n\n",
(f"Parent category: {parent}\n\n" if parent else ''),
(f"##Subcategories\n\n{children}\n\n" if has_children else ''),
(f"## Subcategories\n\n{children}\n\n" if has_children else ''),
f"## Pages in category \"{category['name']}\"\n\n",
f"{pages if has_pages else 'This category has no pages.'}\n\n",
self.render_all_categories_link(),
Expand Down

0 comments on commit b53e260

Please sign in to comment.