| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| from __future__ import annotations |
|
|
| import logging |
| import os |
| import posixpath |
| import yaml |
|
|
| from babel.dates import format_date, format_datetime |
| from copy import copy |
| from datetime import datetime, timezone |
| from jinja2 import pass_context |
| from jinja2.runtime import Context |
| from mkdocs.config.defaults import MkDocsConfig |
| from mkdocs.exceptions import PluginError |
| from mkdocs.plugins import BasePlugin, event_priority |
| from mkdocs.structure import StructureItem |
| from mkdocs.structure.files import File, Files, InclusionLevel |
| from mkdocs.structure.nav import Link, Navigation, Section |
| from mkdocs.structure.pages import Page |
| from mkdocs.structure.toc import AnchorLink, TableOfContents |
| from mkdocs.utils import copy_file, get_relative_url |
| from paginate import Page as Pagination |
| from shutil import rmtree |
| from tempfile import mkdtemp |
| from urllib.parse import urlparse |
| from yaml import SafeLoader |
|
|
| from . import view_name |
| from .author import Author, Authors |
| from .config import BlogConfig |
| from .readtime import readtime |
| from .structure import ( |
| Archive, Category, Profile, |
| Excerpt, Post, View, |
| Reference |
| ) |
|
|
| |
| |
| |
|
|
| |
| class BlogPlugin(BasePlugin[BlogConfig]): |
| supports_multiple_instances = True |
|
|
| |
| def __init__(self, *args, **kwargs): |
| super().__init__(*args, **kwargs) |
|
|
| |
| self.is_serve = False |
| self.is_dirty = False |
|
|
| |
| self.temp_dir = mkdtemp() |
|
|
| |
| def on_startup(self, *, command, dirty): |
| self.is_serve = command == "serve" |
| self.is_dirty = dirty |
|
|
| |
| def on_config(self, config): |
| if not self.config.enabled: |
| return |
|
|
| |
| self.blog: View |
|
|
| |
| if self.config.authors: |
| self.authors = self._resolve_authors(config) |
|
|
| |
| |
| if self.is_serve and self.config.draft_on_serve: |
| self.config.draft = True |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| @event_priority(-50) |
| def on_files(self, files, *, config): |
| if not self.config.enabled: |
| return |
|
|
| |
| root = posixpath.normpath(self.config.blog_dir) |
| site = config.site_dir |
|
|
| |
| path = self.config.post_dir.format(blog = root) |
| path = posixpath.normpath(path) |
|
|
| |
| for file in files.media_files(): |
| if not file.src_uri.startswith(path): |
| continue |
|
|
| |
| |
| file.dest_uri = file.dest_uri.replace(path, root) |
| file.abs_dest_path = os.path.join(site, file.dest_path) |
| file.url = file.url.replace(path, root) |
|
|
| |
| |
| self.blog = self._resolve(files, config) |
| self.blog.posts = sorted( |
| self._resolve_posts(files, config), |
| key = lambda post: ( |
| post.config.pin, |
| post.config.date.created |
| ), |
| reverse = True |
| ) |
|
|
| |
| if self.config.archive: |
| views = self._generate_archive(config, files) |
| self.blog.views.extend(views) |
|
|
| |
| if self.config.categories: |
| views = self._generate_categories(config, files) |
|
|
| |
| |
| |
| |
| self.blog.views.extend(sorted( |
| sorted(views, key = view_name), |
| key = self.config.categories_sort_by, |
| reverse = self.config.categories_sort_reverse |
| )) |
|
|
| |
| if self.config.authors_profiles: |
| views = self._generate_profiles(config, files) |
| self.blog.views.extend(views) |
|
|
| |
| for view in self._resolve_views(self.blog): |
| if self._config_pagination(view): |
| for page in self._generate_pages(view, config, files): |
| view.pages.append(page) |
|
|
| |
| self.blog.file.inclusion = InclusionLevel.INCLUDED |
|
|
| |
| |
| |
| |
| @event_priority(-50) |
| def on_nav(self, nav, *, config, files): |
| if not self.config.enabled: |
| return |
|
|
| |
| |
| |
| |
| |
| |
| if not self.blog.parent and self.config.blog_dir != ".": |
| self.blog.file.inclusion = InclusionLevel.NOT_IN_NAV |
|
|
| |
| |
| self._attach(self.blog, [None, *reversed(self.blog.posts), None]) |
| for post in self.blog.posts: |
| post.file.inclusion = InclusionLevel.NOT_IN_NAV |
|
|
| |
| for view in self._resolve_views(self.blog): |
| view.file.inclusion = self.blog.file.inclusion |
| for page in view.pages: |
| page.file.inclusion = self.blog.file.inclusion |
|
|
| |
| if self.config.archive: |
| title = self._translate(self.config.archive_name, config) |
| views = [_ for _ in self.blog.views if isinstance(_, Archive)] |
|
|
| |
| if self.blog.file.inclusion.is_in_nav(): |
| self._attach_to(self.blog, Section(title, views), nav) |
|
|
| |
| if self.config.categories: |
| title = self._translate(self.config.categories_name, config) |
| views = [_ for _ in self.blog.views if isinstance(_, Category)] |
|
|
| |
| if self.blog.file.inclusion.is_in_nav() and views: |
| self._attach_to(self.blog, Section(title, views), nav) |
|
|
| |
| if self.config.authors_profiles: |
| title = self._translate(self.config.authors_profiles_name, config) |
| views = [_ for _ in self.blog.views if isinstance(_, Profile)] |
|
|
| |
| if self.blog.file.inclusion.is_in_nav() and views: |
| self._attach_to(self.blog, Section(title, views), nav) |
|
|
| |
| for view in self._resolve_views(self.blog): |
| if self._config_pagination(view): |
| for at in range(1, len(view.pages)): |
| self._attach_at(view.parent, view, view.pages[at]) |
|
|
| |
| |
| |
| |
| @event_priority(-50) |
| def on_page_markdown(self, markdown, *, page, config, files): |
| if not self.config.enabled: |
| return |
|
|
| |
| |
| if page not in self.blog.posts: |
| if not self._config_pagination(page): |
| return |
|
|
| |
| |
| if not self.config.pagination_keep_content: |
| view = self._resolve_original(page) |
| if view in self._resolve_views(self.blog): |
|
|
| |
| |
| |
| |
| assert isinstance(view, View) |
| if view != page: |
| name = view._title_from_render or view.title |
| return f"# {name}" |
|
|
| |
| return |
|
|
| |
| if self.config.authors: |
| for id in page.config.authors: |
| if id not in self.authors: |
| raise PluginError(f"Couldn't find author '{id}'") |
|
|
| |
| page.authors.append(self.authors[id]) |
|
|
| |
| separator = self.config.post_excerpt_separator |
| max_authors = self.config.post_excerpt_max_authors |
| max_categories = self.config.post_excerpt_max_categories |
|
|
| |
| |
| |
| |
| if separator not in page.markdown: |
| if self.config.post_excerpt == "required": |
| docs = os.path.relpath(config.docs_dir) |
| path = os.path.relpath(page.file.abs_src_path, docs) |
| raise PluginError( |
| f"Couldn't find '{separator}' in post '{path}' in '{docs}'" |
| ) |
|
|
| |
| |
| page.excerpt = Excerpt(page, config, files) |
| page.excerpt.authors = page.authors[:max_authors] |
| page.excerpt.categories = page.categories[:max_categories] |
|
|
| |
| def on_page_content(self, html, *, page, config, files): |
| if not self.config.enabled: |
| return |
|
|
| |
| |
| if page not in self.blog.posts: |
| return |
|
|
| |
| if self.config.post_readtime: |
| words_per_minute = self.config.post_readtime_words_per_minute |
| if not page.config.readtime: |
| page.config.readtime = readtime(html, words_per_minute) |
|
|
| |
| def on_env(self, env, *, config, files): |
| if not self.config.enabled: |
| return |
|
|
| |
| for post in self.blog.posts: |
| self._generate_links(post, config, files) |
|
|
| |
| def date_filter(date: datetime): |
| return self._format_date_for_post(date, config) |
|
|
| |
| |
| url_filter = env.filters["url"] |
|
|
| |
| |
| @pass_context |
| def url_filter_with_pagination(context: Context, url: str | None): |
| page = context["page"] |
|
|
| |
| |
| if isinstance(page, View): |
| view = self._resolve_original(page) |
| if page.url == url: |
| url = view.url |
|
|
| |
| return url_filter(context, url) |
|
|
| |
| env.filters["date"] = date_filter |
| env.filters["url"] = url_filter_with_pagination |
|
|
| |
| |
| |
| |
| @event_priority(-100) |
| def on_page_context(self, context, *, page, config, nav): |
| if not self.config.enabled: |
| return |
|
|
| |
| |
| view = self._resolve_original(page) |
| if view not in self._resolve_views(self.blog): |
| return |
|
|
| |
| posts, pagination = self._render(page) |
|
|
| |
| def pager(args: object): |
| return pagination.pager( |
| format = self.config.pagination_format, |
| show_if_single_page = self.config.pagination_if_single_page, |
| **args |
| ) |
|
|
| |
| context["posts"] = posts |
| context["pagination"] = pager if pagination else None |
|
|
| |
| def on_shutdown(self): |
| rmtree(self.temp_dir) |
|
|
| |
|
|
| |
| def _is_excluded(self, post: Post): |
| if self.config.draft: |
| return False |
|
|
| |
| |
| |
| |
| if not isinstance(post.config.draft, bool): |
| if self.config.draft_if_future_date: |
| return post.config.date.created > datetime.now(timezone.utc) |
|
|
| |
| return bool(post.config.draft) |
|
|
| |
|
|
| |
| |
| def _resolve(self, files: Files, config: MkDocsConfig): |
| path = os.path.join(self.config.blog_dir, "index.md") |
| path = os.path.normpath(path) |
|
|
| |
| |
| docs = os.path.relpath(config.docs_dir) |
| name = os.path.join(docs, path) |
| if not os.path.isfile(name): |
| file = self._path_to_file(path, config, temp = False) |
| files.append(file) |
|
|
| |
| self._save_to_file(file.abs_src_path, "# Blog\n\n") |
|
|
| |
| file = files.get_file_from_path(path) |
| return View(None, file, config) |
|
|
| |
| |
| def _resolve_post(self, file: File, config: MkDocsConfig): |
| post = Post(file, config) |
|
|
| |
| path = self._format_path_for_post(post, config) |
| temp = self._path_to_file(path, config, temp = False) |
|
|
| |
| file.dest_uri = temp.dest_uri |
| file.abs_dest_path = temp.abs_dest_path |
| file.url = temp.url |
|
|
| |
| post._set_canonical_url(config.site_url) |
| return post |
|
|
| |
| |
| def _resolve_posts(self, files: Files, config: MkDocsConfig): |
| path = self.config.post_dir.format(blog = self.config.blog_dir) |
| path = os.path.normpath(path) |
|
|
| |
| docs = os.path.relpath(config.docs_dir) |
| name = os.path.join(docs, path) |
| if not os.path.isdir(name): |
| os.makedirs(name, exist_ok = True) |
|
|
| |
| for file in files.documentation_pages(): |
| if not file.src_path.startswith(path): |
| continue |
|
|
| |
| file.inclusion = InclusionLevel.EXCLUDED |
|
|
| |
| |
| |
| |
| post = self._resolve_post(file, config) |
| if not self._is_excluded(post): |
| yield post |
|
|
| |
| |
| def _resolve_authors(self, config: MkDocsConfig): |
| path = self.config.authors_file.format(blog = self.config.blog_dir) |
| path = os.path.normpath(path) |
|
|
| |
| docs = os.path.relpath(config.docs_dir) |
| file = os.path.join(docs, path) |
|
|
| |
| config: Authors = Authors() |
| if not os.path.isfile(file): |
| return config.authors |
|
|
| |
| with open(file, encoding = "utf-8-sig") as f: |
| config.config_file_path = os.path.abspath(file) |
| try: |
| config.load_dict(yaml.load(f, SafeLoader) or {}) |
|
|
| |
| |
| except Exception as e: |
| raise PluginError( |
| f"Error reading authors file '{path}' in '{docs}':\n" |
| f"{e}" |
| ) |
|
|
| |
| errors, warnings = config.validate() |
| for _, w in warnings: |
| log.warning(w) |
| for _, e in errors: |
| raise PluginError( |
| f"Error reading authors file '{path}' in '{docs}':\n" |
| f"{e}" |
| ) |
|
|
| |
| return config.authors |
|
|
| |
| def _resolve_views(self, view: View): |
| yield view |
|
|
| |
| for page in view.views: |
| for next in self._resolve_views(page): |
| assert isinstance(next, View) |
| yield next |
|
|
| |
| def _resolve_siblings(self, item: StructureItem, nav: Navigation): |
| if isinstance(item.parent, Section): |
| return item.parent.children |
| else: |
| return nav.items |
|
|
| |
| def _resolve_original(self, page: Page): |
| if isinstance(page, View) and page.pages: |
| return page.pages[0] |
| else: |
| return page |
|
|
| |
|
|
| |
| |
| def _generate_archive(self, config: MkDocsConfig, files: Files): |
| for post in self.blog.posts: |
| date = post.config.date.created |
|
|
| |
| name = self._format_date_for_archive(date, config) |
| path = self._format_path_for_archive(post, config) |
|
|
| |
| file = files.get_file_from_path(path) |
| if not file: |
| file = self._path_to_file(path, config) |
| files.append(file) |
|
|
| |
| self._save_to_file(file.abs_src_path, f"# {name}") |
|
|
| |
| file.inclusion = InclusionLevel.EXCLUDED |
|
|
| |
| if not isinstance(file.page, Archive): |
| yield Archive(name, file, config) |
|
|
| |
| assert isinstance(file.page, Archive) |
| file.page.posts.append(post) |
|
|
| |
| |
| def _generate_categories(self, config: MkDocsConfig, files: Files): |
| for post in self.blog.posts: |
| for name in post.config.categories: |
| path = self._format_path_for_category(name) |
|
|
| |
| categories = self.config.categories_allowed or [name] |
| if name not in categories: |
| docs = os.path.relpath(config.docs_dir) |
| path = os.path.relpath(post.file.abs_src_path, docs) |
| raise PluginError( |
| f"Error reading categories of post '{path}' in " |
| f"'{docs}': category '{name}' not in allow list" |
| ) |
|
|
| |
| file = files.get_file_from_path(path) |
| if not file: |
| file = self._path_to_file(path, config) |
| files.append(file) |
|
|
| |
| self._save_to_file(file.abs_src_path, f"# {name}") |
|
|
| |
| file.inclusion = InclusionLevel.EXCLUDED |
|
|
| |
| if not isinstance(file.page, Category): |
| yield Category(name, file, config) |
|
|
| |
| assert isinstance(file.page, Category) |
| file.page.posts.append(post) |
| post.categories.append(file.page) |
|
|
| |
| |
| def _generate_profiles(self, config: MkDocsConfig, files: Files): |
| for post in self.blog.posts: |
| for id in post.config.authors: |
| author = self.authors[id] |
| path = self._format_path_for_profile(id, author) |
|
|
| |
| file = files.get_file_from_path(path) |
| if not file: |
| file = self._path_to_file(path, config) |
| files.append(file) |
|
|
| |
| self._save_to_file(file.abs_src_path, f"# {author.name}") |
|
|
| |
| |
| file.inclusion = InclusionLevel.EXCLUDED |
| if not author.url: |
| author.url = file.url |
|
|
| |
| if not isinstance(file.page, Profile): |
| yield Profile(author.name, file, config) |
|
|
| |
| assert isinstance(file.page, Profile) |
| file.page.posts.append(post) |
|
|
| |
| |
| def _generate_pages(self, view: View, config: MkDocsConfig, files: Files): |
| yield view |
|
|
| |
| |
| step = self._config_pagination_per_page(view) |
| for at in range(step, len(view.posts), step): |
| path = self._format_path_for_pagination(view, 1 + at // step) |
|
|
| |
| file = files.get_file_from_path(path) |
| if not file: |
| file = self._path_to_file(path, config) |
| files.append(file) |
|
|
| |
| copy_file(view.file.abs_src_path, file.abs_src_path) |
|
|
| |
| file.inclusion = InclusionLevel.EXCLUDED |
|
|
| |
| if not isinstance(file.page, View): |
| yield view.__class__(None, file, config) |
|
|
| |
| assert isinstance(file.page, View) |
| file.page.pages = view.pages |
| file.page.posts = view.posts |
|
|
| |
| |
| def _generate_links(self, post: Post, config: MkDocsConfig, files: Files): |
| if not post.config.links: |
| return |
|
|
| |
| docs = os.path.relpath(config.docs_dir) |
| path = os.path.relpath(post.file.abs_src_path, docs) |
|
|
| |
| |
| for link in _find_links(post.config.links.items): |
| url = urlparse(link.url) |
| if url.scheme: |
| continue |
|
|
| |
| |
| |
| |
| file = files.get_file_from_path(url.path) |
| if not file: |
| log.warning( |
| f"Error reading metadata of post '{path}' in '{docs}':\n" |
| f"Couldn't find file for link '{url.path}'" |
| ) |
| continue |
|
|
| |
| |
| if not isinstance(file.page, Page): |
| link.url = file.url |
| continue |
|
|
| |
| link.__class__ = Reference |
| assert isinstance(link, Reference) |
|
|
| |
| link.title = link.title or file.page.title |
| link.url = file.page.url |
| link.meta = copy(file.page.meta) |
|
|
| |
| |
| if not url.fragment: |
| continue |
|
|
| |
| |
| |
| |
| if self.is_dirty: |
| continue |
|
|
| |
| |
| anchor = _find_anchor(file.page.toc, url.fragment) |
| if not anchor: |
| log.warning( |
| f"Error reading metadata of post '{path}' in '{docs}':\n" |
| f"Couldn't find anchor '{url.fragment}' in '{url.path}'" |
| ) |
|
|
| |
| link.url = url.geturl() |
| continue |
|
|
| |
| link.url += f"#{anchor.id}" |
| link.meta["subtitle"] = anchor.title |
|
|
| |
|
|
| |
| |
| def _attach(self, parent: StructureItem, pages: list[Page]): |
| for tail, page, head in zip(pages, pages[1:], pages[2:]): |
|
|
| |
| page.parent = parent |
| page.previous_page = tail |
| page.next_page = head |
|
|
| |
| |
| if isinstance(page, View): |
| view = self._resolve_original(page) |
| if tail: tail.next_page = view |
| if head: head.previous_page = view |
|
|
| |
| |
| def _attach_at(self, parent: StructureItem, host: Page, page: Page): |
| self._attach(parent, [host.previous_page, page, host.next_page]) |
|
|
| |
| |
| def _attach_to(self, view: View, section: Section, nav: Navigation): |
| section.parent = view.parent |
|
|
| |
| |
| |
| |
| items = self._resolve_siblings(view, nav) |
| items.append(section) |
|
|
| |
| |
| tail = next(item for item in reversed(items) if isinstance(item, Page)) |
| head = tail.next_page |
|
|
| |
| nav.pages.extend(section.children) |
| self._attach(section, [tail, *section.children, head]) |
|
|
| |
|
|
| |
| def _render(self, view: View): |
| posts, pagination = view.posts, None |
|
|
| |
| if self._config_pagination(view): |
| at = view.pages.index(view) |
|
|
| |
| step = self._config_pagination_per_page(view) |
| p, q = at * step, at * step + step |
|
|
| |
| posts = view.posts[p:q] |
| pagination = self._render_pagination(view, (p, q)) |
|
|
| |
| posts = [ |
| self._render_post(post.excerpt, view) |
| for post in posts if post.excerpt |
| ] |
|
|
| |
| return posts, pagination |
|
|
| |
| def _render_post(self, excerpt: Excerpt, view: View): |
| excerpt.render(view, self.config.post_excerpt_separator) |
|
|
| |
| |
| toc = self._config_toc(view) |
| if toc and excerpt.toc.items and view.toc.items: |
| view.toc.items[0].children.append(excerpt.toc.items[0]) |
|
|
| |
| return excerpt |
|
|
| |
| def _render_pagination(self, view: View, range: tuple[int, int]): |
| p, q = range |
|
|
| |
| def url_maker(n: int): |
| return get_relative_url(view.pages[n - 1].url, view.url) |
|
|
| |
| return Pagination( |
| view.posts, page = q // (q - p), |
| items_per_page = q - p, |
| url_maker = url_maker |
| ) |
|
|
| |
|
|
| |
| def _config(self, key: str, default: any): |
| return default if self.config[key] is None else self.config[key] |
|
|
| |
| def _config_toc(self, view: View): |
| default = self.config.blog_toc |
| if isinstance(view, Archive): |
| return self._config("archive_toc", default) |
| if isinstance(view, Category): |
| return self._config("categories_toc", default) |
| if isinstance(view, Profile): |
| return self._config("authors_profiles_toc", default) |
| else: |
| return default |
|
|
| |
| def _config_pagination(self, view: View): |
| default = self.config.pagination |
| if isinstance(view, Archive): |
| return self._config("archive_pagination", default) |
| if isinstance(view, Category): |
| return self._config("categories_pagination", default) |
| if isinstance(view, Profile): |
| return self._config("authors_profiles_pagination", default) |
| else: |
| return default |
|
|
| |
| def _config_pagination_per_page(self, view: View): |
| default = self.config.pagination_per_page |
| if isinstance(view, Archive): |
| return self._config("archive_pagination_per_page", default) |
| if isinstance(view, Category): |
| return self._config("categories_pagination_per_page", default) |
| if isinstance(view, Profile): |
| return self._config("authors_profiles_pagination_per_page", default) |
| else: |
| return default |
|
|
| |
|
|
| |
| def _format_path_for_post(self, post: Post, config: MkDocsConfig): |
| categories = post.config.categories[:self.config.post_url_max_categories] |
| categories = [self._slugify_category(name) for name in categories] |
|
|
| |
| date = post.config.date.created |
| path = self.config.post_url_format.format( |
| categories = "/".join(categories), |
| date = self._format_date_for_post_url(date, config), |
| file = post.file.name, |
| slug = post.config.slug or self._slugify_post(post) |
| ) |
|
|
| |
| path = posixpath.normpath(path.strip("/")) |
| return posixpath.join(self.config.blog_dir, f"{path}.md") |
|
|
| |
| def _format_path_for_archive(self, post: Post, config: MkDocsConfig): |
| date = post.config.date.created |
| path = self.config.archive_url_format.format( |
| date = self._format_date_for_archive_url(date, config) |
| ) |
|
|
| |
| path = posixpath.normpath(path.strip("/")) |
| return posixpath.join(self.config.blog_dir, f"{path}.md") |
|
|
| |
| def _format_path_for_category(self, name: str): |
| path = self.config.categories_url_format.format( |
| slug = self._slugify_category(name) |
| ) |
|
|
| |
| path = posixpath.normpath(path.strip("/")) |
| return posixpath.join(self.config.blog_dir, f"{path}.md") |
|
|
| |
| def _format_path_for_profile(self, id: str, author: Author): |
| path = self.config.authors_profiles_url_format.format( |
| slug = author.slug or id, |
| name = author.name |
| ) |
|
|
| |
| path = posixpath.normpath(path.strip("/")) |
| return posixpath.join(self.config.blog_dir, f"{path}.md") |
|
|
| |
| def _format_path_for_pagination(self, view: View, page: int): |
| path = self.config.pagination_url_format.format( |
| page = page |
| ) |
|
|
| |
| |
| |
| |
| base, _ = posixpath.splitext(view.file.src_uri) |
| if view.is_index: |
| base = posixpath.dirname(base) |
| path = posixpath.join(path, "index") |
|
|
| |
| path = posixpath.normpath(path.strip("/")) |
| return posixpath.join(base, f"{path}.md") |
|
|
| |
|
|
| |
| |
| |
| |
| def _format_date(self, date: datetime, format: str, config: MkDocsConfig): |
| locale: str = config.theme["language"].replace("-", "_") |
| if format in ["full", "long", "medium", "short"]: |
| return format_date(date, format = format, locale = locale) |
| else: |
| return format_datetime(date, format = format, locale = locale) |
|
|
| |
| def _format_date_for_post(self, date: datetime, config: MkDocsConfig): |
| format = self.config.post_date_format |
| return self._format_date(date, format, config) |
|
|
| |
| def _format_date_for_post_url(self, date: datetime, config: MkDocsConfig): |
| format = self.config.post_url_date_format |
| return self._format_date(date, format, config) |
|
|
| |
| def _format_date_for_archive(self, date: datetime, config: MkDocsConfig): |
| format = self.config.archive_date_format |
| return self._format_date(date, format, config) |
|
|
| |
| def _format_date_for_archive_url(self, date: datetime, config: MkDocsConfig): |
| format = self.config.archive_url_date_format |
| return self._format_date(date, format, config) |
|
|
| |
|
|
| |
| def _slugify_post(self, post: Post): |
| separator = self.config.post_slugify_separator |
| return self.config.post_slugify(post.title, separator) |
|
|
| |
| def _slugify_category(self, name: str): |
| separator = self.config.categories_slugify_separator |
| return self.config.categories_slugify(name, separator) |
|
|
| |
|
|
| |
| |
| def _path_to_file(self, path: str, config: MkDocsConfig, *, temp = True): |
| assert path.endswith(".md") |
| file = File( |
| path, |
| config.docs_dir if not temp else self.temp_dir, |
| config.site_dir, |
| config.use_directory_urls |
| ) |
|
|
| |
| |
| |
| |
| if temp: |
| file.generated_by = "material/blog" |
|
|
| |
| return file |
|
|
| |
| def _save_to_file(self, path: str, content: str): |
| os.makedirs(os.path.dirname(path), exist_ok = True) |
| with open(path, "w", encoding = "utf-8") as f: |
| f.write(content) |
|
|
| |
|
|
| |
| def _translate(self, key: str, config: MkDocsConfig) -> str: |
| env = config.theme.get_env() |
| template = env.get_template( |
| "partials/language.html", globals = { "config": config } |
| ) |
|
|
| |
| return template.module.t(key) |
|
|
| |
| |
| |
|
|
| |
| def _find_links(items: list[StructureItem]): |
| for item in items: |
|
|
| |
| if isinstance(item, Link): |
| yield item |
|
|
| |
| if isinstance(item, Section): |
| for item in _find_links(item.children): |
| assert isinstance(item, Link) |
| yield item |
|
|
| |
| def _find_anchor(toc: TableOfContents, id: str): |
| for anchor in toc: |
| if anchor.id == id: |
| return anchor |
|
|
| |
| anchor = _find_anchor(anchor.children, id) |
| if isinstance(anchor, AnchorLink): |
| return anchor |
|
|
| |
| |
| |
|
|
| |
| log = logging.getLogger("mkdocs.material.blog") |
|
|