Odkazy na interní objekty (obrázky)

This commit is contained in:
Jiří Kalvoda 2024-02-27 12:42:23 +01:00
parent a2468b54e1
commit f1f2f63cb5
6 changed files with 32 additions and 6 deletions

View file

@ -72,6 +72,7 @@ class Context:
self.set_data('figure_number_generator', default_figure_number_generator) self.set_data('figure_number_generator', default_figure_number_generator)
self.set_data('section_counters', [0 for i in range(6)]) self.set_data('section_counters', [0 for i in range(6)])
self.set_data('figure_counters', {}) self.set_data('figure_counters', {})
self.set_data('obj_map', {})
def get_command(self, command: str) -> Union[CommandCallable, None]: def get_command(self, command: str) -> Union[CommandCallable, None]:
if command in self._commands: if command in self._commands:

View file

@ -1,4 +1,4 @@
from panflute import Quoted, Emph from panflute import Quoted, Emph, Link
from .command import Command, InlineCommand, BlockCommand, CodeCommand from .command import Command, InlineCommand, BlockCommand, CodeCommand
@ -16,3 +16,10 @@ class FQuoted(Quoted):
class Slanted(Emph): class Slanted(Emph):
pass 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)

View file

@ -6,7 +6,7 @@ from panflute import MetaValue
from typing import Union, Callable from typing import Union, Callable
from .whitespace import NBSP from .whitespace import NBSP
from .elements import FQuoted, Slanted from .elements import FQuoted, Slanted, FLink
from .context import Group, InlineGroup, BlockGroup, Context from .context import Group, InlineGroup, BlockGroup, Context
from .whitespace import Whitespace from .whitespace import Whitespace
from .command import BlockCommand, InlineCommand, CodeCommand, Command from .command import BlockCommand, InlineCommand, CodeCommand, Command
@ -89,6 +89,7 @@ class NOPProcessor:
Underline: self.transform_Underline, Underline: self.transform_Underline,
NBSP: self.transform_NBSP, NBSP: self.transform_NBSP,
FQuoted: self.transform_FQuoted, FQuoted: self.transform_FQuoted,
FLink: self.transform_FLink,
InlineCommand: self.transform_InlineCommand, InlineCommand: self.transform_InlineCommand,
BlockCommand: self.transform_BlockCommand, BlockCommand: self.transform_BlockCommand,
@ -304,6 +305,9 @@ class NOPProcessor:
e.content = self.transform(e.content) e.content = self.transform(e.content)
return e return e
def transform_FLink(self, e: FLink) -> FLink:
return self.transform_Link(e)
def transform_Figure(self, e: Figure) -> Figure: def transform_Figure(self, e: Figure) -> Figure:
e.content = self.transform(e.content) e.content = self.transform(e.content)
e.caption = self.transform(e.caption) e.caption = self.transform(e.caption)

View file

@ -7,7 +7,7 @@ from panflute import stringify
from typing import Union, Callable from typing import Union, Callable
from .whitespace import NBSP from .whitespace import NBSP
from .elements import FQuoted, Slanted from .elements import FQuoted, Slanted, FLink
from .context import Group, InlineGroup, BlockGroup, Context from .context import Group, InlineGroup, BlockGroup, Context
@ -128,6 +128,7 @@ class OutputGenerator:
Underline: self.generate_Underline, Underline: self.generate_Underline,
NBSP: self.generate_NBSP, NBSP: self.generate_NBSP,
FQuoted: self.generate_FQuoted, FQuoted: self.generate_FQuoted,
FLink: self.generate_FLink,
InlineGroup: self.generate_InlineGroup InlineGroup: self.generate_InlineGroup
} }
@ -385,6 +386,9 @@ class OutputGenerator:
def generate_Link(self, e: Link): def generate_Link(self, e: Link):
self.generate_simple_tag(e) self.generate_simple_tag(e)
def generate_FLink(self, e: FLink):
self.generate_Link(e)
def generate_Note(self, e: Note): def generate_Note(self, e: Note):
self.generate_simple_tag(e) self.generate_simple_tag(e)

View file

@ -194,7 +194,7 @@ class UCWTexGenerator(OutputGenerator):
self.write(r"}") self.write(r"}")
def generate_Caption(self, e: Caption): def generate_Caption(self, e: Caption):
self.generate_Emph(e) self.generate_Slanted(e)
def generate_Math(self, e: Math): def generate_Math(self, e: Math):
if e.format == "DisplayMath": if e.format == "DisplayMath":
@ -323,6 +323,11 @@ class UCWTexGenerator(OutputGenerator):
self.writeln(r"}") self.writeln(r"}")
def generate_Link(self, e: Link): 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: if len(e.content) == 1 and isinstance(e.content[0], Str) and e.content[0].text == e.url:
self.write(r"\url{") self.write(r"\url{")
else: else:

View file

@ -14,7 +14,7 @@ import importlib
import json import json
from .whitespace import NBSP from .whitespace import NBSP
from .elements import FQuoted, Slanted from .elements import FQuoted, Slanted, FLink
from .context import Group, InlineGroup, BlockGroup from .context import Group, InlineGroup, BlockGroup
from .util import nullify, import_md from .util import nullify, import_md
from .context import Context, CommandCallable 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) e.attributes["number"] = self.context.get_data("section_number_generator")(e, self.context)
return e return e
def transform_Figure(self, e:Figure) -> Figure: def transform_Figure(self, e: Figure) -> Figure:
if "number" not in e.attributes: if "number" not in e.attributes:
if 'unnumbered' in e.classes: if 'unnumbered' in e.classes:
e.attributes["number"] = "" e.attributes["number"] = ""
else: else:
e.attributes["number"] = self.context.get_data("figure_number_generator")(e, self.context) 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) 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)