Current section

Files

Jump to
nakai src nakai@codegen.erl
Raw

src/nakai@codegen.erl

-module(nakai@codegen).
-compile([no_auto_import, nowarn_unused_vars]).
-export([main/0]).
-export_type([element_description/0, attr_description/0]).
-type element_description() :: {element, binary(), boolean()}.
-type attr_description() :: {attr, binary()} | {const_attr, binary(), binary()}.
-spec element_decoder(gleam@dynamic:dynamic()) -> {ok, element_description()} |
{error, list(gleam@dynamic:decode_error())}.
element_decoder(Value) ->
(gleam@dynamic:decode2(
fun(Field@0, Field@1) -> {element, Field@0, Field@1} end,
gleam@dynamic:field(<<"name"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"leaf"/utf8>>, fun gleam@dynamic:bool/1)
))(Value).
-spec codegen_element(element_description()) -> binary().
codegen_element(Element) ->
case Element of
{element, Name, Leaf} when Leaf =:= false ->
<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
/// The HTML [`<"/utf8, Name/binary>>/binary,
">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/"/utf8>>/binary,
Name/binary>>/binary,
") element
pub fn "/utf8>>/binary,
Name/binary>>/binary,
"(attrs: List(Attr(a)), children: List(Node(a))) -> Node(a) {
Element(tag: \""/utf8>>/binary,
Name/binary>>/binary,
"\", attrs: attrs, children: children)
}
/// Shorthand for `html."/utf8>>/binary,
Name/binary>>/binary,
"(attrs, children: [html.Text(text)])`
pub fn "/utf8>>/binary,
Name/binary>>/binary,
"_text(attrs: List(Attr(a)), text: String) -> Node(a) {
Element(tag: \""/utf8>>/binary,
Name/binary>>/binary,
"\", attrs: attrs, children: [Text(text)])
}
"/utf8>>;
{element, Name@1, Leaf@1} when Leaf@1 =:= true ->
<<<<<<<<<<<<<<<<"
/// The HTML [`<"/utf8, Name@1/binary>>/binary,
" />`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/"/utf8>>/binary,
Name@1/binary>>/binary,
") element
pub fn "/utf8>>/binary,
Name@1/binary>>/binary,
"(attrs: List(Attr(a))) -> Node(a) {
LeafElement(tag: \""/utf8>>/binary,
Name@1/binary>>/binary,
"\", attrs: attrs)
}
"/utf8>>;
_ ->
erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"nakai/codegen"/utf8>>,
function => <<"codegen_element"/utf8>>,
line => 60})
end.
-spec generate_nakai_html() -> {ok, nil} | {error, nil}.
generate_nakai_html() ->
gleam@result:then(
begin
_pipe = gleam@erlang@file:read(<<"./codegen/html.json"/utf8>>),
gleam@result:nil_error(_pipe)
end,
fun(Html_json) ->
gleam@result:then(
begin
_pipe@1 = gleam@json:decode(
Html_json,
gleam@dynamic:list(fun element_decoder/1)
),
gleam@result:nil_error(_pipe@1)
end,
fun(Html) ->
gleam@result:then(
begin
_pipe@2 = gleam@erlang@file:read(
<<"./codegen/html_prelude.gleam"/utf8>>
),
gleam@result:nil_error(_pipe@2)
end,
fun(Html_prelude) ->
Code = begin
_pipe@3 = Html,
_pipe@4 = gleam@list:map(
_pipe@3,
fun codegen_element/1
),
gleam@string:concat(_pipe@4)
end,
gleam@result:then(
begin
_pipe@5 = gleam@erlang@file:write(
gleam@string:concat(
[<<"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// * THIS FILE IS GENERATED. DO NOT EDIT IT. *
// * You're probably looking for ./codegen/html_prelude.gleam, or ./codegen/html.json. *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
"/utf8>>,
Html_prelude,
Code]
),
<<"./src/nakai/html.gleam"/utf8>>
),
gleam@result:nil_error(_pipe@5)
end,
fun(_) -> {ok, nil} end
)
end
)
end
)
end
).
-spec attr_decoder(gleam@dynamic:dynamic()) -> {ok, attr_description()} |
{error, list(gleam@dynamic:decode_error())}.
attr_decoder(Value) ->
Attr_decoder = gleam@dynamic:decode1(
fun(Field@0) -> {attr, Field@0} end,
gleam@dynamic:field(<<"name"/utf8>>, fun gleam@dynamic:string/1)
),
Constattr_decoder = gleam@dynamic:decode2(
fun(Field@0, Field@1) -> {const_attr, Field@0, Field@1} end,
gleam@dynamic:field(<<"name"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"value"/utf8>>, fun gleam@dynamic:string/1)
),
_pipe = Constattr_decoder(Value),
gleam@result:lazy_or(_pipe, fun() -> Attr_decoder(Value) end).
-spec codegen_attr(attr_description()) -> binary().
codegen_attr(Attr) ->
Name@1 = case erlang:element(2, Attr) of
<<"type"/utf8>> ->
<<"type_"/utf8>>;
Name ->
_pipe = Name,
gleam@string:replace(_pipe, <<"-"/utf8>>, <<"_"/utf8>>)
end,
case Attr of
{attr, _} ->
<<<<<<<<"
pub fn "/utf8, Name@1/binary>>/binary,
"(value: String) -> Attr(a) {
Attr(name: \""/utf8>>/binary,
Name@1/binary>>/binary,
"\", value: value)
}
"/utf8>>;
{const_attr, _, Value} ->
<<<<<<<<<<<<"
pub fn "/utf8, Name@1/binary>>/binary,
"() -> Attr(a) {
Attr(name: \""/utf8>>/binary,
Name@1/binary>>/binary,
"\", value: \""/utf8>>/binary,
Value/binary>>/binary,
"\")
}
"/utf8>>
end.
-spec generate_nakai_html_attrs() -> {ok, nil} | {error, nil}.
generate_nakai_html_attrs() ->
gleam@result:then(
begin
_pipe = gleam@erlang@file:read(<<"./codegen/attrs.json"/utf8>>),
gleam@result:nil_error(_pipe)
end,
fun(Attrs_json) ->
gleam@result:then(
begin
_pipe@1 = gleam@json:decode(
Attrs_json,
gleam@dynamic:list(fun attr_decoder/1)
),
gleam@result:nil_error(_pipe@1)
end,
fun(Attrs) ->
gleam@result:then(
begin
_pipe@2 = gleam@erlang@file:read(
<<"./codegen/attrs_prelude.gleam"/utf8>>
),
gleam@result:nil_error(_pipe@2)
end,
fun(Attrs_prelude) ->
Code = begin
_pipe@3 = Attrs,
_pipe@4 = gleam@list:map(
_pipe@3,
fun codegen_attr/1
),
gleam@string:concat(_pipe@4)
end,
gleam@result:then(
begin
_pipe@5 = gleam@erlang@file:write(
gleam@string:concat(
[<<"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// * THIS FILE IS GENERATED. DO NOT EDIT IT. *
// * You're probably looking for ./codegen/attrs_prelude.gleam, or ./codegen/attrs.json. *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
"/utf8>>,
Attrs_prelude,
Code]
),
<<"./src/nakai/html/attrs.gleam"/utf8>>
),
gleam@result:nil_error(_pipe@5)
end,
fun(_) -> {ok, nil} end
)
end
)
end
)
end
).
-spec main() -> {ok, nil} | {error, nil}.
main() ->
gleam@result:then(
generate_nakai_html(),
fun(_) ->
gleam@result:then(
generate_nakai_html_attrs(),
fun(_) -> {ok, nil} end
)
end
).