jack
jack copied to clipboard
jack is a HTML generator library for Emacs Lisp.
~jack~ is a HTML generator library for Emacs Lisp.
To install it put ~jack.el~ file in your load path and require ~jack~ like this:
#+BEGIN_SRC emacs-lisp (require 'jack) #+END_SRC
~jack~ provides the function ~jack-html~ that takes a data structure as input representing the HTML tree you want to generate and generates it as a string.
For instance:
#+BEGIN_SRC emacs-lisp
(jack-html '(:section (:div (:p "foo"))))
;; " foo
HTML attributes are specified in a list starting by the ~@~ sign
#+BEGIN_SRC emacs-lisp (jack-html '(:div (@ :id "id" :class "class" :style "color:red;") "foo")) ;; "<div id="id" class="class" style="color:red;">foo" #+END_SRC
In the keyword defining the HTML tag you can use ~/~ to declare its ~id~ and ~.~ to declare its classes like this:
#+BEGIN_SRC emacs-lisp (jack-html '(:div/id.class-1.class-2 (@ :class "class-3" :style "color:red;") "foo")) ;; "<div id="id" class="class-1 class-2 class-3" style="color:red;">foo" #+END_SRC
Note that I would have prefered to use ~#~ for declaring the ~id~ but it has to be escaped in keywords which is ugly.
Tag content can be lists of components:
#+BEGIN_SRC emacs-lisp (jack-html '(:ul ((:li "1") (:li "2")))) ;; "
- 1
- 2
(jack-html '(:ul (@ :id "id") ((:li "1") (:li "2")))) ;; "<ul id="id">
Components can be generated by a forms:
#+BEGIN_SRC emacs-lisp (jack-html `(:p ,(concat "foo-" "bar"))) ;; "
foo-bar
"(jack-html (mapcar (lambda (n) `(:p ,n)) '(1 2 3))) ;; "
1
2
3
" #+END_SRCTag content can be forms:
#+BEGIN_SRC emacs-lisp
(jack-html (:ul ,(mapcar (lambda (n)
(:li ,n)) '(1 2))))
;; "
- 1
- 2
(jack-html (:ul (@ :id "id") ,(mapcar (lambda (n)
(:li ,n)) '(1 2))))
;; "<ul id="id">
Tag content and attributes can be variables:
#+BEGIN_SRC emacs-lisp (let ((x "foo") (y "bar")) (jack-html `(:p (@ :id ,x) ,y))) ;; "<p id="foo">bar
"(jack-html (let ((x "foo") (y "bar")) `(:p (@ :id ,x) ,y))) ;; "<p id="foo">bar
" #+END_SRCIf the variable ~jack-html-raise-error-p~ is set to ~nil~, which is the default value, ~jack-html~ processes non component object as the empty string.
Let's consider the case of the vector like ~[a b c]~ that is not a component for ~jack-html~.
We have
#+BEGIN_SRC emacs-lisp (let ((jack-html-raise-error-p nil)) (jack-html "foo" [a b c] "bar")) ;; "foobar" #+END_SRC
and,
#+BEGIN_SRC emacs-lisp (let ((jack-html-raise-error-p t)) (jack-html "foo" [a b c] "bar")) #+END_SRC
which raises the error:
#+BEGIN_SRC text Object '[a b c]' of type 'vector' can't be a component in 'jack-html' #+END_SRC