Skip to content

JuliaPluto/HypertextLiteral.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 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(list) = @htl("""
      <table><caption><h3>Selected Books</h3></caption>
      <thead><tr><th>Book<th>Authors<tbody>
      $((render_row(b) for b in list))</tbody></table>""")

    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 and JavaScript 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 => kebab-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

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