Kilobyte Wiki

Artifact Content
Login

Artifact d034f5993afdeee5cce63c81e2753d8a4cf452b5:


-module(html_gen).
-export([
         a/2,
         td/1, td/2,
         tr/1, tr/2,
         html/1, html/2,
         body/1, body/2,
         table/1, table/2,
         input/1,
         label/2,
         div_/1, div_/2,
         br/1, br/0,
         form/2,
         h2/1, h2/2,
         p/2,
         button/2,
         textarea/2
        ]).

atom_to_binary(Atom) -> list_to_binary(atom_to_list(Atom)).

td(Content) -> tag_gen("td", Content).
tr(Content) -> tag_gen("tr",  Content).
html(Content) -> tag_gen("html", Content).
table(Content) -> tag_gen("table", Content).
h2(Content) -> tag_gen("h2", Content).
div_(Content) -> tag_gen("div", Content).

html(Attribs, Content) -> tag_gen("html", Attribs, Content).
form(Attribs, Content) -> tag_gen("form", Attribs, Content).
label(Attribs, Content) -> tag_gen("label", Attribs, Content).
button(Attribs, Content) -> tag_gen("button", Attribs, Content).

%% Custom styling for body tags
css() ->
    AquaCSS = <<"<style>
body,input,textarea {
       font-family: Iosevka, monospace;
       background: #191e2a;
       color: #21EF9F;
}
a { color: aqua; }
a:visited { color: darkcyan; }
</style>">>,
    case kb_config:theme_name() of  
        "AUTUMN" -> 
<<"<style>
body,input,textarea {
       font-family: Iosevka, monospace;
       background: #2a2319;
       color: #EFC121;
}
a { color: #f0ff00; }
a:visited { color: a5622a; }
</style>">>;
        "AQUA" -> AquaCSS;
        _ -> AquaCSS % Default to Aqua if no theme is found
    end.
body(Content) -> tag_gen("body", [css(), Content]).
body(Attribs, Content) -> tag_gen("body", Attribs, [css(), Content]).

div_(Attribs, Content) -> tag_gen("div", Attribs, Content).
h2(Attribs, Content) -> tag_gen("h2", Attribs, Content).
a(Attribs, Content) -> tag_gen("a", Attribs, Content).
p(Attribs, Content) -> tag_gen("p", Attribs, Content).

table(Attribs, Content) -> tag_gen("table", Attribs, Content).
td(Attribs, Content) -> tag_gen("td", Attribs, Content).
tr(Attribs, Content) -> tag_gen("tr", Attribs, Content).

input(Attribs) -> single_tag_gen("input", Attribs).
textarea(Attribs, Content) -> tag_gen("textarea", Attribs, Content).

br() -> <<"<br/>">>.
br(Attribs) -> single_tag_gen("br", Attribs).


single_tag_gen(TagName, Attribs) -> 
    [<<"<">>, TagName, <<" ">>, 
     [[ atom_to_binary(Attrib), <<"=\"">>, Value, <<"\" ">> ] || {Attrib, Value} <- Attribs], 
     <<"/>">>].

tag_gen(TagName, Content) ->
    [<<"<">>, TagName, <<" >">>, 
     Content,
     <<"</">>, TagName, <<">">>].
    
tag_gen(TagName, Attribs, Content) when is_map(Attribs) -> tag_gen(TagName, maps:to_list(Attribs), Content);
tag_gen(TagName, Attribs, Content) ->
    [<<"<">>, TagName, <<" ">>, 
     [ [ atom_to_binary(Attrib), 
         <<"=\"">>, Value, <<"\" ">>
       ] || {Attrib, Value} <- Attribs], 
     <<">">>,
     Content,
     <<"</">>, TagName, <<">">>].