Přidán import jsonu do metadat, resolves #36.

This commit is contained in:
Jan Černohorský 2023-09-20 20:13:38 +02:00
parent b6634ddede
commit 32c2fb3bbc
4 changed files with 41 additions and 0 deletions

View file

@ -101,6 +101,14 @@ class Context:
for k in keys[:-1]: for k in keys[:-1]:
meta = meta[k] meta = meta[k]
del meta.content[key[-1]] # A hack because MetaMap doesn't have a __delitem__ del meta.content[key[-1]] # A hack because MetaMap doesn't have a __delitem__
def import_metadata(self, data, key: str=""):
if isinstance(data, dict) and isinstance(self.get_metadata(key), dict):
for subkey, value in enumerate(data):
self.import_metadata(value, key+"."+subkey if key != "" else subkey)
else:
self.set_metadata(key, data)
# This is a custom element which creates \begingroup \endgroup groups in TeX # This is a custom element which creates \begingroup \endgroup groups in TeX

View file

@ -10,6 +10,7 @@ import os
import re import re
import warnings import warnings
import importlib import importlib
import json
from .whitespace import NBSP from .whitespace import NBSP
from .elements import FQuoted from .elements import FQuoted
@ -469,6 +470,10 @@ class TransformProcessor:
module = importlib.import_module(matches.group(1)) module = importlib.import_module(matches.group(1))
module_name = matches.group(1) if matches.group(2) is None else matches.group(2) module_name = matches.group(1) if matches.group(2) is None else matches.group(2)
self.context.add_commands_from_module(module, module_name) self.context.add_commands_from_module(module, module_name)
elif e.attributes["type"] == "metadata":
data = json.load(open(self.context.dir + "/" + e.content[0].text[1:], "r"))
key = "" if not "key" in e.attributes else e.attributes["key"]
self.context.import_metadata(data, key)
else: else:
raise SyntaxError(f"`{e.attributes['type']}`: invalid import type") raise SyntaxError(f"`{e.attributes['type']}`: invalid import type")

22
test/test.json Normal file
View file

@ -0,0 +1,22 @@
[
{
"first_name": "Jan",
"last_name": "Černohorský",
"abbrev": "GS",
"gender": "M",
"contacts_public": {
"homepage": "https://grsc.cz"
}
},
{
"first_name": "Ondřej",
"last_name": "Machota",
"domestic_name": "Ondra",
"abbrev": "OM",
"gender": "M",
"contacts_public": {
"email": "ondrejmachota@gmail.com",
"discord": "ondrejmachota#0196"
}
}
]

View file

@ -11,6 +11,12 @@ lang: "en"
[#test-files/test-import.md]{type=md} [#test-files/test-import.md]{type=md}
[#test.json]{type=metadata key=orgs}
```python {.run}
return parse_string(f"Hello, {context.get_metadata('orgs')[0]['first_name']}!")
```
# Hello world! # Hello world!
## H2 ## H2
### H3 ### H3