Browse Source

Include přímo TeXových a HTML souborů. Resolve #25

citace
Jan Černohorský 1 year ago
parent
commit
81a5c6d304
  1. 8
      src/formatitko/transform_processor.py
  2. 3
      test/test.md

8
src/formatitko/transform_processor.py

@ -368,7 +368,7 @@ class TransformProcessor:
else:
return BlockGroup(*content, context=new_context)
def transform_Div(self, e: Div) -> Union[Div, Group, Null]:
def transform_Div(self, e: Div) -> Union[Div, Group, Null, RawBlock]:
e.content = self.transform(e.content)
if "group" in e.classes:
@ -389,6 +389,8 @@ class TransformProcessor:
if "partial" in e.attributes:
# `partial` attribute
# Used to include a file which is executed in a different (child) Context.
if not "type" in e.attributes:
e.attributes["type"] = "md"
if not self.context.trusted: # If we're in an untrusted context, we shouldn't allow inclusion of files outside the PWD.
full_path = os.path.abspath(self.context.dir + "/" + e.attributes["partial"])
pwd = os.path.abspath(".")
@ -396,6 +398,7 @@ class TransformProcessor:
return nullify(e)
text = open(self.context.dir + "/" + e.attributes["partial"], "r").read()
path = self.context.dir + "/" + e.attributes["partial"]
if e.attributes["type"] == "md":
includedDoc = import_md(text)
trusted = True
if "untrusted" in e.attributes and (e.attributes["untrusted"] == True or e.attributes["untrusted"] == 'True'):
@ -403,6 +406,8 @@ class TransformProcessor:
if not self.context.trusted:
trusted = False
return self.create_Group(*includedDoc.content, new_context=Context(includedDoc, path, self.context, trusted=trusted))
elif e.attributes["type"] in ["tex", "html"]:
return RawBlock(text, e.attributes["type"])
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)
@ -412,7 +417,6 @@ class TransformProcessor:
def transform_Span(self, e: Span) -> Span:
e.content = self.transform(e.content)
# TODO: This sadly doesn't work. We would need to create a separate class InlineGroup, that would be Inline.
if "group" in e.classes:
# `.group` class for Spans
# Content of Span is enclosed in a separate context, all attributes are passed as metadata

3
test/test.md

@ -4,6 +4,9 @@ subtitle: 'A subtitle'
are_we_there_yet: False
lang: "en"
---
::: {partial="test-top.html" type="html"}
:::
[#test-files/test-import.md]{type=md}
# Hello world!

Loading…
Cancel
Save