From f0d939a65b61ac57d8ede780e142331ae3e674ac Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Fri, 23 Feb 2024 23:37:22 +0100 Subject: [PATCH] Separate context init for transform processor into separate function, add option to not make images clickable. --- src/formatitko/html_generator.py | 4 ++++ src/formatitko/transform_processor.py | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/formatitko/html_generator.py b/src/formatitko/html_generator.py index 280c60e..2aabe85 100644 --- a/src/formatitko/html_generator.py +++ b/src/formatitko/html_generator.py @@ -208,6 +208,10 @@ class HTMLGenerator(OutputGenerator): else: attributes["src"] = url + if e.attributes["no-img-link"]: + self.write(self.single_tag("img", attributes)) + return + img = RawInline(self.single_tag("img", attributes)) link = Link(img, url=url) diff --git a/src/formatitko/transform_processor.py b/src/formatitko/transform_processor.py index 9b7bc67..6ce7999 100644 --- a/src/formatitko/transform_processor.py +++ b/src/formatitko/transform_processor.py @@ -40,6 +40,15 @@ class TransformProcessor(NOPProcessor): def add_command_module(self, module: Union[dict[str, CommandCallable], ModuleType], module_name: str=""): self._command_modules.append((module, module_name)) + def init_context(self, e: Doc) -> Context: + if self.context is not None: + raise DoubleDocError() + self.context = Context(e, self.root_file_path) + for module, module_name in self._command_modules: + self.context.add_commands_from_module(module, module_name) + e.content = [BlockGroup(*e.content, context=self.context)] + return self.context + def get_pretransformers(self) -> list[Callable[[ELCl],ELCl]]: return super().get_pretransformers()+[self.handle_if_attribute, self.handle_ifnot_attribute] @@ -71,12 +80,7 @@ class TransformProcessor(NOPProcessor): raise err def transform_Doc(self, e: Doc) -> Doc: - if self.context is not None: - raise DoubleDocError() - self.context = Context(e, self.root_file_path) - for module, module_name in self._command_modules: - self.context.add_commands_from_module(module, module_name) - e.content = [BlockGroup(*e.content, context=self.context)] + self.init_context(e) e.content = self.transform(e.content) return e @@ -95,8 +99,10 @@ class TransformProcessor(NOPProcessor): e.content = self.transform(e.content) # OG now has Context so this is not needed per se, but I'm keeping this here for the handling of attribute > context > default value # Pass down "no-srcset" metadatum as attribute down to images. - if not "no-srcset" in e.attributes: + if "no-srcset" not in e.attributes: e.attributes["no-srcset"] = self.context.get_metadata("no-srcset") if self.context.get_metadata("no-srcset") is not None else False + if "no-img-link" not in e.attributes: + e.attributes["no-img-link"] = self.context.get_metadata("no-img-link") if self.context.get_metadata("no-img-link") is not None else False return e def create_Group(self, *content, new_context: Context, replaced:Element, inline: bool=False) -> Group: