diff --git a/src/formatitko/context.py b/src/formatitko/context.py index 29dd952..1e0a9e9 100644 --- a/src/formatitko/context.py +++ b/src/formatitko/context.py @@ -72,6 +72,7 @@ class Context: self.set_data('figure_number_generator', default_figure_number_generator) self.set_data('section_counters', [0 for i in range(6)]) self.set_data('figure_counters', {}) + self.set_data('obj_map', {}) def get_command(self, command: str) -> Union[CommandCallable, None]: if command in self._commands: diff --git a/src/formatitko/elements.py b/src/formatitko/elements.py index 1ce1526..bea6a2a 100644 --- a/src/formatitko/elements.py +++ b/src/formatitko/elements.py @@ -1,4 +1,4 @@ -from panflute import Quoted, Emph +from panflute import Quoted, Emph, Link from .command import Command, InlineCommand, BlockCommand, CodeCommand @@ -16,3 +16,10 @@ class FQuoted(Quoted): class Slanted(Emph): pass + +class FLink(Link): + obj_map: map + def __init__(self, *args, **kwargs): + self.obj_map = kwargs["obj_map"] + del kwargs["obj_map"] + super().__init__(*args, **kwargs) diff --git a/src/formatitko/nop_processor.py b/src/formatitko/nop_processor.py index 3c8af97..455bbc8 100644 --- a/src/formatitko/nop_processor.py +++ b/src/formatitko/nop_processor.py @@ -6,7 +6,7 @@ from panflute import MetaValue from typing import Union, Callable from .whitespace import NBSP -from .elements import FQuoted, Slanted +from .elements import FQuoted, Slanted, FLink from .context import Group, InlineGroup, BlockGroup, Context from .whitespace import Whitespace from .command import BlockCommand, InlineCommand, CodeCommand, Command @@ -89,6 +89,7 @@ class NOPProcessor: Underline: self.transform_Underline, NBSP: self.transform_NBSP, FQuoted: self.transform_FQuoted, + FLink: self.transform_FLink, InlineCommand: self.transform_InlineCommand, BlockCommand: self.transform_BlockCommand, @@ -304,6 +305,9 @@ class NOPProcessor: e.content = self.transform(e.content) return e + def transform_FLink(self, e: FLink) -> FLink: + return self.transform_Link(e) + def transform_Figure(self, e: Figure) -> Figure: e.content = self.transform(e.content) e.caption = self.transform(e.caption) diff --git a/src/formatitko/output_generator.py b/src/formatitko/output_generator.py index 022228e..67fd45f 100644 --- a/src/formatitko/output_generator.py +++ b/src/formatitko/output_generator.py @@ -7,7 +7,7 @@ from panflute import stringify from typing import Union, Callable from .whitespace import NBSP -from .elements import FQuoted, Slanted +from .elements import FQuoted, Slanted, FLink from .context import Group, InlineGroup, BlockGroup, Context @@ -128,6 +128,7 @@ class OutputGenerator: Underline: self.generate_Underline, NBSP: self.generate_NBSP, FQuoted: self.generate_FQuoted, + FLink: self.generate_FLink, InlineGroup: self.generate_InlineGroup } @@ -385,6 +386,9 @@ class OutputGenerator: def generate_Link(self, e: Link): self.generate_simple_tag(e) + + def generate_FLink(self, e: FLink): + self.generate_Link(e) def generate_Note(self, e: Note): self.generate_simple_tag(e) diff --git a/src/formatitko/tex_generator.py b/src/formatitko/tex_generator.py index 3bd56a9..0aa4bdd 100644 --- a/src/formatitko/tex_generator.py +++ b/src/formatitko/tex_generator.py @@ -194,7 +194,7 @@ class UCWTexGenerator(OutputGenerator): self.write(r"}") def generate_Caption(self, e: Caption): - self.generate_Emph(e) + self.generate_Slanted(e) def generate_Math(self, e: Math): if e.format == "DisplayMath": @@ -323,6 +323,11 @@ class UCWTexGenerator(OutputGenerator): self.writeln(r"}") def generate_Link(self, e: Link): + if len(e.content) == 0: + if e.url.startswith('#'): + obj = e.obj_map[e.url[1:]] + self.write(str(obj.attributes["number"])) + return if len(e.content) == 1 and isinstance(e.content[0], Str) and e.content[0].text == e.url: self.write(r"\url{") else: diff --git a/src/formatitko/transform_processor.py b/src/formatitko/transform_processor.py index 013a32e..d96915f 100644 --- a/src/formatitko/transform_processor.py +++ b/src/formatitko/transform_processor.py @@ -14,7 +14,7 @@ import importlib import json from .whitespace import NBSP -from .elements import FQuoted, Slanted +from .elements import FQuoted, Slanted, FLink from .context import Group, InlineGroup, BlockGroup from .util import nullify, import_md from .context import Context, CommandCallable @@ -319,10 +319,15 @@ class TransformProcessor(NOPProcessor): e.attributes["number"] = self.context.get_data("section_number_generator")(e, self.context) return e - def transform_Figure(self, e:Figure) -> Figure: + def transform_Figure(self, e: Figure) -> Figure: if "number" not in e.attributes: if 'unnumbered' in e.classes: e.attributes["number"] = "" else: e.attributes["number"] = self.context.get_data("figure_number_generator")(e, self.context) + self.context.get_data("obj_map")[e.identifier] = e return super().transform_Figure(e) + + def transform_Link(self, e: Link) -> Link: + e = FLink(*e.content, url=e.url, identifier=e.identifier, attributes=e.attributes, classes=e.classes, obj_map=self.context.get_data("obj_map")) + return super().transform_Link(e)