Move templates to hexdoc_templates and restructure props a bit

This commit is contained in:
object-Object 2023-08-26 21:31:46 -04:00
parent 4e205871c8
commit d95a8fcc18
26 changed files with 61 additions and 76 deletions

View file

@ -3,6 +3,7 @@
modid = "hexcasting"
book = "hexcasting:thehexbook"
url = "https://gamma-delta.github.io/HexMod"
default_lang = "en_us"
# top takes priority
resource_dirs = [
@ -19,34 +20,6 @@ export_dir = "src/hexdoc/_export/generated"
# NOTE: "!Raw" means "don't apply variable interpolation to this value"
_pattern_regex = { "!Raw" = 'make\(\s*"(?P<name>[a-zA-Z0-9_\/]+)",\s*(?:new )?(?:ActionRegistryEntry|OperationAction)\(\s*HexPattern\.fromAngles\(\s*"(?P<signature>[aqweds]+)",\s*HexDir.(?P<startdir>\w+)\)' }
entry_id_blacklist = []
template = "main.html.jinja"
template_dirs = []
template_packages = [
["hexdoc", "_templates"],
]
is_0_black = false
[template_args]
title = "Hex Book"
mod_name = "Hex Casting"
author = "petrak@, Alwinfy"
description = "The Hex Book, all in one place."
icon_href = "logo.png"
is_bleeding_edge = true
show_landing_text = true
[base_asset_urls]
hexcasting = "https://raw.githubusercontent.com/gamma-delta/HexMod/main/Common/src/main/resources"
[i18n]
default_lang = "en_us"
[[pattern_stubs]]
path = "{^_common.package}/common/lib/hex/HexActions.java"
@ -57,6 +30,26 @@ path = "{^_fabric.package}/FabricHexInitializer.kt"
regex = "{^_pattern_regex}"
[base_asset_urls]
hexcasting = "https://raw.githubusercontent.com/gamma-delta/HexMod/main/Common/src/main/resources"
[template]
main = "main.html.jinja"
packages = [
"hexdoc_templates",
]
[template.args]
title = "Hex Book"
mod_name = "Hex Casting"
author = "petrak@, Alwinfy"
description = "The Hex Book, all in one place."
icon_href = "logo.png"
is_bleeding_edge = true
show_landing_text = true
# platforms
[_common]

View file

@ -5,10 +5,10 @@ build-backend = "hatchling.build"
[project]
name = "hexdoc"
version = "0.1.0+0.11.1-7" # TODO: make a Hatch plugin to auto-set the mod version after the +
version = "0.11.0.1.0" # TODO: make a Hatch plugin to auto-set the mod version after the +
authors = [
{ name="Alwinfy" },
{ name="object-Object", email="object@objectobject.ca" },
{ name="Alwinfy" },
]
readme = "README.md"
requires-python = ">=3.11"
@ -29,9 +29,16 @@ dev = [
"hatchling",
]
[tool.hatch.build]
packages = [
"src/hexdoc",
"src/hexdoc_templates",
]
[project.scripts]
hexdoc = "hexdoc.hexdoc:main"
[project.entry-points."hexdoc.export"]
hexcasting = "hexdoc._export:__resources__"
@ -51,23 +58,16 @@ hexcasting = "hexdoc.hexcasting.hex_recipes"
hexcasting = "hexdoc.hexcasting.hex_recipes"
[tool.hatch.build]
packages = ["src/hexdoc"]
[tool.pytest.ini_options]
addopts = ["--import-mode=importlib"]
[tool.coverage.report]
include_namespace_packages = true
[tool.isort]
profile = "black"
combine_as_imports = true
[tool.pyright]
pythonVersion = "3.11"
pythonPlatform = "All"

View file

@ -98,10 +98,10 @@ def main(args: Args | None = None) -> None:
env = SandboxedEnvironment(
# search order: template_dirs, template_packages
loader=ChoiceLoader(
[FileSystemLoader(props.template_dirs)]
[FileSystemLoader(props.template.dirs)]
+ [
PackageLoader(name, str(path))
for name, path in props.template_packages
for name, path in props.template.packages
]
),
undefined=StrictUndefined,
@ -118,10 +118,10 @@ def main(args: Args | None = None) -> None:
}
# load and render template
template = env.get_template(props.template)
template = env.get_template(props.template.main)
docs = strip_empty_lines(
template.render(
**props.template_args,
**props.template.args,
book=book,
props=props,
)

View file

@ -121,10 +121,10 @@ class I18n:
folder="lang",
namespace="*",
glob=[
f"{props.i18n.default_lang}.json",
f"{props.i18n.default_lang}.json5",
f"{props.i18n.default_lang}.flatten.json",
f"{props.i18n.default_lang}.flatten.json5",
f"{props.default_lang}.json",
f"{props.default_lang}.json5",
f"{props.default_lang}.flatten.json",
f"{props.default_lang}.flatten.json5",
],
decode=decode_and_flatten_json_dict,
export=self._export,

View file

@ -4,9 +4,9 @@ import re
from pathlib import Path
from typing import Annotated, Any, Self
from pydantic import AfterValidator, HttpUrl
from pydantic import AfterValidator, Field, HttpUrl, field_validator
from .model import HexDocModel, StripHiddenModel
from .model import StripHiddenModel
from .resource import ResourceDir, ResourceLocation
from .toml_placeholders import load_toml_with_placeholders
@ -17,56 +17,48 @@ NoTrailingSlashHttpUrl = Annotated[
]
class HexDocMeta(HexDocModel):
book_url: NoTrailingSlashHttpUrl
class PatternStubProps(StripHiddenModel):
path: Path
regex: re.Pattern[str]
class XplatProps(StripHiddenModel):
src: Path
pattern_stubs: list[PatternStubProps] | None = None
resources: Path
class TemplateProps(StripHiddenModel):
main: str
dirs: list[Path] = Field(default_factory=list)
packages: list[tuple[str, Path]]
args: dict[str, Any]
@field_validator("packages", mode="before")
def _check_packages(cls, values: Any | list[Any]):
if not isinstance(values, list):
return values
class PlatformProps(XplatProps):
recipes: Path
tags: Path
class I18nProps(StripHiddenModel):
default_lang: str
for i, value in enumerate(values):
if isinstance(value, str):
values[i] = (value, Path())
return values
class Properties(StripHiddenModel):
modid: str
book: ResourceLocation
url: NoTrailingSlashHttpUrl
default_lang: str
is_0_black: bool = Field(default=False)
"""If true, the style `$(0)` changes the text color to black; otherwise it resets
the text color to the default."""
resource_dirs: list[ResourceDir]
export_dir: Path
entry_id_blacklist: set[ResourceLocation]
entry_id_blacklist: set[ResourceLocation] = Field(default_factory=set)
template: str
template_dirs: list[Path]
template_packages: list[tuple[str, Path]]
is_0_black: bool
"""If true, the style `$(0)` changes the text color to black; otherwise it resets
the text color to the default."""
template_args: dict[str, Any]
pattern_stubs: list[PatternStubProps]
base_asset_urls: dict[str, NoTrailingSlashHttpUrl]
"""Mapping from modid to the url of that mod's `resources` directory on GitHub."""
i18n: I18nProps
pattern_stubs: list[PatternStubProps]
template: TemplateProps
@classmethod
def load(cls, path: Path) -> Self:

View file

@ -101,7 +101,7 @@ class ModResourceLoader:
type="assets",
folder=Path("patchouli_books")
/ self.props.book.path
/ self.props.i18n.default_lang
/ self.props.default_lang
/ folder,
namespace=self.props.book.namespace,
)