|
| 1 | +# niccup |
| 2 | + |
| 3 | +Hiccup-style HTML generation for Nix. Describe HTML as Nix lists/attrsets, render to strings. Heavily inspired by [hiccup](https://github.com/weavejester/hiccup) (Fast library for rendering HTML in Clojure) by [@weavejester](https://github.com/weavejester) |
| 4 | + |
| 5 | +[View on GitHub](https://github.com/embedding-shapes/niccup) |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Flakes: |
| 10 | +```nix |
| 11 | +inputs.niccup.url = "github:embedding-shapes/niccup"; |
| 12 | +# use inputs.niccup.lib |
| 13 | +``` |
| 14 | + |
| 15 | +Non-flakes: |
| 16 | +```nix |
| 17 | +let |
| 18 | + niccup = builtins.fetchTarball "https://github.com/embedding-shapes/niccup/archive/master.tar.gz"; |
| 19 | + h = import (niccup + "/src/lib.nix") { }; |
| 20 | +in h.render [ "p" "Hello" ] |
| 21 | +``` |
| 22 | + |
| 23 | +## Security Note |
| 24 | + |
| 25 | +This library assumes trusted inputs. Like server-side templates, the expressions should be developer-authored code, not user-generated content. Do not pass untrusted input directly to `raw` or `comment`, or do so at your own risk. Don't say you weren't warned tho. |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +```nix |
| 30 | +let h = inputs.niccup.lib; in |
| 31 | +h.render [ |
| 32 | + "div#main.container" |
| 33 | + { lang = "en"; class = [ "app" "dark" ]; } |
| 34 | + [ "h1" "Hello from Nix" ] |
| 35 | + [ "p" "Hiccup-style HTML in Nix." ] |
| 36 | + (h.comment "List example") |
| 37 | + [ "ul" (map (x: [ "li.item" x ]) [ "one" "two" "three" ]) ] |
| 38 | +] |
| 39 | +``` |
| 40 | + |
| 41 | +Write to file (use nixpkgs `writeText`): |
| 42 | +```nix |
| 43 | +{ pkgs, inputs, ... }: |
| 44 | +pkgs.writeText "index.html" (inputs.niccup.lib.render [ "p" "Hello" ]) |
| 45 | +``` |
| 46 | + |
| 47 | +## Data Model |
| 48 | + |
| 49 | +**Element**: `[ tag-spec attrs? children... ]` |
| 50 | + |
| 51 | +**Tag spec**: string with optional CSS shorthand: `"div#id.class1.class2"`. ID must precede classes. |
| 52 | + |
| 53 | +**Attributes** (optional attrset, second position): |
| 54 | +- Merged with shorthand; classes combine (shorthand first), ID from map overwrites shorthand. |
| 55 | +- `class`: string or list of strings. |
| 56 | +- Boolean `true` renders as `attr="attr"`; `false`/`null` omits the attribute. |
| 57 | + |
| 58 | +**Children**: |
| 59 | +- Strings/numbers: escaped, numbers via `toString`. |
| 60 | +- Elements: nested `[ tag ... ]` lists. |
| 61 | +- Lists: flattened one level (enables `map` patterns). |
| 62 | +- `raw` nodes: unescaped HTML. |
| 63 | +- `comment` nodes: `<!-- ... -->`. |
| 64 | + |
| 65 | +**Void tags** (`img`, `br`, `hr`, `input`, `meta`, `link`, `area`, `base`, `col`, `embed`, `source`, `track`, `wbr`): no closing tag. |
| 66 | + |
| 67 | +## API |
| 68 | + |
| 69 | +- `render : expr -> string` — Render to minified HTML string. |
| 70 | +- `renderPretty : expr -> string` — Render to indented, human-readable HTML. |
| 71 | +- `raw : string -> node` — Mark content as unescaped HTML. |
| 72 | +- `comment : string -> node` — Emit HTML comment. |
| 73 | + |
| 74 | +Exported as `lib` from the flake. |
| 75 | + |
| 76 | +## Development |
| 77 | + |
| 78 | +``` |
| 79 | +just build # checks syntax, builds the library |
| 80 | +
|
| 81 | +just test # runs a bunch of tests |
| 82 | +
|
| 83 | +just build-website # builds the project website to result/ |
| 84 | +
|
| 85 | +just all-examples # builds all of the examples, currently `blog`, `quine` and `art` |
| 86 | +
|
| 87 | +just example blog # builds only the `blog` example |
| 88 | +``` |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +MIT 2025 - [@embedding-shapes](https://github.com/embedding-shapes) |
0 commit comments