getpelican/pelican

Static files of drafts will be published even when `DRAFT_SAVE_AS = ""`

diegoe opened this issue · 1 comments

  • I have read the Filing Issues and subsequent “How to Get Help” sections of the documentation.
  • I have searched the issues (including closed ones) and believe that this is not a duplicate.
  • OS version and name: macOS 14.5 23F79
  • Python version: 3.12.4
  • Pelican version: 4.9.1
  • Link to theme: custom
  • Links to plugins: none
  • Link to your site: none
  • Link to your source: none
  • Link to a Gist with the contents of your settings file: Set DRAFT_SAVE_AS="" in default config

Issue

When an article is set to draft status, the static files used in it will be pulled into the published output even if no draft pages are generated (DRAFT_SAVE_AS="")

Since the above setting is described in docs as a way to omit entire categories/types, I would expect static files from said content to be omitted too.

FWIW, I have monkey-patched the above behavior with a plugin connected to article_generator_context:

"""Pelican plugin to omit publishing static_links of unrendered content.

Patches `Generator.add_static_links` to skip any content that has an
empty `.save_as`. This happens whenever content is omitted in Pelican's
configuration by setting a `*_SAVE_AS` variable to `""`
"""
import logging
import types

from pelican import signals
from pelican.contents import Content
from pelican.generators import Generator


logger = logging.getLogger(__name__)


def create_omit_draft_static(
    context_sender: Generator, metadata: dict
) -> None:
    """Patch Generator.add_static_links to omit unrendered content."""
    original_function = context_sender.add_static_links

    def patched(self: Generator, content: Content):
        if content.save_as:
            original_function(content)
        else:
            logger.info(
                "Omitting static_links for: %s",
                content.relative_source_path,
            )

    context_sender.add_static_links = types.MethodType(
        patched, context_sender
    )


def register() -> None:
    """Register Pelican plugin."""
    signals.article_generator_context.connect(create_omit_draft_static)