Browse Source

Separate context init for transform processor into separate function, add option to not make images clickable.

master
Jan Černohorský 9 months ago
parent
commit
f0d939a65b
  1. 4
      src/formatitko/html_generator.py
  2. 20
      src/formatitko/transform_processor.py

4
src/formatitko/html_generator.py

@ -208,6 +208,10 @@ class HTMLGenerator(OutputGenerator):
else: else:
attributes["src"] = url attributes["src"] = url
if e.attributes["no-img-link"]:
self.write(self.single_tag("img", attributes))
return
img = RawInline(self.single_tag("img", attributes)) img = RawInline(self.single_tag("img", attributes))
link = Link(img, url=url) link = Link(img, url=url)

20
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=""): def add_command_module(self, module: Union[dict[str, CommandCallable], ModuleType], module_name: str=""):
self._command_modules.append((module, module_name)) 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]]: def get_pretransformers(self) -> list[Callable[[ELCl],ELCl]]:
return super().get_pretransformers()+[self.handle_if_attribute, self.handle_ifnot_attribute] return super().get_pretransformers()+[self.handle_if_attribute, self.handle_ifnot_attribute]
@ -71,12 +80,7 @@ class TransformProcessor(NOPProcessor):
raise err raise err
def transform_Doc(self, e: Doc) -> Doc: def transform_Doc(self, e: Doc) -> Doc:
if self.context is not None: self.init_context(e)
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)]
e.content = self.transform(e.content) e.content = self.transform(e.content)
return e return e
@ -95,8 +99,10 @@ class TransformProcessor(NOPProcessor):
e.content = self.transform(e.content) 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 # 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. # 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 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 return e
def create_Group(self, *content, new_context: Context, replaced:Element, inline: bool=False) -> Group: def create_Group(self, *content, new_context: Context, replaced:Element, inline: bool=False) -> Group:

Loading…
Cancel
Save