Skip to content

Commit 9e8d950

Browse files
Init
0 parents  commit 9e8d950

File tree

17 files changed

+1048
-0
lines changed

17 files changed

+1048
-0
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master, main]
6+
pull_request:
7+
8+
jobs:
9+
check:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
13+
- uses: DeterminateSystems/determinate-nix-action@b3e3f405539b332fcb96794525f35fb10c230baa # v3.13.2
14+
- run: nix flake check --all-systems

.github/workflows/pages.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Deploy to GitHub Pages
2+
3+
on:
4+
push:
5+
branches: [master, main]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
concurrency:
14+
group: pages
15+
cancel-in-progress: false
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
22+
- uses: DeterminateSystems/determinate-nix-action@b3e3f405539b332fcb96794525f35fb10c230baa # v3.13.2
23+
- name: Build website
24+
run: nix build .#website
25+
- name: Upload artifact
26+
uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3.0.1
27+
with:
28+
path: result
29+
30+
deploy:
31+
environment:
32+
name: github-pages
33+
url: ${{ steps.deployment.outputs.page_url }}
34+
runs-on: ubuntu-latest
35+
needs: build
36+
steps:
37+
- name: Deploy to GitHub Pages
38+
id: deployment
39+
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Nix build outputs
2+
**/result
3+
**/result-*
4+
5+
# Direnv
6+
/.direnv
7+
.envrc
8+
9+
# Editor swap/backup
10+
*~
11+
.*.swp
12+

Justfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
default: build
2+
3+
lock:
4+
nix flake lock
5+
6+
build:
7+
nix build .#default
8+
9+
build-website:
10+
nix build .#website
11+
12+
test:
13+
nix flake check --all-systems
14+
15+
all-examples:
16+
just example blog
17+
just example quine
18+
just example art
19+
20+
example name:
21+
cd examples/{{name}} && nix build

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2025 - @embedding-shapes
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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)

examples/art/flake.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/art/flake.nix

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
description = "Generative SVG art example using niccup";
3+
4+
inputs = {
5+
niccup.url = "path:../..";
6+
nixpkgs.follows = "niccup/nixpkgs";
7+
};
8+
9+
outputs = { self, nixpkgs, niccup }:
10+
let
11+
systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
12+
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
13+
in {
14+
packages = forAllSystems (system:
15+
let
16+
pkgs = import nixpkgs { inherit system; };
17+
h = niccup.lib;
18+
19+
# Sierpinski triangle: recursively subdivide into 3 smaller triangles
20+
sierpinski = depth: x: y: size:
21+
if depth == 0 then
22+
let
23+
# Equilateral triangle pointing up
24+
x1 = x;
25+
y1 = y;
26+
x2 = x + size;
27+
y2 = y;
28+
x3 = x + size / 2;
29+
y3 = y - size * 0.866; # sqrt(3)/2 ≈ 0.866
30+
in
31+
[ "polygon" { points = "${toString x1},${toString y1} ${toString x2},${toString y2} ${toString x3},${toString y3}"; } ]
32+
else
33+
let
34+
half = size / 2;
35+
height = size * 0.866;
36+
in [
37+
(sierpinski (depth - 1) x y half)
38+
(sierpinski (depth - 1) (x + half) y half)
39+
(sierpinski (depth - 1) (x + half / 2) (y - height / 2) half)
40+
];
41+
42+
page = h.renderPretty [
43+
"html" { lang = "en"; }
44+
[ "head"
45+
[ "meta" { charset = "utf-8"; } ]
46+
[ "title" "Sierpinski Triangle" ]
47+
[ "style" (h.raw ''
48+
body { margin: 0; display: flex; justify-content: center; align-items: center; min-height: 100vh; background: #1a1a2e; }
49+
svg { max-width: 90vmin; max-height: 90vmin; }
50+
polygon { fill: #e94560; }
51+
'') ]
52+
]
53+
[ "body"
54+
[ "svg" { viewBox = "0 0 100 90"; xmlns = "http://www.w3.org/2000/svg"; }
55+
(sierpinski 6 0.0 86.0 100.0)
56+
]
57+
]
58+
];
59+
60+
indexHtml = pkgs.writeText "index.html" page;
61+
62+
in {
63+
default = pkgs.runCommand "art" {} ''
64+
mkdir -p $out
65+
cp ${indexHtml} $out/index.html
66+
'';
67+
});
68+
};
69+
}

examples/blog/flake.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)