hsx/README.md

107 lines
1.9 KiB
Markdown
Raw Normal View History

2024-05-27 02:39:52 +00:00
# HSX (WIP)
2018-06-24 17:08:30 +00:00
2024-05-25 12:48:35 +00:00
HSX (hypertext s-expression) is an incredibly simple HTML5 generation library for Common Lisp.
2024-05-27 02:39:52 +00:00
This is a fork project of [flute](https://github.com/ailisp/flute/), originally created by Bo Yao.
2024-02-03 09:56:09 +00:00
2024-05-27 09:08:25 +00:00
# Usage
2024-05-27 09:24:19 +00:00
Using the `hsx` macro, you can implement HTML with S-expression.
2024-05-27 09:08:25 +00:00
2024-05-27 09:24:19 +00:00
```lisp
2024-05-27 09:08:25 +00:00
(hsx
(div :id "greeting" :class "flex"
(h1 "Hello World")
(p
"This is"
(strong "example!"))))
↓ ↓ ↓
<div id="greeting" class="flex">
<h1>Hello World</h1>
<p>
This is
<strong>example!</strong>
</p>
</div>
```
2024-05-27 09:44:07 +00:00
HSX elements are essentially functions, so you can freely compose them and embed CL code to them.
2024-05-27 09:08:25 +00:00
2024-05-27 09:24:19 +00:00
```lisp
2024-05-27 09:08:25 +00:00
(hsx
(div
(p :id (+ 1 1))
(ul
(loop
:for i :from 1 :to 3
:collect (li (format nil "item~a" i))))
(if t
(p "true")
(p "false"))))
↓ ↓ ↓
<div>
<p id="2"></p>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
<p>true</p>
</div>
```
2024-05-27 09:24:19 +00:00
To define a component, just define a function that accepts keyword arguments or property list or both, and define HSX element with `defhsx` macro.
2024-05-27 09:08:25 +00:00
2024-05-27 09:40:50 +00:00
`children` is a special property that accepts children of a component.
2024-05-27 09:24:19 +00:00
```lisp
2024-05-27 09:08:25 +00:00
(defhsx card #'%card)
2024-05-27 09:40:50 +00:00
(defun %card (&key title children)
2024-05-27 09:08:25 +00:00
(hsx
(div
(h1 title)
2024-05-27 09:40:50 +00:00
children)))
2024-05-27 09:08:25 +00:00
2024-05-27 09:24:19 +00:00
or
(defhsx card #'%card)
(defun %card (&rest props)
(hsx
(div
(h1 (getf props :title))
2024-05-27 09:40:50 +00:00
(getf props :children))))
2024-05-27 09:24:19 +00:00
2024-05-27 09:40:50 +00:00
(hsx
(card :title "card1"
(p "brah brah brah...")))
2024-05-27 09:08:25 +00:00
↓ ↓ ↓
<div>
<h1>card1</h1>
<p>brah brah brah...</p>
</div>
```
The previous definition can be simplified by using the `defcomp` macro.
2024-05-27 09:24:19 +00:00
```lisp
2024-05-27 09:40:50 +00:00
(defcomp card (&key title children)
2024-05-27 09:08:25 +00:00
(hsx
(div
(h1 title)
2024-05-27 09:40:50 +00:00
children)))
2024-05-27 09:08:25 +00:00
```
# License
2024-05-27 02:47:10 +00:00
This project is licensed under the terms of the MIT license.
2024-02-08 17:54:12 +00:00
Copyright (c) 2024, skyizwhite.
2024-05-20 05:38:28 +00:00
Copyright (c) 2018, Bo Yao.