How to write HTML from julia?

We have great packages for embedding julia code into markdown documents, and render HTML using Weave.jl or Literate.jl.
However, sometimes I’ll like to generate an HTML-file from inside a julia-script rather than using one the templating packages. Something similar to what the hwriter R-package does.

The following works, but it is somewhat low-level:

julia> open("/tmp/index.html", "w") do io
           show(io, "text/html", html"""<!DOCTYPE html><HTML lang = "en"><HEAD><meta charset="UTF-8"/></HEAD><BODY>""")
           show(io, "text/html", html"<h1>My title</h1>")
           show(io, "text/html", html"Some text.")
           show(io, "text/html", html"</BODY></HTML>")
       end

Is there already a package to help make this more ergonomic?

2 Likes

Maybe GitHub - JuliaPluto/HypertextLiteral.jl: Julia library for the string interpolation of HTML and SVG?

3 Likes

Thanks for the link to HypertextLiteral @dpsanders .
I did not know of that, and it is definitely relevant for this usecase.

It appears to be mainly focused on improving on the @html_str string macro from base Julia.

I’m looking for something to reduce the amount of “boiler-plate” I need to type to generate an HTML-document, and as far as I can see, that is not the goal of HypertextLiteral.

GitHub - JunoLab/Hiccup.jl: HTML DSL AFAICT and GitHub - JuliaWeb/Hyperscript.jl: Hyperscript: A lightweight DOM representation for Julia come to mind for this if you’re trying to reduce boilerplate. Both work pretty well from what I recall.

2 Likes

Thanks @mike. Both Hiccup and Hyperscript look like they can help me write HTML in a simple way.
The READMEs don’t show how to generate a full HTML document and write it to a file, so I’ll take a look at the source, and see if I can generate a simple example.

1 Like

Thanks for the solution @mike (even 2 solutions :smiley: ).

Here’s a way to do it in Hyperscript.
That is a clear improvement over the naive @html_str version:

julia> using Hyperscript

julia> cont =
    [
        m("h1", "My title"),
        "Some text",
    ] ;

julia> doc = 
    [
        m("head",
          m("meta", charset="UTF-8"),
          ),
        m("body",
          cont,
          )
    ]
2-element Vector{Hyperscript.Node{Hyperscript.HTMLSVG}}:
 <head><meta charset="UTF-8" /></head>
 <body><h1>My title</h1>Some text</body>

julia> savehtml("/tmp/hyper.html", doc... ) ;

shell> cat /tmp/hyper.html
<!doctype html>
<html><head><meta charset="UTF-8" /></head><body><h1>My title</h1>Some text</body></html>
3 Likes

Hiccup is also nice.
It does not have a dedicated save method, as far as I can see.

Here’s one way:

julia> using Hiccup

julia> cont =
    [
        h1("My title"),
        "Some text"
    ] ;

julia> doc = html(
    head(
        meta(charset="UTF-8")
    ),
    body(
        cont
    )
)
<html><head><meta charset="UTF-8" /></head><body><h1>My title</h1>Some text</body></html>

julia> open("/tmp/hiccup.html", "w") do io
    show(io, "text/html", html"<!DOCTYPE html>")
    show(io, "text/html", doc)
end

shell> cat /tmp/hiccup.html
<!DOCTYPE html><html><head><meta charset="UTF-8" /></head><body><h1>My title</h1>Some text</body></html>

2 Likes

Thanks for sharing! Have you considered submitting those two examples to the respective READMEs? It would be a great help to others with the same question. :slight_smile:

1 Like

Good idea @waldyrious .

I’ll look at this over the weeked.

I actually filed an issue while preparing the first example :smile:

1 Like

Done for Hyperscript: Pull request #38. I’ll probably leave it with that.

1 Like