Skip to content

Commit

Permalink
feat: add lectern.contrib.tagged_function_shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Mar 29, 2024
1 parent 0b5e801 commit 3ea2017
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 3 deletions.
20 changes: 20 additions & 0 deletions examples/with_beet/beet.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
23 changes: 23 additions & 0 deletions examples/with_beet/tagged.txt
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions lectern/contrib/relative_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
64 changes: 64 additions & 0 deletions lectern/contrib/tagged_function_shorthand.py
Original file line number Diff line number Diff line change
@@ -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)
89 changes: 88 additions & 1 deletion tests/snapshots/examples__beet_project__0.pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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"
]
}
```
Expand Down

0 comments on commit 3ea2017

Please sign in to comment.