From 3ea201743f48b0614a9c5de2d7af5b91e0ca3995 Mon Sep 17 00:00:00 2001 From: Valentin Berlier Date: Fri, 29 Mar 2024 03:11:40 +0100 Subject: [PATCH] feat: add lectern.contrib.tagged_function_shorthand --- examples/with_beet/beet.json | 20 +++++ examples/with_beet/tagged.txt | 23 +++++ lectern/contrib/relative_location.py | 7 +- lectern/contrib/tagged_function_shorthand.py | 64 +++++++++++++ .../examples__beet_project__0.pack.md | 89 ++++++++++++++++++- 5 files changed, 200 insertions(+), 3 deletions(-) create mode 100644 examples/with_beet/tagged.txt create mode 100644 lectern/contrib/tagged_function_shorthand.py diff --git a/examples/with_beet/beet.json b/examples/with_beet/beet.json index c0cc9aa..660d8de 100644 --- a/examples/with_beet/beet.json +++ b/examples/with_beet/beet.json @@ -46,6 +46,26 @@ } } } + }, + { + "require": [ + "lectern.contrib.relative_location", + "lectern.contrib.tagged_function_shorthand" + ], + "pipeline": ["lectern"], + "meta": { + "lectern": { + "load": "tagged.txt" + }, + "tagged_function_shorthand": { + "tags": { + "load": "minecraft:load", + "tick": "minecraft:tick", + "foo": "demo:foo", + "bar": ["demo:abc", "demo:123"] + } + } + } } ], "meta": { diff --git a/examples/with_beet/tagged.txt b/examples/with_beet/tagged.txt new file mode 100644 index 0000000..5d11f71 --- /dev/null +++ b/examples/with_beet/tagged.txt @@ -0,0 +1,23 @@ +@load_function demo:a +say tagged a + +@load_function demo:b +say tagged b + +@load_function a_relative +say tagged a_relative + +@load_function +say tagged load + +@load_function(prepend) +say before + +@tick_function wow +say tagged wow + +@foo_function +say tagged foo + +@bar_function +say tagged bar diff --git a/lectern/contrib/relative_location.py b/lectern/contrib/relative_location.py index 28f6bc5..cbda97c 100644 --- a/lectern/contrib/relative_location.py +++ b/lectern/contrib/relative_location.py @@ -30,8 +30,11 @@ def __call__( fragment: Fragment, directives: Mapping[str, Directive], ) -> Fragment: - if isinstance(directives.get(fragment.directive), NamespacedResourceDirective): - name = fragment.expect("name") + if ( + isinstance(directives.get(fragment.directive), NamespacedResourceDirective) + and fragment.arguments + ): + name = fragment.arguments[0] if ":" not in name: fragment = replace(fragment, arguments=[self.ctx.generate.path(name)]) return fragment diff --git a/lectern/contrib/tagged_function_shorthand.py b/lectern/contrib/tagged_function_shorthand.py new file mode 100644 index 0000000..491c8f1 --- /dev/null +++ b/lectern/contrib/tagged_function_shorthand.py @@ -0,0 +1,64 @@ +__all__ = [ + "tagged_function_shorthand", + "TaggedFunctionDirective", +] + + +from dataclasses import dataclass, replace +from typing import Dict, Sequence, Type + +from beet import ( + Context, + DataPack, + Function, + Generator, + ListOption, + NamespaceFile, + PluginOptions, + ResourcePack, + configurable, +) +from beet.core.utils import required_field + +from lectern import Document, Fragment, NamespacedResourceDirective + + +class TaggedFunctionOptions(PluginOptions): + tags: Dict[str, ListOption[str]] = {} + + +def beet_default(ctx: Context): + ctx.require(tagged_function_shorthand) + + +@configurable(validator=TaggedFunctionOptions) +def tagged_function_shorthand(ctx: Context, opts: TaggedFunctionOptions): + document = ctx.inject(Document) + for shorthand, tags in opts.tags.items(): + document.directives[f"{shorthand}_function"] = TaggedFunctionDirective( + shorthand=shorthand, + tags=tags.entries(), + generate=ctx.generate, + ) + + +@dataclass +class TaggedFunctionDirective(NamespacedResourceDirective): + shorthand: str = required_field() + tags: Sequence[str] = required_field() + generate: Generator = required_field() + + file_type: Type[NamespaceFile] = Function + + def __call__(self, fragment: Fragment, assets: ResourcePack, data: DataPack): + if not fragment.arguments: + fragment = replace( + fragment, arguments=(self.generate.path(self.shorthand),) + ) + + super().__call__(fragment, assets, data) + + full_name = fragment.expect("full_name") + + for tag in self.tags: + data.function_tags.setdefault(tag).add(full_name) diff --git a/tests/snapshots/examples__beet_project__0.pack.md b/tests/snapshots/examples__beet_project__0.pack.md index 386ba89..517a9a6 100644 --- a/tests/snapshots/examples__beet_project__0.pack.md +++ b/tests/snapshots/examples__beet_project__0.pack.md @@ -27,6 +27,37 @@ function with_beet:def say relative ``` +`@function with_beet:a_relative` + +```mcfunction +say tagged a_relative +``` + +`@function with_beet:load` + +```mcfunction +say before +say tagged load +``` + +`@function with_beet:wow` + +```mcfunction +say tagged wow +``` + +`@function with_beet:foo` + +```mcfunction +say tagged foo +``` + +`@function with_beet:bar` + +```mcfunction +say tagged bar +``` + ### demo `@function demo:foo` @@ -139,6 +170,48 @@ say azertyuiopqsdfghjklm say 2 + 2 is 4 (end of citation) ``` +`@function demo:a` + +```mcfunction +say tagged a +``` + +`@function demo:b` + +```mcfunction +say tagged b +``` + +`@function_tag demo:foo` + +```json +{ + "values": [ + "with_beet:foo" + ] +} +``` + +`@function_tag demo:abc` + +```json +{ + "values": [ + "with_beet:bar" + ] +} +``` + +`@function_tag demo:123` + +```json +{ + "values": [ + "with_beet:bar" + ] +} +``` + ### embedded `@function embedded:foo` @@ -322,7 +395,21 @@ say 9 ```json { "values": [ - "isolated:thing" + "isolated:thing", + "with_beet:wow" + ] +} +``` + +`@function_tag minecraft:load` + +```json +{ + "values": [ + "demo:a", + "demo:b", + "with_beet:a_relative", + "with_beet:load" ] } ```