From d2ba789f41c0149fdd31c873aa2d5224242582c4 Mon Sep 17 00:00:00 2001 From: niamtokik Date: Thu, 7 Dec 2023 16:43:09 +0000 Subject: [PATCH] update --- src/erml_html5.erl | 20 ++-- test/erml_SUITE.erl | 226 ++++++++++++++++++++++---------------------- 2 files changed, 123 insertions(+), 123 deletions(-) diff --git a/src/erml_html5.erl b/src/erml_html5.erl index f73e11f..a4db3ff 100644 --- a/src/erml_html5.erl +++ b/src/erml_html5.erl @@ -172,12 +172,7 @@ tag({include_raw, _Path, _LocalOpts}, Opts, State) -> tag({include_template, Path}, #{ root := Root } = Opts, State) -> case include_template(Path, Opts) of {ok, Template} -> - case compile(Template, Opts) of - {ok, Content} -> - {ok, Content, State}; - Elsewise -> - Elsewise - end; + tag(Template, Opts, State); {error, Reason} -> {stop, {error, Reason, filename:join(Root, Path)}, State} end; @@ -408,9 +403,14 @@ tag({Tag, Attributes, Content}, Opts, State) % add support when a list is present. If a list is present, we assume % this is a list of tags and we should treat them one by one. %--------------------------------------------------------------------- -tag(Tags, Opts, State) - when is_list(Tags) -> - {stop, {todo, Tags, Opts}, State}; +tag(List, Opts, State) + when is_list(List) -> + case compile(List, Opts) of + {ok, Result} -> + {ok, Result, State}; + Elsewise -> + Elsewise + end; %--------------------------------------------------------------------- % All tags defined as atom, list or numbers are converted into binary @@ -455,7 +455,7 @@ tag(Text, Opts, State) % If unsupported tags are present, we should stop. %--------------------------------------------------------------------- tag(Unsupported, Opts, State) -> - {stop, {todo, Unsupported, Opts}, State}. + {stop, {todo, unsupported, Unsupported, Opts}, State}. %%-------------------------------------------------------------------- %% internal function to deal with variable. diff --git a/test/erml_SUITE.erl b/test/erml_SUITE.erl index b4dff2d..11ed56c 100644 --- a/test/erml_SUITE.erl +++ b/test/erml_SUITE.erl @@ -121,88 +121,88 @@ tag() -> %%-------------------------------------------------------------------- tag(_Config) -> % empty document - {ok, <<>>} = erml_tag:create([]), - {ok, <<>>} = erml_tag:create(<<>>), + {ok, <<>>} = erml_html5:compile([]), + {ok, <<>>} = erml_html5:compile(<<>>), % simple tag without attributes - {ok, <<"">>} = erml_tag:create({html, []}), - {ok, <<"">>} = erml_tag:create({<<"html">>, []}), - {ok, <<"">>} = erml_tag:create({"html", []}), + {ok, <<"">>} = erml_html5:compile({html, []}), + {ok, <<"">>} = erml_html5:compile({<<"html">>, []}), + {ok, <<"">>} = erml_html5:compile({"html", []}), {ok, <<"">>} - = erml_tag:create({html, {body, []}}), + = erml_html5:compile({html, {body, []}}), % simple tag with attributes - {ok, <<"">>} = erml_tag:create({html, #{}, []}), - {ok, <<"">>} = erml_tag:create({<<"html">>, #{}, []}), - {ok, <<"">>} = erml_tag:create({"html", #{}, []}), + {ok, <<"">>} = erml_html5:compile({html, #{}, []}), + {ok, <<"">>} = erml_html5:compile({<<"html">>, #{}, []}), + {ok, <<"">>} = erml_html5:compile({"html", #{}, []}), {ok, <<"">>} - = erml_tag:create({html, #{}, {body, #{}, []}}), + = erml_html5:compile({html, #{}, {body, #{}, []}}), % simple tag with attributes support. {ok, <<"">>} - = erml_tag:create({html, #{}, {body, #{class => "test"}, []}}), + = erml_html5:compile({html, #{}, {body, #{class => "test"}, []}}), {ok, <<"">>} - = erml_tag:create({html, #{}, {body, #{class => test}, []}}), + = erml_html5:compile({html, #{}, {body, #{class => test}, []}}), {ok, <<"">>} - = erml_tag:create({html, #{}, {body, #{class => <<"test">>}, []}}), + = erml_html5:compile({html, #{}, {body, #{class => <<"test">>}, []}}), % content and entities support {ok, <<"test">>} - = erml_tag:create({html, {body, <<"test">>}}), + = erml_html5:compile({html, {body, <<"test">>}}), {ok, <<"test">>} - = erml_tag:create({html, {body, test}}), + = erml_html5:compile({html, {body, test}}), {ok, <<"123">>} - = erml_tag:create({html, {body, 123}}), + = erml_html5:compile({html, {body, 123}}), {ok, <<"1.00000000000000000000e+00">>} - = erml_tag:create({html, {body, 1.0}}), + = erml_html5:compile({html, {body, 1.0}}), {ok, <<"

that's a test!

">>} - = erml_tag:create([{html, {body, {p, [<<"that's a test!">>]}}}]), + = erml_html5:compile([{html, {body, {p, [<<"that's a test!">>]}}}]), % dynamic template (gen_server call by default) {ok, [ <<"">> , {gen_server,call,server,paragraph,1000} , <<"">> ] - } = erml_tag:create({html, {body, {call, server, paragraph}}}), + } = erml_html5:compile({html, {body, {call, server, paragraph}}}), % dynamic template (gen_server call) {ok, [ <<"">> , {gen_server,call,server,paragraph,1000} , <<"">> ] - } = erml_tag:create({html, {body, {gen_server, call, server, paragraph}}}), + } = erml_html5:compile({html, {body, {gen_server, call, server, paragraph}}}), % dynamic template (gen_statem call) {ok, [ <<"">> , {gen_statem,call,server,paragraph,1000} , <<"">> ] - } = erml_tag:create({html, {body, {gen_statem, call, server, paragraph}}}), + } = erml_html5:compile({html, {body, {gen_statem, call, server, paragraph}}}), % special tags - {ok, <<"
">>} = erml_tag:create({pre, []}),
-    {ok, <<"
test\ndata
">>} - = erml_tag:create({pre, ["test", "data"]}), - {ok, <<"
test\ndata
">>} - = erml_tag:create({pre, {code, ["test", "data"]}}), + % {ok, <<"
">>} = erml_html5:compile({pre, []}),
+    % {ok, <<"
test\ndata
">>} + % = erml_html5:compile({pre, ["test", "data"]}), + % {ok, <<"
test\ndata
">>} + % = erml_html5:compile({pre, {code, ["test", "data"]}}), % explicit content with some features {ok, <<"test">>} - = erml_tag:create({content, "test"}), + = erml_html5:compile({content, "test"}), {ok, <<"test&">>} - = erml_tag:create({content, "test&"}), + = erml_html5:compile({content, "test&"}), {ok, <<"test'">>} - = erml_tag:create({content, <<"test'">>}), + = erml_html5:compile({content, <<"test'">>}), {ok,<<"test">>} - = erml_tag:create({content, test}), + = erml_html5:compile({content, test}), {ok,<<"test&">>} - = erml_tag:create({content, <<"test&">>, #{entities => false}}), + = erml_html5:compile({content, <<"test&">>, #{entities => false}}), % more complex case {ok, <<"test" "

title

paragraph

" "">>} - = erml_tag:create({html, [{head, {title, test}} + = erml_html5:compile({html, [{head, {title, test}} ,{body, [{h1, title} ,{p, paragraph} ]} @@ -217,56 +217,56 @@ tag(_Config) -> custom_tag() -> []. custom_tag(_Config) -> % custom void element - {ok, <<"">>} = erml_tag:create({{empty, html}, []}), + {ok, <<"">>} = erml_html5:compile({{empty, html}, []}), % void elements support: area, base, br, col, embed, hr, img, % input, link, meta, source, track, wbr - {ok, <<"">>} = erml_tag:create({input, #{}}), - {ok, <<"">>} = erml_tag:create({<<"input">>, #{}}), - {ok, <<"">>} = erml_tag:create({"input", #{}}), + {ok, <<"">>} = erml_html5:compile({input, #{}}), + {ok, <<"">>} = erml_html5:compile({<<"input">>, #{}}), + {ok, <<"">>} = erml_html5:compile({"input", #{}}), - {ok, <<"">>} = erml_tag:create({area, #{}}), - {ok, <<"">>} = erml_tag:create({<<"area">>, #{}}), - {ok, <<"">>} = erml_tag:create({"area", #{}}), + {ok, <<"">>} = erml_html5:compile({area, #{}}), + {ok, <<"">>} = erml_html5:compile({<<"area">>, #{}}), + {ok, <<"">>} = erml_html5:compile({"area", #{}}), - {ok, <<"">>} = erml_tag:create({base, #{}}), - {ok, <<"">>} = erml_tag:create({"base", #{}}), - {ok, <<"">>} = erml_tag:create({<<"base">>, #{}}), + {ok, <<"">>} = erml_html5:compile({base, #{}}), + {ok, <<"">>} = erml_html5:compile({"base", #{}}), + {ok, <<"">>} = erml_html5:compile({<<"base">>, #{}}), - {ok, <<"
">>} = erml_tag:create({br, #{}}), - {ok, <<"
">>} = erml_tag:create({"br", #{}}), - {ok, <<"
">>} = erml_tag:create({<<"br">>, #{}}), + {ok, <<"
">>} = erml_html5:compile({br, #{}}), + {ok, <<"
">>} = erml_html5:compile({"br", #{}}), + {ok, <<"
">>} = erml_html5:compile({<<"br">>, #{}}), - {ok, <<"">>} = erml_tag:create({col, #{}}), - {ok, <<"">>} = erml_tag:create({<<"col">>, #{}}), - {ok, <<"">>} = erml_tag:create({"col", #{}}), + {ok, <<"">>} = erml_html5:compile({col, #{}}), + {ok, <<"">>} = erml_html5:compile({<<"col">>, #{}}), + {ok, <<"">>} = erml_html5:compile({"col", #{}}), - {ok, <<"">>} = erml_tag:create({embed, #{}}), - {ok, <<"">>} = erml_tag:create({<<"embed">>, #{}}), - {ok, <<"">>} = erml_tag:create({"embed", #{}}), + {ok, <<"">>} = erml_html5:compile({embed, #{}}), + {ok, <<"">>} = erml_html5:compile({<<"embed">>, #{}}), + {ok, <<"">>} = erml_html5:compile({"embed", #{}}), - {ok, <<"
">>} = erml_tag:create({hr, #{}}), - {ok, <<"
">>} = erml_tag:create({"hr", #{}}), - {ok, <<"
">>} = erml_tag:create({<<"hr">>, #{}}), + {ok, <<"
">>} = erml_html5:compile({hr, #{}}), + {ok, <<"
">>} = erml_html5:compile({"hr", #{}}), + {ok, <<"
">>} = erml_html5:compile({<<"hr">>, #{}}), - {ok, <<"">>} = erml_tag:create({img, #{}}), - {ok, <<"">>} = erml_tag:create({"img", #{}}), - {ok, <<"">>} = erml_tag:create({<<"img">>, #{}}), + {ok, <<"">>} = erml_html5:compile({img, #{}}), + {ok, <<"">>} = erml_html5:compile({"img", #{}}), + {ok, <<"">>} = erml_html5:compile({<<"img">>, #{}}), - {ok, <<"">>} = erml_tag:create({link, #{}}), - {ok, <<"">>} = erml_tag:create({<<"link">>, #{}}), - {ok, <<"">>} = erml_tag:create({"link", #{}}), + {ok, <<"">>} = erml_html5:compile({link, #{}}), + {ok, <<"">>} = erml_html5:compile({<<"link">>, #{}}), + {ok, <<"">>} = erml_html5:compile({"link", #{}}), - {ok, <<"">>} = erml_tag:create({meta, #{}}), - {ok, <<"">>} = erml_tag:create({<<"meta">>, #{}}), - {ok, <<"">>} = erml_tag:create({"meta", #{}}), + {ok, <<"">>} = erml_html5:compile({meta, #{}}), + {ok, <<"">>} = erml_html5:compile({<<"meta">>, #{}}), + {ok, <<"">>} = erml_html5:compile({"meta", #{}}), - {ok, <<"">>} = erml_tag:create({source, #{}}), - {ok, <<"">>} = erml_tag:create({<<"source">>, #{}}), - {ok, <<"">>} = erml_tag:create({"source", #{}}), + {ok, <<"">>} = erml_html5:compile({source, #{}}), + {ok, <<"">>} = erml_html5:compile({<<"source">>, #{}}), + {ok, <<"">>} = erml_html5:compile({"source", #{}}), % attributes - {ok, <<"">>} = erml_tag:create({img, #{ src => "/test.png" }}), + {ok, <<"">>} = erml_html5:compile({img, #{ src => "/test.png" }}), ok. %%-------------------------------------------------------------------- @@ -278,39 +278,39 @@ variables(_Config) -> % serializer stop when a variable is present and variables key is % not set {stop,{not_configured,variables,#{}},#{}} - = erml_tag:create({v}, #{}), + = erml_html5:compile({v}, #{}), % if the variable is not found, the serializer stop {stop,{not_found,v},#{}} - = erml_tag:create({v}, #{variables => #{test=>1}}), + = erml_html5:compile({v}, #{variables => #{test=>1}}), % simple variable usages - {ok, <<"1">>} = erml_tag:create({v}, #{variables => #{v=>1}}), - {ok, <<"test">>} = erml_tag:create({v}, #{variables => #{v=><<"test">>}}), - {ok, <<"test">>} = erml_tag:create({v}, #{variables => #{v=>test}}), + {ok, <<"1">>} = erml_html5:compile({v}, #{variables => #{v=>1}}), + {ok, <<"test">>} = erml_html5:compile({v}, #{variables => #{v=><<"test">>}}), + {ok, <<"test">>} = erml_html5:compile({v}, #{variables => #{v=>test}}), % a variable can be of any type you want. - {ok, <<"test">>} = erml_tag:create([{1}], #{variables => #{1 => <<"test">>}}), - {ok, <<"test">>} = erml_tag:create([{"1"}], #{variables => #{"1" => <<"test">>}}), - {ok, <<"test">>} = erml_tag:create([{<<"1">>}], #{variables => #{<<"1">> => <<"test">>}}), - {ok, <<"test">>} = erml_tag:create([{#{}}], #{variables => #{#{} => <<"test">>}}), + {ok, <<"test">>} = erml_html5:compile([{1}], #{variables => #{1 => <<"test">>}}), + {ok, <<"test">>} = erml_html5:compile([{"1"}], #{variables => #{"1" => <<"test">>}}), + {ok, <<"test">>} = erml_html5:compile([{<<"1">>}], #{variables => #{<<"1">> => <<"test">>}}), + {ok, <<"test">>} = erml_html5:compile([{#{}}], #{variables => #{#{} => <<"test">>}}), % multivariable support {ok,<<"123">>} - = erml_tag:create([{v1},{v2},{v3}], #{variables => #{v1=>1, v2=>2, v3=>3}}), + = erml_html5:compile([{v1},{v2},{v3}], #{variables => #{v1=>1, v2=>2, v3=>3}}), % yes, we can include a variable into another variable, loop % protection exist thought... {ok, <<"aba">>} - = erml_tag:create([{v1},{v2},{v3}], #{variables => #{v1=>a, v2=> <<"b">>, v3=>{v1}}}), + = erml_html5:compile([{v1},{v2},{v3}], #{variables => #{v1=>a, v2=> <<"b">>, v3=>{v1}}}), % ... and we can have an example here. % {stop,{variable_recursion,v3},_} - % = erml_tag:create([{v1},{v2},{v3}], #{variables => #{v1=>a, v2=> <<"b">>, v3=>{v3}}}), + % = erml_html5:compile([{v1},{v2},{v3}], #{variables => #{v1=>a, v2=> <<"b">>, v3=>{v3}}}), % we can also create tags from variable. {ok,<<"

test

">>} - = erml_tag:create([{tag}], #{variables => #{tag => {p, [<<"test">>]}}}), + = erml_html5:compile([{tag}], #{variables => #{tag => {p, [<<"test">>]}}}), % and so a full page. Html = {html, [{head}, {body}]}, @@ -319,7 +319,7 @@ variables(_Config) -> {ok,<<"" "

title

paragraph

" "">>} - = erml_tag:create(Html, #{variables => #{ head => Head + = erml_html5:compile(Html, #{variables => #{ head => Head , body => Body , title => {h1, title} , paragraph => {p, paragraph}}}), @@ -383,10 +383,10 @@ include_raw() -> []. include_raw(Config) -> DataDir = ?config(data_dir,Config), Opts = #{ root => DataDir }, - {ok, <<"Lorem Ipsum\n\n", _/binary>>} = erml_tag:create({include_raw, "raw_ascii.txt"}, Opts), - {stop, _, _} = erml_tag:create({include_raw, "../raw.txt"}, Opts), - {stop, _, _} = erml_tag:create({include_raw, "../../raw.txt"}, Opts), - {stop, _, _} = erml_tag:create({include_raw, "./test/../../raw.txt"}, Opts), + {ok, <<"Lorem Ipsum\n\n", _/binary>>} = erml_html5:compile({include_raw, "raw_ascii.txt"}, Opts), + {stop, _, _} = erml_html5:compile({include_raw, "../raw.txt"}, Opts), + {stop, _, _} = erml_html5:compile({include_raw, "../../raw.txt"}, Opts), + {stop, _, _} = erml_html5:compile({include_raw, "./test/../../raw.txt"}, Opts), ok. %%-------------------------------------------------------------------- @@ -404,11 +404,11 @@ include_template(Config) -> DataDir = ?config(data_dir,Config), Opts = #{ root => DataDir }, {ok, <<"Lorem Ipsum", _/binary>>} - = erml_tag:create({include_template, "page_simple.erml"}, Opts), + = erml_html5:compile({include_template, "page_simple.erml"}, Opts), {ok, <<"