Added FileLink element for publishing and linking local files.

This commit is contained in:
Jan Černohorský 2024-03-07 17:58:18 +01:00
parent 42d04b77de
commit 0e5735cba2
5 changed files with 36 additions and 4 deletions

View file

@ -1,4 +1,4 @@
from panflute import Quoted from panflute import Quoted, Link
from .command import Command, InlineCommand, BlockCommand, CodeCommand from .command import Command, InlineCommand, BlockCommand, CodeCommand
@ -14,3 +14,6 @@ class FQuoted(Quoted):
del kwargs["style"] del kwargs["style"]
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
class FileLink(Link):
pass

View file

@ -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):
@ -219,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)

View file

@ -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:

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 from .elements import FQuoted, 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
@ -88,6 +88,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,
FileLink: self.transform_FileLink,
InlineCommand: self.transform_InlineCommand, InlineCommand: self.transform_InlineCommand,
BlockCommand: self.transform_BlockCommand, BlockCommand: self.transform_BlockCommand,
@ -299,6 +300,10 @@ class NOPProcessor:
e.content = self.transform(e.content) e.content = self.transform(e.content)
return e return 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)

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 from .elements import FQuoted, FileLink
from .context import Group, InlineGroup, BlockGroup, Context from .context import Group, InlineGroup, BlockGroup, Context
@ -127,6 +127,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,
FileLink: self.generate_FileLink,
InlineGroup: self.generate_InlineGroup InlineGroup: self.generate_InlineGroup
} }
@ -367,7 +368,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):