Skip to content
Snippets Groups Projects
Commit 12510933 authored by Jiří Kalvoda's avatar Jiří Kalvoda
Browse files

Update na novou verzi formátitka

parent 5c60027d
No related branches found
No related tags found
No related merge requests found
......@@ -13,13 +13,13 @@ import plotly.graph_objects as go
import formatitko
from formatitko.transform import transform
from formatitko.util import import_md
from formatitko.context import Context, BlockGroup
from formatitko.images import ImageProcessor
from formatitko.output_generator import OutputGenerator
from formatitko.images import ImageProcessor, ImageProcessorNamespace
from formatitko.output_generator import OutputGenerator, FormatitkoRecursiveError
from formatitko.transform_processor import TransformProcessor
from formatitko.tex_generator import UCWTexGenerator
tracebacklimit=100
from panflute import convert_text
import panflute as pf
......@@ -38,7 +38,7 @@ def formatitko_command(f):
return f
@formatitko_command
def formatitko_code_and_output(element, context):
def formatitko_code_and_output(element, context, processor):
import panflute as pf
code = element.text
old_stdout = sys.stdout
......@@ -50,14 +50,14 @@ def formatitko_code_and_output(element, context):
for name in ["highlight"]:
if name in element.classes:
attributes[name] = element.classes["name"]
return [
return processor.transform([
pf.CodeBlock(code, classes=element.classes, attributes=attributes),
pf.Para(pf.Str("Output:")),
pf.CodeBlock(output.getvalue(), classes=["text"], attributes={"highlight":"True"})
]
])
@formatitko_command
def formatitko_plotly(element, context):
def formatitko_plotly(element, context, processor):
code = element.text
globals = {"plotly": plotly_lib, "go": go, "fig": go.Figure()}
......@@ -68,24 +68,24 @@ def formatitko_plotly(element, context):
plotly_lib.io.kaleido.scope.mathjax = None
plotly_lib.io.write_image(fig, file, format='pdf', width=650)
return [pf.Image(url=file)]
return processor.transform([pf.Image(url=file)])
@formatitko_command
def formatitko_sage(element, context):
def formatitko_sage(element, context, processor):
import sage.all
code = element.text
file = element.attributes["name"] + ".pdf"
r = sage.all.sage_eval("r", cmds=code)
r.save(file)
return [pf.Image(url=file)]
r.save(file, figsize=3)
return processor.transform([pf.Image(url=file)])
@formatitko_command
def formatitko_head(element, context):
def formatitko_head(element, context, processor):
keywords = "\n".join(f" /field{i+1} {{{v}}}" for i,v in enumerate(context.get_metadata("ft.keywords") + context.get_metadata("ft.en.keywords")))
return [pf.RawBlock(f"""
return processor.transform([pf.RawBlock(f"""
\\startmetadata
dc:creator {{{context.get_metadata("ft.author")}}}
dc:publisher {{Univerzita Karlova}}
......@@ -118,25 +118,37 @@ def formatitko_head(element, context):
\\def\\ftSupervisorDepartmentEN{{{context.get_metadata("ft.en.supervisor_department") or context.get_metadata("ft.en.department")}}}
\\input head.tex
""", format="tex"), ]
""", format="tex"), ])
@formatitko_command
def formatitko_tail(element, context):
def formatitko_tail(element, context, processor):
return []
def import_md(s: str, standalone: bool=True, bibliography=None):
from panflute import Element, Block, Inline, Null, Str, Doc, convert_text, Para, Plain
extra_args = []
if bibliography:
extra_args += ["-C", "--bibliography="+str(bibliography)]
return convert_text(s, standalone=standalone, input_format="markdown-definition_lists", extra_args=extra_args)
def main():
# Use panflute to parse the input MD file
doc = import_md(open("bakalarka/index.md", "r").read())
doc = import_md(open("bakalarka/index.md", "r").read(), bibliography="bakalarka/sample.bib")
OutputGenerator(sys.stdout).generate(doc)
tp = TransformProcessor("bakalarka/index.md")
tp.add_command_module(formatitko_commands)
try:
doc = tp.transform(doc)
except FormatitkoRecursiveError as e:
e.pretty_print(tracebacklimit=tracebacklimit)
return 1
imageProcessor = ImageProcessor({"": ImageProcessorNamespace("img_public_dir", "img_web_path", "img_cache_dir", ["."], True)})
imageProcessor = ImageProcessor("img_public_dir", "img_web_path", "img_cache_dir", ".")
with open("formatitko.tex", "w") as file:
d = pathlib.Path("/".join(formatitko.__file__.split("/")[:-1]))
......@@ -144,7 +156,11 @@ def main():
file.write(source.read())
with open("bakalarka.tex", "w") as file:
try:
UCWTexGenerator(file, imageProcessor).generate(doc)
except FormatitkoRecursiveError as e:
e.pretty_print(tracebacklimit=tracebacklimit)
return 1
subprocess.run(["luatex", "bakalarka"], check=True)
......
import panflute as pf
from formatitko.command_env import parse_string
from formatitko.elements import Slanted
formatitko_commands={}
def formatitko_command(f):
......@@ -9,39 +10,42 @@ def formatitko_command(f):
return f
def make_emph(*elements):
def make_emph(*elements, wrap_class=pf.Emph):
out = []
for e in elements:
if isinstance(e, pf.Para) or isinstance(e, pf.Plain):
out.append(type(e)(pf.Emph(*e.content)))
out.append(type(e)(wrap_class(*e.content)))
elif isinstance(e, pf.BulletList):
out.append(type(e)(*(pf.ListItem(*make_emph(*i.content)) for i in e.content)))
out.append(type(e)(*(pf.ListItem(*make_emph(*i.content, wrap_class=wrap_class)) for i in e.content)))
else:
print(e, type(e))
raise NotImplemented()
return out
@formatitko_command
def box(element, context):
def box(element, context, processor):
content = processor.transform(element.content)
title = {
'fact': "Fakt",
'task': "Úloha",
'algo': "Algoritmus",
'theorem': "Věta",
'lemma': "Lemma"
}[element.attributes["t"]]
out = []
for e in element.content:
for e in content:
if not out:
if isinstance(e, pf.Para):
out.append(pf.Para(pf.Strong(*parse_string(f"{title}: ")), pf.Emph(*e.content)))
out.append(pf.Para(pf.Strong(*parse_string(f"{title}: ")), Slanted(*e.content)))
else:
raise NotImplemented()
else:
out += make_emph(e)
out += make_emph(e, wrap_class=Slanted)
return out
@formatitko_command
def proof(element, context):
out = [i for i in element.content]
def proof(element, context, processor):
out = processor.transform(element.content)
if isinstance(out[0], pf.Para):
out[0] = pf.Para(pf.Emph(*parse_string(f"Důkaz: ")), *out[0].content)
else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment