Browse Source

Přidána možnost přidávání elementů do hlavičky a patičky stránky, užitečné pro používání formátítka pro standalone HTML.

citace
Jan Černohorský 1 year ago
parent
commit
96fabf6a14
  1. 4
      src/formatitko/html_generator.py
  2. 66
      src/formatitko/output_generator.py
  3. 15
      src/formatitko/transform_processor.py
  4. 2
      test/test.md

4
src/formatitko/html_generator.py

@ -101,10 +101,10 @@ class HTMLGenerator(OutputGenerator):
def generate_Null(self, e: Null):
pass
def generate_Doc(self, e: Doc):
#def generate_Doc(self, e: Doc):
# formatter = HtmlFormatter(style=e.get_metadata("highlight-style") if e.get_metadata("highlight-style") is not None else "default")
# self.generate_simple_tag(tag="style", attributes={}, content=formatter.get_style_defs(".highlight"))
self.generate_simple_tag(e, tag="main")
#self.generate_simple_tag(e, tag="main")
def generate_CodeBlock(self, e: CodeBlock):
lexer = None

66
src/formatitko/output_generator.py

@ -2,6 +2,7 @@ from panflute import Element, ListContainer, Inline, Block
from panflute import Cite, Code, Emph, Image, LineBreak, Link, Math, Note, Quoted, RawInline, SmallCaps, SoftBreak, Space, Span, Str, Strikeout, Strong, Subscript, Superscript, Underline
from panflute import BlockQuote, BulletList, Citation, CodeBlock, Definition, DefinitionItem, DefinitionList, Div, Figure, Header, HorizontalRule, LineBlock, LineItem, ListItem, MetaBlocks, MetaBool, MetaInlines, MetaList, MetaMap, MetaString, Null, OrderedList, Para, Plain, RawBlock, Table, TableBody, TableFoot, TableHead
from panflute import TableRow, TableCell, Caption, Doc
from panflute import MetaValue
from typing import Union, Callable
from .whitespace import NBSP
@ -53,12 +54,6 @@ class OutputGenerator:
Header: self.generate_Header,
HorizontalRule: self.generate_HorizontalRule,
LineBlock: self.generate_LineBlock,
MetaBlocks: self.generate_MetaBlocks,
MetaBool: self.generate_MetaBool,
MetaInlines: self.generate_MetaInlines,
MetaList: self.generate_MetaList,
MetaMap: self.generate_MetaMap,
MetaString: self.generate_MetaString,
Null: self.generate_Null,
OrderedList: self.generate_OrderedList,
Para: self.generate_Para,
@ -97,6 +92,14 @@ class OutputGenerator:
InlineGroup: self.generate_InlineGroup
}
self.TYPE_DICT_META = {
MetaBlocks: self.generate_MetaBlocks,
MetaBool: self.generate_MetaBool,
MetaInlines: self.generate_MetaInlines,
MetaMap: self.generate_MetaMap,
MetaString: self.generate_MetaString,
}
def generate(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]]):
if isinstance(e, Group):
old_context = self.context
@ -109,6 +112,10 @@ class OutputGenerator:
self.generate_Inline(e)
elif isinstance(e, Block):
self.generate_Block(e)
elif isinstance(e, MetaValue):
self.generate_MetaValue(e)
elif isinstance(e, MetaList):
self.generate_MetaList(e)
else:
try:
self.TYPE_DICT_MISC[type(e)](e)
@ -231,6 +238,31 @@ class OutputGenerator:
for el in e:
self.generate(el)
def generate_MetaList(self, e: MetaList):
for child in e:
self.generate(child)
def generate_MetaValue(self, e: MetaValue):
try:
self.TYPE_DICT_META[type(e)](e)
except KeyError:
self.generate(e.content)
def generate_MetaBlocks(self, e: MetaBlocks):
self.generate(e.content)
def generate_MetaInlines(self, e: MetaInlines):
self.generate(e.content)
def generate_MetaBool(self, e: MetaBool):
self.generate_simple_tag(e)
def generate_MetaMap(self, e: MetaMap):
self.generate_simple_tag(e)
def generate_MetaString(self, e: MetaString):
self.generate_simple_tag(e)
def generate_Inline(self, e: Inline):
self.TYPE_DICT_INLINE[type(e)](e)
@ -370,24 +402,6 @@ class OutputGenerator:
def generate_ListItem(self, e: ListItem):
self.generate_simple_tag(e)
def generate_MetaBlocks(self, e: MetaBlocks):
self.generate_simple_tag(e)
def generate_MetaBool(self, e: MetaBool):
self.generate_simple_tag(e)
def generate_MetaInlines(self, e: MetaInlines):
self.generate_simple_tag(e)
def generate_MetaList(self, e: MetaList):
self.generate_simple_tag(e)
def generate_MetaMap(self, e: MetaMap):
self.generate_simple_tag(e)
def generate_MetaString(self, e: MetaString):
self.generate_simple_tag(e)
def generate_OrderedList(self, e: OrderedList):
self.generate_simple_tag(e)
@ -416,7 +430,11 @@ class OutputGenerator:
self.generate_simple_tag(e)
def generate_Doc(self, e: Doc):
if "header_content" in e.metadata:
self.generate(e.metadata["header_content"])
self.generate_simple_tag(e)
if "footer_content" in e.metadata:
self.generate(e.metadata["footer_content"])
def generate_BlockGroup(self, e: BlockGroup):
self.generate_simple_tag(e)

15
src/formatitko/transform_processor.py

@ -2,6 +2,7 @@ from panflute import Element, ListContainer, Inline, Block
from panflute import Cite, Code, Emph, Image, LineBreak, Link, Math, Note, Quoted, RawInline, SmallCaps, SoftBreak, Space, Span, Str, Strikeout, Strong, Subscript, Superscript, Underline
from panflute import BlockQuote, BulletList, Citation, CodeBlock, Definition, DefinitionItem, DefinitionList, Div, Figure, Header, HorizontalRule, LineBlock, LineItem, ListItem, MetaBlocks, MetaBool, MetaInlines, MetaList, MetaMap, MetaString, Null, OrderedList, Para, Plain, RawBlock, Table, TableBody, TableFoot, TableHead
from panflute import TableRow, TableCell, Caption, Doc
from panflute import MetaValue
from typing import Union, Callable
from types import ModuleType
@ -409,6 +410,20 @@ class TransformProcessor:
elif e.attributes["type"] in ["tex", "html"]:
return RawBlock(text, e.attributes["type"])
if "header_content" in e.classes:
header_content = self.context.get_metadata("header_content")
header_content = [] if header_content is None else header_content
header_content.append(MetaBlocks(*self.transform(e.content)))
self.context.set_metadata("header_content", header_content)
return Null()
if "footer_content" in e.classes:
footer_content = self.context.get_metadata("footer_content")
footer_content = [] if footer_content is None else footer_content
footer_content.append(MetaBlocks(*self.transform(e.content)))
self.context.set_metadata("footer_content", footer_content)
return Null()
if "lang" in e.attributes:
warnings.warn("To set language in a way formátítko will understand, this Div has to have the `.group` class and be a Group.", UserWarning)

2
test/test.md

@ -4,8 +4,10 @@ subtitle: 'A subtitle'
are_we_there_yet: False
lang: "en"
---
:::: {.header_content}
::: {partial="test-top.html" type="html"}
:::
::::
[#test-files/test-import.md]{type=md}

Loading…
Cancel
Save