Skip to content

Commit

Permalink
feat: apply directives to context directly
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Aug 7, 2023
1 parent e9e7005 commit a4cbf48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
6 changes: 2 additions & 4 deletions lectern/contrib/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ def messaging(ctx: Context, opts: MessagingOptions):
if fragments := list(
document.markdown_extractor.parse_fragments(message, directives)
):
assets, data = document.markdown_extractor.apply_directives(
directives, fragments, document.loaders
document.markdown_extractor.apply_directives(
ctx.assets, ctx.data, directives, fragments, document.loaders
)
ctx.assets.merge(assets)
ctx.data.merge(data)
elif code_blocks := [
token.content
for token in document.markdown_extractor.parser.parse(message) # type: ignore
Expand Down
20 changes: 12 additions & 8 deletions lectern/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,21 @@ def extract(
loaders: Iterable[FragmentLoader] = (),
) -> Tuple[ResourcePack, DataPack]:
"""Extract a resource pack and a data pack."""
return self.apply_directives(
directives, self.parse_fragments(source, directives), loaders
assets, data = ResourcePack(), DataPack()
self.apply_directives(
assets, data, directives, self.parse_fragments(source, directives), loaders
)
return assets, data

def apply_directives(
self,
assets: ResourcePack,
data: DataPack,
directives: Mapping[str, Directive],
fragments: Iterable[Fragment],
loaders: Iterable[FragmentLoader] = (),
) -> Tuple[ResourcePack, DataPack]:
):
"""Apply directives into a blank data pack and a blank resource pack."""
assets, data = ResourcePack(), DataPack()

for fragment in fragments:
for loader in loaders:
fragment = loader(fragment, directives)
Expand All @@ -127,8 +129,6 @@ def apply_directives(
if fragment:
directives[fragment.directive](fragment, assets, data)

return assets, data

def parse_fragments(
self,
source: str,
Expand Down Expand Up @@ -249,11 +249,15 @@ def extract(
loaders: Iterable[FragmentLoader] = (),
external_files: Optional[FileSystemPath] = None,
) -> Tuple[ResourcePack, DataPack]:
return self.apply_directives(
assets, data = ResourcePack(), DataPack()
self.apply_directives(
assets,
data,
directives,
self.parse_fragments(source, directives, external_files),
loaders,
)
return assets, data

def parse_fragments(
self,
Expand Down

0 comments on commit a4cbf48

Please sign in to comment.