Current section
Files
Jump to
Current section
Files
src/glamour@server.erl
-module(glamour@server).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glamour/server.gleam").
-export([default_options/0, render/3]).
-export_type([options/0, rendered/0]).
-type options() :: {options,
binary(),
gleam@option:option(binary()),
gleam@option:option(binary()),
list(binary()),
list(binary()),
boolean()}.
-type rendered() :: {rendered, binary()}.
-file("src/glamour/server.gleam", 24).
-spec default_options() -> options().
default_options() ->
{options,
<<"en"/utf8>>,
none,
none,
[<<" <meta charset=\"utf-8\">\n"/utf8>>,
<<" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"/utf8>>],
[<<"/assets/app.js"/utf8>>],
false}.
-file("src/glamour/server.gleam", 117).
-spec trim_hash(binary()) -> binary().
trim_hash(Selector) ->
case gleam_stdlib:string_starts_with(Selector, <<"#"/utf8>>) of
true ->
case string:length(Selector) of
0 ->
Selector;
1 ->
<<""/utf8>>;
Len ->
gleam@string:slice(Selector, 1, Len - 1)
end;
false ->
Selector
end.
-file("src/glamour/server.gleam", 129).
-spec escape_html(binary()) -> binary().
escape_html(Input) ->
_pipe = Input,
_pipe@1 = gleam@string:replace(_pipe, <<"&"/utf8>>, <<"&"/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<"<"/utf8>>, <<"<"/utf8>>),
_pipe@3 = gleam@string:replace(_pipe@2, <<">"/utf8>>, <<">"/utf8>>),
_pipe@4 = gleam@string:replace(_pipe@3, <<"\""/utf8>>, <<"""/utf8>>),
gleam@string:replace(_pipe@4, <<"'"/utf8>>, <<"'"/utf8>>).
-file("src/glamour/server.gleam", 138).
-spec escape_json(binary()) -> binary().
escape_json(Json_text) ->
_pipe = Json_text,
_pipe@1 = gleam@string:replace(_pipe, <<"</"/utf8>>, <<"<\\/>"/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<"<"/utf8>>, <<"\\u003C"/utf8>>),
_pipe@3 = gleam@string:replace(_pipe@2, <<">"/utf8>>, <<"\\u003E"/utf8>>),
_pipe@4 = gleam@string:replace(_pipe@3, <<"&"/utf8>>, <<"\\u0026"/utf8>>),
_pipe@5 = gleam@string:replace(
_pipe@4,
<<"\\u2028"/utf8>>,
<<"\\\\u2028"/utf8>>
),
gleam@string:replace(_pipe@5, <<"\\u2029"/utf8>>, <<"\\\\u2029"/utf8>>).
-file("src/glamour/server.gleam", 38).
-spec render(glamour@app:spec(OFR, any()), OFR, gleam@option:option(options())) -> rendered().
render(Spec, Model, Options) ->
Defaults = default_options(),
Final_options = case Options of
{some, Value} ->
Value;
none ->
Defaults
end,
{options, Lang, Title, Csp_nonce, Head, Client_scripts, _} = Final_options,
Html_fragment = lustre@element:to_string((erlang:element(3, Spec))(Model)),
State_json = (erlang:element(4, Spec))(Model),
State_text = begin
_pipe = gleam@json:to_string(State_json),
escape_json(_pipe)
end,
Fingerprint = glamour@fingerprint:compute(State_text, Html_fragment),
Title_tag = case Title of
{some, Value@1} ->
<<<<" <title>"/utf8, (escape_html(Value@1))/binary>>/binary,
"</title>\n"/utf8>>;
none ->
<<""/utf8>>
end,
Nonce_attr = case Csp_nonce of
{some, Nonce} ->
<<<<" nonce=\""/utf8, Nonce/binary>>/binary, "\""/utf8>>;
none ->
<<""/utf8>>
end,
Script_tags = begin
_pipe@1 = Client_scripts,
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Src) ->
<<<<<<<<" <script src=\""/utf8, Src/binary>>/binary,
"\""/utf8>>/binary,
Nonce_attr/binary>>/binary,
"></script>\n"/utf8>>
end
),
erlang:list_to_binary(_pipe@2)
end,
Head_block = <<<<<<<<" <head>\n"/utf8, Title_tag/binary>>/binary,
(erlang:list_to_binary(Head))/binary>>/binary,
Script_tags/binary>>/binary,
" </head>\n"/utf8>>,
Body_block = <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" <body>\n"/utf8,
" <div id=\""/utf8>>/binary,
(trim_hash(
erlang:element(
6,
Spec
)
))/binary>>/binary,
"\" data-glamour-fp=\""/utf8>>/binary,
Fingerprint/binary>>/binary,
"\">"/utf8>>/binary,
Html_fragment/binary>>/binary,
"</div>\n"/utf8>>/binary,
" <script type=\"application/json\" id=\""/utf8>>/binary,
(erlang:element(7, Spec))/binary>>/binary,
"\""/utf8>>/binary,
Nonce_attr/binary>>/binary,
">"/utf8>>/binary,
State_text/binary>>/binary,
"</script>\n"/utf8>>/binary,
" </body>\n"/utf8>>,
Document = <<<<<<<<<<<<"<!doctype html>\n"/utf8, "<html lang=\""/utf8>>/binary,
Lang/binary>>/binary,
"\">\n"/utf8>>/binary,
Head_block/binary>>/binary,
Body_block/binary>>/binary,
"</html>"/utf8>>,
{rendered, Document}.