Browse Source

Přidána data na kontextu nezávislá na docu.

separate-katex-repo
Jan Černohorský 2 months ago
parent
commit
93f5949361
  1. 36
      src/formatitko/context.py

36
src/formatitko/context.py

@ -23,6 +23,7 @@ CommandCallable = Callable[[Command, 'Context', 'NOPProcessor'], list[Element]]
class Context:
parent: Union["Context", None]
_commands: dict[str, Union[CommandCallable, None]]
_data: dict[str, object]
doc: Doc
trusted: bool
path: str
@ -34,6 +35,7 @@ class Context:
def __init__(self, doc: Doc, path: str, parent: Union['Context', None]=None, trusted: bool=True):
self.parent = parent
self._commands = {}
self._data = {}
self.doc = doc
self.trusted = trusted
self.path = path
@ -113,6 +115,40 @@ class Context:
else:
self.set_metadata(key, data)
def get_data(self, key: str, immediate: bool=False):
data = self._data
keys = key.split(".")
try:
for k in keys:
data = data[k]
return data
except KeyError:
if self.parent and not immediate:
return self.parent.get_data(key)
else:
return None
def set_data(self, key: str, value: object):
data = self._data
keys = key.split(".")
for k in keys[:-1]:
try:
data = data[k]
except KeyError:
data[k] = {}
data = data[k]
data[keys[-1]] = value
def unset_data(self, key: str):
if key == "":
self._doc = {}
data = self._doc
keys = key.split(".")
for k in keys[:-1]:
data = data[k]
del data[keys[-1]]
# This is a custom element which creates \begingroup \endgroup groups in TeX

Loading…
Cancel
Save