Browse Source

Merge remote-tracking branch 'origin/master' into jk-bakalarka

Conflicts:
	src/formatitko/elements.py
	src/formatitko/nop_processor.py
	src/formatitko/output_generator.py

FileLink vs FLink
jk-bakalarka
Jiří Kalvoda 8 months ago
parent
commit
6d629137fb
  1. 3
      src/formatitko/elements.py
  2. 22
      src/formatitko/html_generator.py
  3. 3
      src/formatitko/images.py
  4. 7
      src/formatitko/nop_processor.py
  5. 11
      src/formatitko/output_generator.py

3
src/formatitko/elements.py

@ -23,3 +23,6 @@ class FLink(Link):
self.obj_map = kwargs["obj_map"] self.obj_map = kwargs["obj_map"]
del kwargs["obj_map"] del kwargs["obj_map"]
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
class FileLink(Link):
pass

22
src/formatitko/html_generator.py

@ -19,6 +19,7 @@ from .output_generator import OutputGenerator
from .katex import KatexClient from .katex import KatexClient
from .images import ImageProcessor, ImageProcessorNamespaceSearcher from .images import ImageProcessor, ImageProcessorNamespaceSearcher
from .util import inlinify from .util import inlinify
from .elements import FileLink
class HTMLGenerator(OutputGenerator): class HTMLGenerator(OutputGenerator):
@ -194,6 +195,8 @@ class HTMLGenerator(OutputGenerator):
attributes["width"] = e.attributes["width"] attributes["width"] = e.attributes["width"]
if "height" in e.attributes: if "height" in e.attributes:
attributes["height"] = e.attributes["height"] attributes["height"] = e.attributes["height"]
if "title" in e.attributes:
attributes["title"] = e.attributes["title"]
if e.title: if e.title:
attributes["alt"] = e.title attributes["alt"] = e.title
@ -202,7 +205,7 @@ class HTMLGenerator(OutputGenerator):
HTMLGenerator(fake_out, self.katexClient, self.imageProcessor).generate(e.content) HTMLGenerator(fake_out, self.katexClient, self.imageProcessor).generate(e.content)
attributes["alt"] = fake_out.getvalue() attributes["alt"] = fake_out.getvalue()
if len(srcset) != 0: if len(srcset) > 1:
attributes["src"] = srcset[-1][0] attributes["src"] = srcset[-1][0]
attributes["srcset"] = ", ".join([" ".join(src) for src in srcset]) attributes["srcset"] = ", ".join([" ".join(src) for src in srcset])
else: else:
@ -217,6 +220,23 @@ class HTMLGenerator(OutputGenerator):
self.generate(link) self.generate(link)
def generate_FileLink(self, e: FileLink):
url = e.url
# The directory of the current file relative to the current working directory
source_dir = self.context.dir
# The directory of the current file relative to the md file we were called on
rel_dir = self.context.rel_dir
searcher = self.imageProcessor.get_searcher_by_path(url, rel_dir, source_dir)
url = self.imageProcessor.get_path_without_namespace(url)
url = self.imageProcessor.process_image(url, "", searcher, self.context)
searcher.publish_image(url)
url = searcher.get_web_path() + "/" + url
self.generate_Link(Link(*e.content, url=url))
def generate_InlineGroup(self, e: InlineGroup): def generate_InlineGroup(self, e: InlineGroup):
self.generate_Group(e) self.generate_Group(e)

3
src/formatitko/images.py

@ -173,6 +173,9 @@ class ImageProcessor:
if format == "jpg": if format == "jpg":
format = "jpeg" format = "jpeg"
if format == "":
format = ext
# Locate all dependencies # Locate all dependencies
deps_full = [full_path] deps_full = [full_path]
for dep in deps: for dep in deps:

7
src/formatitko/nop_processor.py

@ -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, FLink from .elements import FQuoted, Slanted, FLink, FileLink
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
@ -90,6 +90,7 @@ class NOPProcessor:
NBSP: self.transform_NBSP, NBSP: self.transform_NBSP,
FQuoted: self.transform_FQuoted, FQuoted: self.transform_FQuoted,
FLink: self.transform_FLink, FLink: self.transform_FLink,
FileLink: self.transform_FileLink,
InlineCommand: self.transform_InlineCommand, InlineCommand: self.transform_InlineCommand,
BlockCommand: self.transform_BlockCommand, BlockCommand: self.transform_BlockCommand,
@ -308,6 +309,10 @@ class NOPProcessor:
def transform_FLink(self, e: FLink) -> FLink: def transform_FLink(self, e: FLink) -> FLink:
return self.transform_Link(e) return self.transform_Link(e)
def transform_FileLink(self, e: FileLink) -> FileLink:
e.content = self.transform(e.content)
return 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)

11
src/formatitko/output_generator.py

@ -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, FLink from .elements import FQuoted, Slanted, FLink, FileLink
from .context import Group, InlineGroup, BlockGroup, Context from .context import Group, InlineGroup, BlockGroup, Context
@ -129,6 +129,7 @@ class OutputGenerator:
NBSP: self.generate_NBSP, NBSP: self.generate_NBSP,
FQuoted: self.generate_FQuoted, FQuoted: self.generate_FQuoted,
FLink: self.generate_FLink, FLink: self.generate_FLink,
FileLink: self.generate_FileLink,
InlineGroup: self.generate_InlineGroup InlineGroup: self.generate_InlineGroup
} }
@ -274,9 +275,9 @@ class OutputGenerator:
self.write(self.end_tag(tag)) self.write(self.end_tag(tag))
def generate_raw_block_tag(self, tag: str, text: str, attributes: dict[str,str]={}): def generate_raw_block_tag(self, tag: str, text: str, attributes: dict[str,str]={}):
self.writeln(self.start_tag(tag, attributes)) self.writeraw(self.start_tag(tag, attributes))
self.writeraw(text) self.writeraw(text)
self.writeln(self.end_tag(tag)) self.writeraw(self.end_tag(tag))
def generate_empty_block_tag(self, tag: str, attributes: dict[str,str]={}): def generate_empty_block_tag(self, tag: str, attributes: dict[str,str]={}):
self.writeln(self.single_tag(tag, attributes)) self.writeln(self.single_tag(tag, attributes))
@ -369,7 +370,9 @@ class OutputGenerator:
self.write("\"") self.write("\"")
self.generate(e.content) self.generate(e.content)
self.write("\"") self.write("\"")
def generate_FileLink(self, e: FileLink):
self.generate_simple_tag(e)
# Inline Elements # Inline Elements
def generate_Cite(self, e: Cite): def generate_Cite(self, e: Cite):

Loading…
Cancel
Save