Browse Source

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

katex-socket
Jan Černohorský 1 year ago
parent
commit
32c2fb3bbc
  1. 8
      src/formatitko/context.py
  2. 5
      src/formatitko/transform_processor.py
  3. 22
      test/test.json
  4. 6
      test/test.md

8
src/formatitko/context.py

@ -101,6 +101,14 @@ class Context:
for k in keys[:-1]:
meta = meta[k]
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

5
src/formatitko/transform_processor.py

@ -10,6 +10,7 @@ import os
import re
import warnings
import importlib
import json
from .whitespace import NBSP
from .elements import FQuoted
@ -469,6 +470,10 @@ class TransformProcessor:
module = importlib.import_module(matches.group(1))
module_name = matches.group(1) if matches.group(2) is None else matches.group(2)
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:
raise SyntaxError(f"`{e.attributes['type']}`: invalid import type")

22
test/test.json

@ -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"
}
}
]

6
test/test.md

@ -11,6 +11,12 @@ lang: "en"
[#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!
## H2
### H3

Loading…
Cancel
Save