Skip to main content

A port of Clojure Hiccup for Hy

Project description

Hyccup

Tests

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

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"

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>'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hyccup-1.0.0a1.dev3.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

hyccup-1.0.0a1.dev3-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file hyccup-1.0.0a1.dev3.tar.gz.

File metadata

  • Download URL: hyccup-1.0.0a1.dev3.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.1 Linux/5.12.13-185.current

File hashes

Hashes for hyccup-1.0.0a1.dev3.tar.gz
Algorithm Hash digest
SHA256 e587e5d5d00f03836c4463994cf3287804551976b5f1512f308e579ab86ade65
MD5 33ea17acdea238d3e21dfcf4ae1bcf83
BLAKE2b-256 9c0d6f3551a9909395aecfc2c65e2a87ea0e3426a7e031569e0fb42a549835e5

See more details on using hashes here.

File details

Details for the file hyccup-1.0.0a1.dev3-py3-none-any.whl.

File metadata

  • Download URL: hyccup-1.0.0a1.dev3-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.1 Linux/5.12.13-185.current

File hashes

Hashes for hyccup-1.0.0a1.dev3-py3-none-any.whl
Algorithm Hash digest
SHA256 c2af333cc1513eded065c6dab01ece5c097efcf2d1ba4d8db74fd58a5f783db7
MD5 4aa86b1ffb923f977123e47b7b60b8b5
BLAKE2b-256 f1a283a8c11ec53aad7fe9a195adcc5d1a21427ec8c7d5e578cc13520874e5a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page