Browse Source

#21: Ukazatel na kontext se nyní propaguje i do OG.

pull/28/head
Jan Černohorský 1 year ago
parent
commit
67a89d252b
  1. 3
      src/formatitko/context.py
  2. 2
      src/formatitko/formatitko.py
  3. 6
      src/formatitko/output_generator.py
  4. 2
      src/formatitko/transform.py

3
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. # Whenever a new context is created, its content should be eclosed in a group and vice-versa.
class Group(Div): class Group(Div):
def __init__(self, *args, metadata={}, **kwargs): def __init__(self, *args, context:Context, metadata={}, **kwargs):
self.metadata = metadata self.metadata = metadata
self.context = context
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)

2
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 # Now wrap the document contents in a group, which is able to pop its language
# setting out to TeX # 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) # 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) imageProcessor = ImageProcessor(args.img_public_dir, args.img_web_path, args.img_cache_dir, *args.img_lookup_dirs)

6
src/formatitko/output_generator.py

@ -20,6 +20,7 @@ class OutputGenerator:
self.indent_str = indent_str self.indent_str = indent_str
self.indent_level = initial_indent_level self.indent_level = initial_indent_level
self._at_start_of_line = True self._at_start_of_line = True
self.current_context = None
self.TYPE_DICT_MISC = { self.TYPE_DICT_MISC = {
TableRow: self.generate_TableRow, TableRow: self.generate_TableRow,
@ -87,6 +88,9 @@ class OutputGenerator:
} }
def generate(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]]): 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): if isinstance(e, list):
for el in e: for el in e:
self.generate(el) self.generate(el)
@ -101,6 +105,8 @@ class OutputGenerator:
self.TYPE_DICT_MISC[type(e)](e) self.TYPE_DICT_MISC[type(e)](e)
except KeyError: except KeyError:
raise UnknownElementError(type(e)) raise UnknownElementError(type(e))
if isinstance(e, Group):
self.current_context = old_context
def escape_special_chars(self, text: str) -> str: def escape_special_chars(self, text: str) -> str:
return text return text

2
src/formatitko/transform.py

@ -72,7 +72,7 @@ def transform(e: Element, c: Context) -> Element:
nContext = Context(includedDoc, path, c, trusted=trusted) nContext = Context(includedDoc, path, c, trusted=trusted)
language = includedDoc.get_metadata("language") language = includedDoc.get_metadata("language")
includedDoc = includedDoc.walk(transform, nContext) 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. # Transform panflute's Quoted to custom FQuoted, see above.
if isinstance(e, Quoted): if isinstance(e, Quoted):

Loading…
Cancel
Save