EEx Engine and custom protocol#7
EEx Engine and custom protocol#7LostKobrakai wants to merge 6 commits intoHermanverschooten:mainfrom
Conversation
|
I really like this - it would have saved me a lot of headache when designing the name badge screens 😅 Another thought that I had was to use a modified version of the defmacro sigil_TYPST({:<<>>, meta, [expr]}, modifiers)
when modifiers == [] or modifiers == ~c"noformat" do
if not Macro.Env.has_var?(__CALLER__, {:assigns, nil}) do
raise "~TYPST requires a variable named \"assigns\" to exist and be set to a map"
end
options = [
engine: Typst.Engine,
file: __CALLER__.file,
line: __CALLER__.line + 1,
caller: __CALLER__,
indentation: meta[:indentation] || 0,
source: expr,
]
EEx.compile_string(expr, options)
endthis enables defining functions very similar to LiveView: defmodule SomeModule do
import Typst, only: [sigil_TYPST: 2]
def render_typst_markdown(assigns) do
~TYPST"""
#text(<%= @text_props %>)[<%= @text_content %>]
"""
end
end
SomeModule.render_typst_markdown(%{text_props: %{size: "16pt", font: "Helvetica"}, text_content: "Hello World"})I was testing this with |
|
Ah, I missed handling the assigns (really the only additional thing |
|
@LostKobrakai yeah I think the assigns would be nice. Using the custom engine is also good, because eventually maybe we can get the engine to check if the syntax is valid, similar to HEEx. But that's probably a ways off 😄 |
|
This needs some more work still. I noticed that there really need to be at least two protocols. One for the markup context and one for the code context (skipping math for now), given they both encode data differently. I might need something like |
|
@LostKobrakai let me know if you need help! |
Instead of using
String.Chars– which is already implemented for many primitive datatypes, but in ways not specific to typst – use a custom protocol to transform to related datastructures in typst.This currently goes
to_stringdirectly. Not sure if it would be useful to decouple eex and encoding likePhoenix.HTML.Safedoes, where eex returns "safe iodata" and there's another step to turn that into a string. Probably good to skip for now.What do you think?