-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from isaacvando/template-draft
template draft
- Loading branch information
Showing
9 changed files
with
874 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
interface CodeGen | ||
exposes [generate] | ||
imports [Parser.{ Node }] | ||
|
||
generate : List { name : Str, nodes : List Node } -> Str | ||
generate = \templates -> | ||
functions = | ||
List.map templates renderTemplate | ||
|> Str.joinWith "\n\n" | ||
names = | ||
List.map templates .name | ||
|> Str.joinWith ",\n" | ||
|> indent | ||
|> indent | ||
|
||
""" | ||
interface Pages | ||
exposes [ | ||
$(names) | ||
] | ||
imports [] | ||
$(functions) | ||
escapeHtml : Str -> Str | ||
escapeHtml = \\input -> | ||
input | ||
|> Str.replaceEach "&" "&" | ||
|> Str.replaceEach "<" "<" | ||
|> Str.replaceEach ">" ">" | ||
|> Str.replaceEach "\\"" """ | ||
|> Str.replaceEach "'" "'" | ||
|
||
""" | ||
# \"" | ||
|
||
RenderNode : [ | ||
Text Str, | ||
Conditional { condition : Str, trueBranch : List RenderNode, falseBranch : List RenderNode }, | ||
Sequence { item : Str, list : Str, body : List RenderNode }, | ||
] | ||
|
||
renderTemplate : { name : Str, nodes : List Node } -> Str | ||
renderTemplate = \{ name, nodes } -> | ||
body = | ||
condense nodes | ||
|> renderNodes | ||
|
||
""" | ||
$(name) = \\model -> | ||
$(body) | ||
""" | ||
|
||
renderNodes : List RenderNode -> Str | ||
renderNodes = \nodes -> | ||
when List.map nodes toStr is | ||
[] -> "\"\"" |> indent | ||
[elem] -> elem | ||
blocks -> | ||
list = blocks |> Str.joinWith ",\n" | ||
""" | ||
[ | ||
$(list) | ||
] | ||
|> Str.joinWith "" | ||
""" | ||
|> indent | ||
|
||
toStr = \node -> | ||
block = | ||
when node is | ||
Text t -> | ||
""" | ||
\""" | ||
$(t) | ||
\""" | ||
""" | ||
|
||
Conditional { condition, trueBranch, falseBranch } -> | ||
""" | ||
if $(condition) then | ||
$(renderNodes trueBranch) | ||
else | ||
$(renderNodes falseBranch) | ||
""" | ||
|
||
Sequence { item, list, body } -> | ||
""" | ||
List.map $(list) \\$(item) -> | ||
$(renderNodes body) | ||
|> Str.joinWith "" | ||
""" | ||
indent block | ||
|
||
condense : List Node -> List RenderNode | ||
condense = \nodes -> | ||
List.map nodes \node -> | ||
when node is | ||
RawInterpolation i -> Text "\$($(i))" | ||
Interpolation i -> Text "\$($(i) |> escapeHtml)" | ||
Text t -> | ||
# Escape Roc string interpolations from the template | ||
escaped = Str.replaceEach t "$" "\\$" | ||
Text escaped | ||
|
||
Sequence { item, list, body } -> Sequence { item, list, body: condense body } | ||
Conditional { condition, trueBranch, falseBranch } -> | ||
Conditional { | ||
condition, | ||
trueBranch: condense trueBranch, | ||
falseBranch: condense falseBranch, | ||
} | ||
|> List.walk [] \state, elem -> | ||
when (state, elem) is | ||
([.. as rest, Text x], Text y) -> | ||
combined = Str.concat x y |> Text | ||
rest |> List.append combined | ||
|
||
_ -> List.append state elem | ||
|
||
indent : Str -> Str | ||
indent = \input -> | ||
Str.split input "\n" | ||
|> List.map \str -> | ||
Str.concat " " str | ||
|> Str.joinWith "\n" |
Oops, something went wrong.