HypertextLiteral.jl

Julia library for the string interpolation of HTML and SVG
Author MechanicalRabbit
Popularity
19 Stars
Updated Last
1 Year Ago
Started In
October 2020

HypertextLiteral.jl

HypertextLiteral is a Julia package for generating HTML, SVG, and other SGML tagged content. It works similar to Julia string interpolation, only that it tracks hypertext escaping needs and provides handy conversions dependent upon context.

Stable Docs Dev Docs Release Build Nightly Build Code Coverage Zulip Chat ISC License

This project is inspired by Hypertext Literal by Mike Bostock (@mbostock) available at here. This work is based upon a port to Julia written by Michiel Dral with significant architectural feedback by Kirill Simonov (@xitology).

This package provides the macro @htl which returns an object that can be rendered to MIME"text/html" displays. This macro provides contextual interpolation sensible to the needs of HTML construction.

    using HypertextLiteral

    books = [
     (name="Who Gets What & Why", year=2012, authors=["Alvin Roth"]),
     (name="Switch", year=2010, authors=["Chip Heath", "Dan Heath"]),
     (name="Governing The Commons", year=1990, authors=["Elinor Ostrom"])]

    render_row(book) = @htl("""
      <tr><td>$(book.name) ($(book.year))<td>$(join(book.authors, " & "))
    """)

    render_table(books) = @htl("""
      <table><caption><h3>Selected Books</h3></caption>
      <thead><tr><th>Book<th>Authors<tbody>
      $((render_row(b) for b in books))</tbody></table>""")

    display("text/html", render_table(books))
    #=>
    <table><caption><h3>Selected Books</h3></caption>
    <thead><tr><th>Book<th>Authors<tbody>
      <tr><td>Who Gets What &amp; Why (2012)<td>Alvin Roth
      <tr><td>Switch (2010)<td>Chip Heath &amp; Dan Heath
      <tr><td>Governing The Commons (1990)<td>Elinor Ostrom
    </tbody></table>
    =#

This library implements many features for working with HTML data within the Julia language, including:

  • Performant escaping of interpolated values
  • Handles boolean valued attributes, such as disabled, checked
  • Serialization of Pair and Tuple objects as attribute pairs
  • Conversion of snake_case => camel-case for attribute names
  • Support for CSS style formatting via Pair, Tuple and Dict
  • Translation of Julia values to Javascript within script tag
  • Direct inclusion of objects (like HTML) showable by MIME"text/html"
  • Extension API for customizing object display in various contexts

When AbstractString, Integer or AbsractFloat values are interpolated as content, their escaped values are printed to the output. By default, other values are printed with a <span> wrapper, with its class reflecting the module and type names.

    display("text/html", @htl("<div>$missing</div>")
    #-> <div><span class="Base-Missing">missing</span></div>

By default, this will show up as missing in the browser display. However, a style can be used to change this display.

<style>
span.Base-Missing {visibility: collapse;}
span.Base-Missing::before {content: "N/A"; visibility: visible;}
</style>

For more detail, please see the documentation and join us on Julia's Zulip.

Required Packages

No packages found.

Used By Packages