#!/usr/bin/env julia using Faker, HypertextLiteral, Hyperscript, BenchmarkTools using HypertextLiteral: @htl_str # This is going to simulate a hierarchical report that lists a set of # companies, and for each company, a list of employees. Faker.seed(4321) make_employee() = ( first_name=Faker.first_name(), last_name=Faker.last_name(), title=Faker.job(), main_number=Faker.phone_number(), email=Faker.email(), cell_phone=Faker.cell_phone(), color= Faker.hex_color(), comments= Faker.paragraphs() ) make_customer() = ( company=Faker.company(), url=Faker.url(), phrase=Faker.catch_phrase(), active=Faker.date_time_this_decade(before_now=true, after_now=false), notes= Faker.sentence(number_words=rand(2:9), variable_nb_words=true), employees=[make_employee() for x in 1:rand(3:18)]) database = [make_customer() for x in 1:13] htl_database(d) = @htl(" Customers & Employees) $((map(d) do c; htl_customer(c); end)) ") htl_customer(c) = @htl("
Company
$(c.company)
Phrase
$(c.phrase)
Active Since
$(c.active)
Employees
$((map(c.employees) do e; htl_employee(e); end))
Last NameFirst NameTitle E-MailOffice PhoneCell Phone Comments
") htl_employee(e) = @htl(" $(e.last_name)$(e.first_name)$(e.title) $(e.email) $(e.main_number)$(e.cell_phone) $((@htl("$c") for c in e.comments)) ") htl_test() = begin io = IOBuffer() ob = htl_database(database) show(io, MIME("text/html"), ob) return io end # very silly test using attributes rather than elements... att_database(d) = @htl(""" $(map(d) do c; att_customer(c); end) """) att_customer(c) = @htl("""
$((map(c.employees) do e; att_employee(e); end))
""") att_employee(e) = @htl("""
e.email)> e.main_number))> $((@htl("") for x in e.comments)) """) att_test() = begin io = IOBuffer() ob = att_database(database) show(io, MIME("text/html"), ob) return io end ee(x) = replace(replace(x, "&" => "&"), "<" => "<") ea(x) = replace(replace(x, "&" => "&"), "'" => "'") reg_database(d) = """ Customers & Employees $(join([reg_customer(c) for c in d])) """ reg_customer(c) = """
Company
$(ee(c.company))
Phrase
$(ee(c.phrase))
Active Since
$(ee(c.active))
Employees
$(join([reg_employee(e) for e in c.employees]))
Last NameFirst NameTitle E-MailOffice PhoneCell Phone Comments
""" reg_employee(e) = """ $(ee(e.last_name))$(ee(e.first_name))$(e.title) $(ee(e.email)) $(ee(e.main_number))$(ee(e.cell_phone)) $(join(["$(ee(c))" for c in e.comments])) """ reg_test() = begin io = IOBuffer() ob = reg_database(database) show(io, ob) return io end @tags html head body title dl dt dd table tr th td span hs_database(d) = html(head(title("Customers & Employees")), body([hs_customer(c) for c in d]...)) hs_customer(c)= dl(dt("Company"), dd(c.company), dt("Phrase"), dd(c.phrase), dt("Active Since"), dd(c.active), dt("Employees"), dd( table(tr(th("Last Name"),th("First Name"),th("Title"), th("E-Mail"),th("Office Phone"),th("Cell Phone"), th("Comments")), [hs_employee(e) for e in c.employees]...))) hs_employee(e) = tr(td(e.last_name), td(e.first_name), td(e.title), td(href="mailto:$(e.email)", e.email), td(e.main_number), td(e.cell_phone), td([span(c) for c in e.comments]...)) hs_test() = begin io = IOBuffer() ob = hs_database(database) show(io, MIME("text/html"), ob) return io end function H(xs...) HTML() do io for x in xs show(io, MIME"text/html"(), x) end end end function entity(str::AbstractString) @assert length(str) == 1 entity(str[1]) end entity(ch::Char) = "&#$(Int(ch));" HE(x) = HTML(replace(x, r"[<&]" => entity)) HA(x) = HTML(replace(x, r"[<']" => entity)) #HE(x) = HTML(replace(replace(x, "&" => "&"), "<" => "<")) #HA(x) = HTML(replace(replace(x, "&" => "&"), "\"" => """)) cus_database(d) = H(HTML(""), HE("Customers & Employees"), HTML(""), [cus_customer(c) for c in d]..., HTML("")) cus_customer(c) = H(HTML("
Company
"), HE(c.company), HTML("
Phrase
"), HE(c.phrase), HTML("
Active Siince
"), HE(c.active), HTML("""
Employees
"""), [cus_employee(e) for e in c.employees]..., HTML("
Last NameFirst NameTitle E-MailOffice PhoneCell Phone Comments
")) cus_employee(e) = H(HTML(""), HE(e.last_name), HTML(""), HE(e.first_name), HTML(""), HE(e.title), HTML(""), HE(e.email), HTML(""), HTML(""), HE(e.main_number), HTML(""), HE(e.cell_phone), HTML(""), [H(HTML(""), HE(c), HTML("")) for c in e.comments]...) cus_test() = begin io = IOBuffer() ob = cus_database(database) show(io, MIME("text/html"), ob) return io end nest_result(d) = @htl(" Customers & Employees) $((map(d) do c; @htl("
Company
$(c.company)
Phrase
$(c.phrase)
Active Since
$(c.active)
Employees
$((map(c.employees) do e; @htl("
Last NameFirst NameTitle E-MailOffice PhoneCell Phone Comments
$(e.last_name)$(e.first_name)$(e.title) $(e.email) $(e.main_number)$(e.cell_phone) $((@htl("$c") for c in e.comments)) "); end))
"); end)) ") nest_test() = begin io = IOBuffer() ob = nest_result(database) show(io, MIME("text/html"), ob) return io end BenchmarkTools.DEFAULT_PARAMETERS.seconds = 20 @info("interpolate: ", @benchmark reg_test()) @info("Custom HTML: ", @benchmark cus_test()) @info("Hyperscript: ", @benchmark hs_test()) @info("HypertextLiteral: ", @benchmark htl_test()) @info("HTL (Attributes): ", @benchmark att_test()) @info("Nest Testing: ", @benchmark nest_test()) if false open("htl.html", "w") do f ob = htl_database(database) show(f, MIME("text/html"), ob) end open("hs.html", "w") do f ob = hs_database(database) show(f, MIME("text/html"), ob) end open("reg.html", "w") do f ob = reg_database(database) show(f, ob) end open("cus.html", "w") do f ob = cus_database(database) show(f, MIME("text/html"), ob) end end