Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(plugin): add a flag attribute to acts depending on the used Mkdocs command #311

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions mkdocs_rss_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from email.utils import formatdate
from pathlib import Path
from re import compile as re_compile
from typing import List, Optional
from typing import List, Literal, Optional

# 3rd party
from jinja2 import Environment, FileSystemLoader, select_autoescape
Expand Down Expand Up @@ -55,9 +55,30 @@ class GitRssPlugin(BasePlugin[RssPluginConfig]):
# allow to set the plugin multiple times in the same mkdocs config
supports_multiple_instances = True

def __init__(self):
def __init__(self, *args, **kwargs):
"""Instantiation."""
# pages storage
super().__init__(*args, **kwargs)

self.cmd_is_serve: bool = False

def on_startup(
self, *, command: Literal["build", "gh-deploy", "serve"], dirty: bool
) -> None:
"""The `startup` event runs once at the very beginning of an `mkdocs` invocation.
Note that for initializing variables, the __init__ method is still preferred.
For initializing per-build variables (and whenever in doubt), use the
on_config event.

See: https://www.mkdocs.org/user-guide/plugins/#on_startup

Args:
command: the command that MkDocs was invoked with, e.g. "serve" for `mkdocs serve`.
dirty: whether `--dirty` flag was passed.
"""
# flag used command to disable some actions if serve is used
self.cmd_is_serve = command == "serve"

self.pages_to_filter: List[PageInformation] = []
# prepare output feeds
self.feed_created: dict = {}
Expand Down Expand Up @@ -108,6 +129,7 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig:
cache_dir=self.cache_dir,
use_git=self.config.use_git,
integration_material_social_cards=self.integration_material_social_cards,
mkdocs_command_is_on_serve=self.cmd_is_serve,
)

# check template dirs
Expand Down
15 changes: 13 additions & 2 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ class Util:

def __init__(
self,
path: str = ".",
cache_dir: Path = DEFAULT_CACHE_FOLDER,
use_git: bool = True,
integration_material_social_cards: Optional[
IntegrationMaterialSocialCards
] = None,
mkdocs_command_is_on_serve: bool = False,
path: str = ".",
use_git: bool = True,
):
"""Class hosting the plugin logic.
Expand All @@ -87,6 +88,13 @@ def __init__(
integration_material_social_cards (bool, optional): option to enable
integration with Social Cards plugin from Material theme. Defaults to True.
"""
self.mkdocs_command_is_on_serve = mkdocs_command_is_on_serve
if self.mkdocs_command_is_on_serve:
logger.debug(
"Mkdocs serve - Fetching remote images length is disabled to avoid "
"HTTP errors."
)

if use_git:
logger.debug("Git use is enabled.")
try:
Expand Down Expand Up @@ -654,6 +662,9 @@ def get_remote_image_length(
Returns:
int | None: image length as int or None
"""
if self.mkdocs_command_is_on_serve:
return None

# first, try HEAD request to avoid downloading the image
try:
attempt += 1
Expand Down