-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lectern.contrib.tagged_function_shorthand
- Loading branch information
Showing
5 changed files
with
200 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters