Move templates into hexdoc package

This commit is contained in:
object-Object 2023-07-24 22:27:11 -04:00
parent 03e7683ae1
commit 178d7c2af5
27 changed files with 62 additions and 61 deletions

View file

@ -14,6 +14,9 @@ default_recipe_dir = 0
pattern_regex = {_Raw='HexPattern\.fromAngles\("([qweasd]+)", HexDir\.(\w+)\),\s*modLoc\("([^"]+)"\)([^;]*true\);)?'}
template = "main.html.jinja"
template_dirs = []
template_packages = []
spoilers = [
"hexcasting:opened_eyes",
"hexcasting:y_u_no_cast_angy",

View file

@ -2,6 +2,9 @@
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build]
packages = ["src/hexdoc"]
[project]
name = "HexDoc"
@ -43,10 +46,6 @@ hexdoc-minecraft = "hexdoc.minecraft.recipe.ingredients"
hexdoc-hexcasting = "hexdoc.hexcasting.hex_recipes"
[tool.hatch.build]
packages = ["src/hexdoc"]
[tool.pytest.ini_options]
addopts = ["--import-mode=importlib"]
markers = [

View file

@ -9,7 +9,7 @@
{# table of contents #}
<nav>
{% include "table_of_contents.html.jinja" %}
{% include "components/table_of_contents.html.jinja" %}
</nav>
{# actual book content (ie. all the categories) #}

View file

@ -1,4 +1,4 @@
{% import "macros.html.jinja" as macros %}
{% import "common/macros.html.jinja" as macros %}
<section id="{{ category.id.path }}">
{% call macros.maybe_spoilered(category) %}

View file

@ -1,4 +1,4 @@
{% import "macros.html.jinja" as macros -%}
{% import "common/macros.html.jinja" as macros -%}
<h2 id="table-of-contents" class="page-header">
Table of Contents<a

View file

@ -1,4 +1,4 @@
{% import "macros.html.jinja" as macros -%}
{% import "common/macros.html.jinja" as macros -%}
<div id="{{ entry.id.path }}">
{% call macros.maybe_spoilered(entry) %}

View file

@ -36,7 +36,7 @@
</head>
<body>
{% include "welcome.html.jinja" +%}
{% include "components/welcome.html.jinja" +%}
{% include "book.html.jinja" +%}
</body>
</html>

View file

@ -1,5 +1,5 @@
{% extends "pages/Page.html.jinja" %}
{% include "macros.html.jinja" %}
{% extends "pages/patchouli/page.html.jinja" %}
{% include "common/macros.html.jinja" %}
{% block body %}
{{ macros.recipe_block([page.recipe], "name", "A mind-flaying recipe producing the", "") }}

View file

@ -1,5 +1,5 @@
{% extends "pages/PageWithTitle.html.jinja" %}
{% include "macros.html.jinja" %}
{% extends "pages/patchouli/text.html.jinja" %}
{% include "common/macros.html.jinja" %}
{% block inner_body %}
{{ macros.recipe_block(page.recipes, "item", "Several crafting recipes, for the", ", ") }}

View file

@ -1,4 +1,4 @@
{% extends "pages/PageWithTitle.html.jinja" %}
{% extends "pages/patchouli/text.html.jinja" %}
{% block inner_body %}
<details class="spell-collapsible">

View file

@ -1,3 +1,3 @@
{% extends "pages/PageWithPattern.html.jinja" %}
{% extends "pages/hexcasting/manual_pattern.html.jinja" %}
{% block title_attrs %} class="pattern-title"{% endblock %}

View file

@ -1,5 +1,5 @@
{% extends "pages/PageWithTitle.html.jinja" %}
{% include "macros.html.jinja" %}
{% extends "pages/patchouli/text.html.jinja" %}
{% include "common/macros.html.jinja" %}
{% block inner_body %}
{{ macros.recipe_block(page.recipes, "item", "The crafting recipe for the", " and ") }}

View file

@ -1,4 +1,4 @@
{% extends "pages/PageWithTitle.html.jinja" %}
{% extends "pages/patchouli/text.html.jinja" %}
{% block inner_body %}
<p class="img-wrapper">

View file

@ -1,4 +1,4 @@
{% extends "pages/PageWithTitle.html.jinja" %}
{% extends "pages/patchouli/text.html.jinja" %}
{% block inner_body %}
{{ super() }}

View file

@ -1,4 +1,4 @@
{% extends "pages/PageWithTitle.html.jinja" %}
{% extends "pages/patchouli/text.html.jinja" %}
{% block inner_body %}
<h4 class="spotlight-title page-header">{{ page.item }}</h4>

View file

@ -1,5 +1,5 @@
{% extends "pages/Page.html.jinja" %}
{% import "macros.html.jinja" as macros %}
{% extends "pages/patchouli/page.html.jinja" %}
{% import "common/macros.html.jinja" as macros %}
{% block body %}
{% if page.title is not none %}

View file

@ -12,10 +12,7 @@ from ..hex_recipes import BrainsweepRecipe
from .abstract_hex_pages import PageWithOpPattern, PageWithPattern
class LookupPatternPage(
PageWithOpPattern[HexContext],
type="hexcasting:pattern",
):
class LookupPatternPage(PageWithOpPattern[HexContext], type="hexcasting:pattern"):
@model_validator(mode="before")
def _pre_root_lookup(cls, values: dict[str, Any], info: ValidationInfo):
context = cast(HexContext, info.context)
@ -34,7 +31,6 @@ class LookupPatternPage(
class ManualOpPatternPage(
PageWithOpPattern[HexContext],
type="hexcasting:manual_pattern",
template_name="PageWithPattern",
):
pass
@ -42,7 +38,6 @@ class ManualOpPatternPage(
class ManualRawPatternPage(
PageWithPattern[HexContext],
type="hexcasting:manual_pattern",
template_name="PageWithPattern",
):
pass
@ -50,7 +45,7 @@ class ManualRawPatternPage(
class ManualPatternNosigPage(
PageWithPattern[HexContext],
type="hexcasting:manual_pattern_nosig",
template_name="PageWithPattern",
template_type="hexcasting:manual_pattern",
):
input: None = None
output: None = None

View file

@ -5,7 +5,6 @@ from pydantic.functional_validators import ModelWrapValidatorHandler
from hexdoc.minecraft import LocalizedStr
from hexdoc.resource import ResourceLocation, TypeTaggedUnion
from hexdoc.utils import TagValue
from ..model import AnyBookContext
from ..text import FormatTree
@ -19,6 +18,7 @@ class Page(TypeTaggedUnion[AnyBookContext], group="hexdoc.Page", type=None):
__template: ClassVar[str]
type: ResourceLocation | None
advancement: ResourceLocation | None = None
flag: str | None = None
anchor: str | None = None
@ -26,22 +26,19 @@ class Page(TypeTaggedUnion[AnyBookContext], group="hexdoc.Page", type=None):
def __init_subclass__(
cls,
*,
type: TagValue | None,
template: str | None = None,
template_name: str | None = None,
type: str | None,
template_type: str | None = None,
) -> None:
super().__init_subclass__(group=None, type=type)
# jinja template
match template, template_name:
case str(), None:
cls.__template = template
case None, str():
cls.__template = f"pages/{template_name}.html.jinja"
case None, None:
cls.__template = f"pages/{cls.__name__}.html.jinja"
case _:
raise ValueError("Must specify at most one of template, template_name")
# jinja template path
if template_type is not None:
template = ResourceLocation.from_str(template_type)
else:
template = cls.type
if template:
cls.__template = f"pages/{template.namespace}/{template.path}.html.jinja"
@model_validator(mode="wrap")
@classmethod

View file

@ -9,11 +9,7 @@ from ..text import FormatTree
from .abstract_pages import Page, PageWithText, PageWithTitle
class TextPage(
PageWithTitle[BookContext],
type="patchouli:text",
template_name="PageWithTitle",
):
class TextPage(PageWithTitle[BookContext], type="patchouli:text"):
text: FormatTree
@ -78,5 +74,9 @@ class QuestPage(PageWithTitle[BookContext], type="patchouli:quest"):
title: LocalizedStr = LocalizedStr.with_value("Objective")
class EmptyPage(Page[BookContext], type="patchouli:empty", template_name="Page"):
class EmptyPage(
Page[BookContext],
type="patchouli:empty",
template_type="patchouli:page",
):
draw_filler: bool = True

View file

@ -51,6 +51,9 @@ class Properties(HexDocModel[Any]):
pattern_regex: re.Pattern[str]
template: str
template_dirs: list[Path]
template_packages: list[tuple[str, Path]]
spoilers: set[ResourceLocation]
blacklist: set[ResourceLocation]
@ -96,10 +99,6 @@ class Properties(HexDocModel[Any]):
def entries_dir(self) -> Path:
return self.book_dir / self.lang / "entries"
@property
def templates_dir(self) -> Path:
return self.book_dir / self.lang / "templates"
@property
def default_recipe_dir(self) -> Path:
return self.recipe_dirs[self.default_recipe_dir_index_]

View file

@ -131,7 +131,7 @@ class Entity(BaseResourceLocation, regex=_make_regex(nbt=True)):
class TypeTaggedUnion(InternallyTaggedUnion[AnyContext], key="type", value=None):
type: ResourceLocation | None
type: ResourceLocation | NoValueType | None
def __init_subclass__(
cls,
@ -143,7 +143,5 @@ class TypeTaggedUnion(InternallyTaggedUnion[AnyContext], key="type", value=None)
match type:
case str():
cls.type = ResourceLocation.from_str(type)
case NoValueType():
cls.type = None
case None:
pass
case _:
cls.type = type

View file

@ -3,7 +3,13 @@
from pathlib import Path
from jinja2 import Environment, FileSystemLoader, StrictUndefined
from jinja2 import (
ChoiceLoader,
Environment,
FileSystemLoader,
PackageLoader,
StrictUndefined,
)
# from jinja2.sandbox import SandboxedEnvironment
from tap import Tap
@ -41,8 +47,12 @@ def main(args: Args | None = None) -> None:
# set up Jinja environment
# TODO: SandboxedEnvironment
env = Environment(
# TODO: ChoiceLoader w/ ModuleLoader, but we need the directory restructure
loader=FileSystemLoader("./templates"),
# search order: template_dirs, template_packages, built-in hexdoc templates
loader=ChoiceLoader(
[FileSystemLoader(props.template_dirs)]
+ [PackageLoader(name, str(path)) for name, path in props.template_packages]
+ [PackageLoader("hexdoc", "_templates")]
),
undefined=StrictUndefined,
lstrip_blocks=True,
trim_blocks=True,