A port of Clojure Hiccup for Hy
Project description
Hyccup
Hyccup is a port of Hiccup for Hy, a Lisp embed in Python.
It allows you to represent HTML into data structure and to dump it.
=> (import [hyccup.core [html]])
=> (html ['div {'class "my-class" 'id "my-id"} "Hello Hyccup"])
"<div class=\"my-class\" id=\"my-id\">Hello Hyccup</div>"
Differences with Hiccup
Keywords
As keywords are not a Python concept and as Hy is very close to Python, they cannot be used efficiently. Thus, we rely on strings or symbols instead.
That is to say,
[:div#an-id {:class "a-class"} "some text"]
must be changed to
["div#an-id" {"class" "a-class"} "some text"] ;; with strings
['div#an-id {'class "a-class"} "some text"] ;; with symbols
HTML Options
Instead of passing options in a dictionary as the first argument:
(html {:mode "xhtml" :espace-strings? true} [:p "example"])
Pass them as keyword arguments (or use unpacking):
(html ['p "example"] :mode "xhtml" :espace-strings True)
(html ['p "example"] #** {'mode "xhtml" 'espace-strings True})
(html ['p "example"] (unpack-mapping {'mode "xhtml" 'espace-strings True}))
Note that the escape flag argument has no ?
suffix in Hyccup.
Lists
The following form is valid in Hiccup:
(html (list [:p "some text"] [:p "another p"]))
In Hyccup, just chain the elements or use unpacking (as we already use lists to represent elements, where Hiccup use Clojure vectors).
(html ['p "some text"] ['p "another p"]))
(html #* [['p "some text"] ['p "another p"]]))
(html (unpack-iterable [['p "some text"] ['p "another p"]])))
with-*
macros
with-base-url
and with-encoding
are replaced by context managers.
Change
=> (with-base-url "/foo/"
(to-str (to-uri "/bar")))
"/foo/bar"
=> (with-encoding "UTF-8"
(url-encode {:iroha "いろは"}))
"iroha=%E3%81%84%E3%82%8D%E3%81%AF"
To
=> (with [(base-url "/foo/")]
(to-str (to-uri "/bar")))
"/foo/bar"
=> (with [(encoding "UTF-8")]
(url-encode {'iroha "いろは"}))
"iroha=%E3%81%84%E3%82%8D%E3%81%AF"
defhtml
and defelem
defhtml
and defelem
macros from Hiccup is available in two modules:
hyccup.defmacros
for usingdefhtml
anddefelem
as macros in Hy code:
=> (require [hyccup.defmacros [defhtml defelem]])
=> (defelem link-to [link text]
... ['a {'href link} text])
=> (link-to "https://www.pycolore.fr" "Pycolore" )
['a {'href "https://www.pycolore.fr"} "Pycolore"]
=> (defhtml linked-section-html [link text content]
... ['section
... ['h1 (link-to link text)]
... ['p content]])
=> (linked-section-html "https://www.pycolore.fr" "Pycolore" "Lorem Ipsum")
'<section>
<h1>
<a href="https://www.pycolore.fr">Pycolore</a>
</h1>
<p>
Lorem Ipsum
</p>
</section>'
hyccup.defdecos
for usingdefhtml
anddefelem
decorators in Python code. See Python Interop section.
Python interop
You can call Hyccup functions from Python code:
>>> import hy
>>> from hyccup.core import html
>>> html(["div", {"class": "my-class", "id": "my-id"}, "Hello Hyccup"])
'<div class="my-class" id="my-id">Hello Hyccup</div>'
defelem
and defhtml
macros are available as decorators in Python:
>>> from hyccup.defdecos import defelem, defhtml
>>> @defhtml # pass output of function to html()
... @defelem # merge last arg dict with attributes
... def link_to(link: str, text: str):
... return ["a", {"href": link}, text]
...
>>> link_to("https://www.pycolore.fr", "Pycolore", {"class": "some-class"})
'<a class="some-class" href="https://www.pycolore.fr">Pycolore</a>'
It is possible to pass HTML options to defhtml
decorator
>>> @defhtml(mode="html")
... def paragraph(content=""):
... return ["p", content]
...
>>> paragraph()
'<p></p>'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hyccup-1.0.0a1.tar.gz
.
File metadata
- Download URL: hyccup-1.0.0a1.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.1 Linux/5.13.2-188.current
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f93d70d16cc7ad92991c4d4badc76956c8ba966fdfaf3c1a9334ffce2ae7350 |
|
MD5 | 4c71ea1a0ace667b913312a85c79bd73 |
|
BLAKE2b-256 | cde1e78e7e4487073ee896e6eff1f40aeaf13ed44ca25bf707fdc98fec7aa0c9 |
File details
Details for the file hyccup-1.0.0a1-py3-none-any.whl
.
File metadata
- Download URL: hyccup-1.0.0a1-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.1 Linux/5.13.2-188.current
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a210e71c72540f4e99e4d258c51a5d4017aa45e636fc8cf7935be86259e2198 |
|
MD5 | a4bc68338014994e762fc3ca2eae3b9d |
|
BLAKE2b-256 | 795e731aba088c171e8f49e9254efa84302b5e239b7cde2dd9ba25834d0a7130 |