Skip to content

A small Janet library to create HTML strings from Janet data structures.

License

Notifications You must be signed in to change notification settings

m7andrew/encode-html

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Encode HTML

A small Janet library to create HTML strings from Janet data structures. Similar to Clojure's Hiccup.

Install

jpm install https://github.com/m7andrew/encode-html

Examples

Simple HTML structure:

(import html)

(html/encode 
  [:div {:class "foo"}
    [:a {:href "/some/link"} "Link"]
    [:span {} "Hello"]])
<div class="foo">
  <a href="/some/link">Link</a>
  <span>Hello</span>
</div>

Loop using html/each:

(import html)

(def todos 
  ["Write some janet code"
   "Make a sandwich"
   "Find the lost city of Atlantis"])

(html/encode 
  [:ul {}
    (html/each item todos
      [:li {} item])])

Loop using map:

(import html)

(def todos 
  ["Write some janet code"
   "Make a sandwich"
   "Find the lost city of Atlantis"])

(defn todo [item]
  [:li {} item])

(html/encode 
  [:ul {} ;(map todo todos)])

Result using either method:

<ul>
  <li>Write some janet code</li>
  <li>Make a sandwich</li>
  <li>Find the lost city of Atlantis</li>
</ul>

Functions

Encode

(html/encode element)

Takes an element and returns an HTML string.

Each

(html/each x ds & body)

Like Janet's each. Returns the body for each x in the data structure ds.

Syntax

Each element has the general form:

[tag attributes & children]
  • tag is a keyword for the tag name.
  • attributes is a struct with keyword keys and string values.
  • children is a sequence of elements, strings, or other types of data.

Any child that is not another element (tuple) gets converted to a string. So you can do things like insert numbers directly:

[:span {} 42]
<span>42</span>

About

A small Janet library to create HTML strings from Janet data structures.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published