feature: ability to detect includes

This commit is contained in:
Prof. Dr. Matthias Jung DL9MJ
2025-06-24 22:04:14 +02:00
committed by Tim Grelka
parent 5d0084bac5
commit 2d0dc3e1fc
3 changed files with 44 additions and 1 deletions
+13 -1
View File
@@ -6,6 +6,7 @@ from mistletoe import Document, HtmlRenderer
from .comment import BlockComment from .comment import BlockComment
from .dash import Dash from .dash import Dash
from .halfwidth_spaces import HalfwidthSpaces from .halfwidth_spaces import HalfwidthSpaces
from .include import Include
from .morse import Morse from .morse import Morse
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
from .photo import Photo from .photo import Photo
@@ -28,7 +29,13 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
section_url = "section.html" section_url = "section.html"
ref_id = 0 ref_id = 0
def __init__(self, question_renderer=None, picture_handler=None, photo_handler=None): def __init__(
self,
question_renderer=None,
picture_handler=None,
photo_handler=None,
include_handler=None
) :
super().__init__( super().__init__(
Dash, Dash,
BlockComment, BlockComment,
@@ -46,10 +53,12 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
Photo, Photo,
Table, Table,
Qso, Qso,
Include
) )
self.question_renderer = question_renderer self.question_renderer = question_renderer
self.picture_handler = picture_handler self.picture_handler = picture_handler
self.photo_handler = photo_handler self.photo_handler = photo_handler
self.include_handler = include_handler
def render_dash(self, token): def render_dash(self, token):
return " – " return " – "
@@ -227,3 +236,6 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
table += "</table>" table += "</table>"
return table return table
def render_include(self, token):
return self.include_handler(token.content)
+13
View File
@@ -0,0 +1,13 @@
import re
from mistletoe.span_token import SpanToken
class Include(SpanToken):
"""
Span token ("[include:ident]").
Includes raw html or javascript code from a file with the given identifier.
"""
parse_inner = False
pattern = re.compile(r'\[include:\s*([^\]]+)\]')
+18
View File
@@ -0,0 +1,18 @@
import pytest
from mistletoe import Document
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
@pytest.mark.html
def test_include_html():
assertions = {
'[include:ident]': "<p>ident</p>\n",
}
def test_function(input):
return f"{input}"
with FiftyOhmHtmlRenderer(include_handler=test_function) as renderer:
for assertion in assertions:
assert renderer.render(Document(assertion)) == assertions[assertion]