Skip to content

Commit

Permalink
Consolidate hexdoc hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object committed Nov 7, 2023
1 parent dc82819 commit 1c653fa
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 48 deletions.
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ hexdoc = "hexdoc._cli.app:app"

[project.entry-points.hexdoc]
hexdoc = "hexdoc._hooks:HexdocPlugin"
minecraft = "hexdoc.minecraft._hooks:MinecraftPlugin"
patchouli = "hexdoc.patchouli._hooks:PatchouliPlugin"

# Pytest

Expand Down Expand Up @@ -139,6 +137,9 @@ extend-exclude = [
"[{][{]cookiecutter.output_directory[}][}]",
]

[tool.ruff.lint.isort]
combine-as-imports = true

# type checking

[tool.pyright]
Expand Down
2 changes: 1 addition & 1 deletion src/hexdoc/_cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def render(

template_names = (
props.template.render
if props.template.was_render_set
if props.template.override_default_render
else pm.default_rendered_templates(props.template.include)
) | props.template.extend_render

Expand Down
11 changes: 4 additions & 7 deletions src/hexdoc/_cli/utils/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import shutil
from pathlib import Path
from typing import Any
from typing import Any, Sequence

from _hexdoc_favicons import Favicons
from jinja2 import (
Expand All @@ -20,6 +20,7 @@
from jinja2.sandbox import SandboxedEnvironment

from hexdoc.core import Properties, ResourceLocation
from hexdoc.core.properties import JINJA_NAMESPACE_ALIASES
from hexdoc.data import HexdocMetadata
from hexdoc.jinja import (
IncludeRawExtension,
Expand All @@ -37,10 +38,6 @@


class HexdocTemplateLoader(BaseLoader):
NAMESPACE_ALIASES = {
"patchouli": "hexdoc",
}

def __init__(
self,
included: dict[str, PackageLoader],
Expand All @@ -57,7 +54,7 @@ def __init__(
self.props_file = props_file

def get_source(self, environment: Environment, template: str):
for alias, replacement in self.NAMESPACE_ALIASES.items():
for alias, replacement in JINJA_NAMESPACE_ALIASES.items():
if template.startswith(f"{alias}:"):
logging.getLogger(__name__).info(
f"Replacing {alias} with {replacement} for template {template}"
Expand All @@ -80,7 +77,7 @@ def get_source(self, environment: Environment, template: str):
raise


def create_jinja_env(pm: PluginManager, include: list[str], props_file: Path):
def create_jinja_env(pm: PluginManager, include: Sequence[str], props_file: Path):
included, extra = pm.load_jinja_templates(include)

env = SandboxedEnvironment(
Expand Down
19 changes: 18 additions & 1 deletion src/hexdoc/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
from pathlib import Path

import hexdoc
from hexdoc.minecraft.recipe import (
ingredients as minecraft_ingredients,
recipes as minecraft_recipes,
)
from hexdoc.patchouli.page import pages as patchouli_pages
from hexdoc.plugin import (
DefaultRenderedTemplatesImpl,
HookReturn,
LoadJinjaTemplatesImpl,
LoadResourceDirsImpl,
LoadTaggedUnionsImpl,
ModVersionImpl,
hookimpl,
)


class HexdocPlugin(
LoadTaggedUnionsImpl,
ModVersionImpl,
LoadResourceDirsImpl,
LoadJinjaTemplatesImpl,
Expand All @@ -21,7 +28,8 @@ class HexdocPlugin(
@staticmethod
@hookimpl
def hexdoc_mod_version():
return "(TODO: remove)"
# need to implement this to be able to use `hexdoc export`
return ""

@staticmethod
@hookimpl
Expand All @@ -30,6 +38,15 @@ def hexdoc_load_resource_dirs() -> HookReturn[Package]:

return [generated, resources]

@staticmethod
@hookimpl
def hexdoc_load_tagged_unions() -> HookReturn[Package]:
return [
patchouli_pages,
minecraft_recipes,
minecraft_ingredients,
]

@staticmethod
@hookimpl
def hexdoc_load_jinja_templates() -> tuple[Package, str]:
Expand Down
20 changes: 17 additions & 3 deletions src/hexdoc/core/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from typing import Any, Self, Sequence

from pydantic import Field, PrivateAttr
from pydantic import Field, PrivateAttr, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict

from hexdoc.model.strip_hidden import StripHiddenModel
Expand All @@ -14,10 +14,15 @@
load_toml_with_placeholders,
relative_path_root,
)
from hexdoc.utils.types import PydanticOrderedSet

from .resource import ResourceLocation
from .resource_dir import ResourceDir

JINJA_NAMESPACE_ALIASES = {
"patchouli": "hexdoc",
}


class EnvironmentVariableProps(BaseSettings):
model_config = SettingsConfigDict(env_file=".env", extra="allow")
Expand Down Expand Up @@ -63,7 +68,7 @@ def _github_repository_parts(self):
class TemplateProps(StripHiddenModel, validate_assignment=True):
static_dir: RelativePath | None = None
icon: RelativePath
include: list[str]
include: PydanticOrderedSet[str]

render: dict[Path, str] = Field(default_factory=dict)
extend_render: dict[Path, str] = Field(default_factory=dict)
Expand All @@ -73,9 +78,18 @@ class TemplateProps(StripHiddenModel, validate_assignment=True):
_was_render_set: bool = PrivateAttr(False)

@property
def was_render_set(self):
def override_default_render(self):
return self._was_render_set

@field_validator("include", mode="after")
@classmethod
def _resolve_aliases(cls, values: PydanticOrderedSet[str]):
for alias, replacement in JINJA_NAMESPACE_ALIASES.items():
if alias in values:
values.remove(alias)
values.add(replacement)
return values


class MinecraftAssetsProps(StripHiddenModel):
ref: str
Expand Down
12 changes: 0 additions & 12 deletions src/hexdoc/minecraft/_hooks.py

This file was deleted.

22 changes: 0 additions & 22 deletions src/hexdoc/patchouli/_hooks.py

This file was deleted.

0 comments on commit 1c653fa

Please sign in to comment.