From 67a89d252b9c88de95eba60020a07a67091cc333 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Thu, 27 Jul 2023 14:49:29 +0200 Subject: [PATCH] =?UTF-8?q?#21:=20Ukazatel=20na=20kontext=20se=20nyn=C3=AD?= =?UTF-8?q?=20propaguje=20i=20do=20OG.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/formatitko/context.py | 3 ++- src/formatitko/formatitko.py | 2 +- src/formatitko/output_generator.py | 6 ++++++ src/formatitko/transform.py | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/formatitko/context.py b/src/formatitko/context.py index 96e112e..9eac008 100644 --- a/src/formatitko/context.py +++ b/src/formatitko/context.py @@ -100,7 +100,8 @@ class Context: # # Whenever a new context is created, its content should be eclosed in a group and vice-versa. class Group(Div): - def __init__(self, *args, metadata={}, **kwargs): + def __init__(self, *args, context:Context, metadata={}, **kwargs): self.metadata = metadata + self.context = context super().__init__(*args, **kwargs) diff --git a/src/formatitko/formatitko.py b/src/formatitko/formatitko.py index 03dfe49..ba7a95e 100755 --- a/src/formatitko/formatitko.py +++ b/src/formatitko/formatitko.py @@ -47,7 +47,7 @@ def main(): # Now wrap the document contents in a group, which is able to pop its language # setting out to TeX - doc.content = [Group(*doc.content, metadata={"language":language})] + doc.content = [Group(*doc.content, context=context, metadata={"language":language})] # Initialize the image processor (this just keeps some basic state) imageProcessor = ImageProcessor(args.img_public_dir, args.img_web_path, args.img_cache_dir, *args.img_lookup_dirs) diff --git a/src/formatitko/output_generator.py b/src/formatitko/output_generator.py index 783e790..ba5a393 100644 --- a/src/formatitko/output_generator.py +++ b/src/formatitko/output_generator.py @@ -20,6 +20,7 @@ class OutputGenerator: self.indent_str = indent_str self.indent_level = initial_indent_level self._at_start_of_line = True + self.current_context = None self.TYPE_DICT_MISC = { TableRow: self.generate_TableRow, @@ -87,6 +88,9 @@ class OutputGenerator: } def generate(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]]): + if isinstance(e, Group): + old_context = self.current_context + self.current_context = e.context if isinstance(e, list): for el in e: self.generate(el) @@ -101,6 +105,8 @@ class OutputGenerator: self.TYPE_DICT_MISC[type(e)](e) except KeyError: raise UnknownElementError(type(e)) + if isinstance(e, Group): + self.current_context = old_context def escape_special_chars(self, text: str) -> str: return text diff --git a/src/formatitko/transform.py b/src/formatitko/transform.py index e89447d..9dc824d 100644 --- a/src/formatitko/transform.py +++ b/src/formatitko/transform.py @@ -72,7 +72,7 @@ def transform(e: Element, c: Context) -> Element: nContext = Context(includedDoc, path, c, trusted=trusted) language = includedDoc.get_metadata("language") includedDoc = includedDoc.walk(transform, nContext) - e = Group(*includedDoc.content, metadata={"language": language}) + e = Group(*includedDoc.content, context=nContext, metadata={"language": language}) # Transform panflute's Quoted to custom FQuoted, see above. if isinstance(e, Quoted):