The print-html Reference Manual

Table of Contents

Next: , Previous: , Up: (dir)   [Contents][Index]

The print-html Reference Manual

This is the print-html Reference Manual, version 1.0.0, generated automatically by Declt version 2.4 patchlevel 1 "Will Decker" on Fri May 24 09:35:46 2019 GMT+0.


Next: , Previous: , Up: Top   [Contents][Index]

1 Introduction

* Overview

This program is an Html generator for Common Lisp.

Why another one?

I want programmatically generate and process Html.
In particular I want to

- Store Html in variables

- Use Html as a function argument

- Use Html as a function return value

To achieve this, we transform symbolic expressions into lists of tag
structures.  The method print-html then prints its input as properly
escaped Html.


* Package

We only use common-lisp.

#+begin_src lisp
(defpackage print-html
  (:use :common-lisp)
  (:export :render :print-html :print-html-to-string :html :unsafe))

(in-package :print-html)
#+end_src

* Self closing tags

#+begin_src lisp
(defvar *self-closing-tags*
  (list :area :base :br :col :command :embed :frame :hr :img :input :keygen
	:link :menuitem :meta :param :source :track :wbr)
  "List of self closing tags.")
#+end_src

* Render

The /RENDER/ method is called by /PRINT-HTML/ to map an
object into something that /PRINT-HTML/ is specialized for.
By default, /RENDER/ calls /princ-to-string/.

#+begin_src lisp
(defgeneric render (object)
  (:method (object)
    (princ-to-string object)))
#+end_src

* Print Html

The interpreter. Calls /render/ for objects it is not specialized
for.

#+begin_src lisp
(defgeneric print-html (object stream)
  (:method (object stream)
    (print-html (render object) stream))
  (:method ((char character) stream)
    (case char
      (#\< (write-string "<" stream))
      (#\> (write-string ">" stream))
      (#\& (write-string "&" stream))
      (#\" (write-string """ stream))
      (t (write-char char stream))))
  (:method ((string string) stream)
    (map nil (lambda (char) (print-html char stream)) string))
  (:method ((list list) stream)
    (dolist (object list)
      (print-html object stream))))
#+end_src

Print /OBJECT/ to string.  Note: This function is also used to
escape attributes.

#+begin_src lisp
(defun print-html-to-string (object)
  (with-output-to-string (stream)
    (print-html object stream)))
#+end_src

* Tag

#+begin_src lisp
(defstruct tag name attrs children)

(defmethod print-html ((self tag) stream)
  (print-html (tag-children self) stream))

(defmethod print-html :before ((self tag) stream)
  (format stream "~&<~(~a~)~{ ~(~a~)=~s~}>" (tag-name self)
	  (loop for (k v) on (tag-attrs self) by #'cddr when v
	     collect (print-html-to-string k) and
	     collect (print-html-to-string (if (eq v t) k v)))))

(defmethod print-html :after ((self tag) stream)
  (unless (member (tag-name self) *self-closing-tags*)
    (format stream "~&" (tag-name self))))
#+end_src

* Html DSL 

Macroexpand example:

The code

#+begin_example
 (print-html-to-string
   (html
     ((:span :style "color:blue") "text")))
#+end_example

expands to

#+begin_example
(PRINT-HTML-TO-STRING (LIST (MAKE-TAG :NAME
                                      :SPAN
                                      :ATTRS
                                      (LIST :STYLE "color:blue")
                                      :CHILDREN
                                      (HTML "text"))))
#+end_example

and evaluates to

#+begin_example
"text"
#+end_example

The html generation macro: 

#+begin_src lisp
(defmacro html (&body body)
  (labels ((listify (x) (if (listp x) x (list x)))
           (codegen (x)
             (cond ((atom x) x)
                   ((not (keywordp (car (listify (car x))))) x)
                   (t (destructuring-bind (head &rest body) x
                        (destructuring-bind (name &rest attrs) (listify head)
                          `(make-tag :name ,name :attrs (list ,@attrs)
                                     :children (html ,@body))))))))
    `(list ,@(mapcar #'codegen body))))
#+end_src

* Extending the Print-Html package

** Doctype

Print doctype.

#+begin_src lisp
(defmethod print-html ((self (eql :doctype-html)) stream)
  (format stream "~&"))
#+end_src

** Unsafe

Print string without escaping

#+begin_src lisp
(defstruct (unsafe (:constructor unsafe (string))) string)

(defmethod print-html ((unsafe unsafe) stream)
  (write-string (unsafe-string unsafe) stream))
#+end_src


Next: , Previous: , Up: Top   [Contents][Index]

2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 print-html

Author

Olaf Ritter von Ruppert <oruppert@googlemail.com>

License

MIT License

Description

Simple html generator.

Version

1.0.0

Source

print-html.asd (file)

Component

print-html.lisp (file)


Next: , Previous: , Up: Top   [Contents][Index]

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.1 print-html.asd

Location

print-html.asd

Systems

print-html (system)


Previous: , Up: Lisp files   [Contents][Index]

3.1.2 print-html/print-html.lisp

Parent

print-html (system)

Location

print-html.lisp

Packages

print-html

Exported Definitions
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

4 Packages

Packages are listed by definition order.


Previous: , Up: Packages   [Contents][Index]

4.1 print-html

Source

print-html.lisp (file)

Use List

common-lisp

Exported Definitions
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Exported definitions


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.1 Macros

Macro: html &body BODY
Package

print-html

Source

print-html.lisp (file)


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.2 Functions

Function: print-html-to-string OBJECT
Package

print-html

Source

print-html.lisp (file)

Function: unsafe STRING
Package

print-html

Source

print-html.lisp (file)


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.3 Generic functions

Generic Function: print-html OBJECT STREAM
Package

print-html

Source

print-html.lisp (file)

Methods
Method: print-html (UNSAFE unsafe) STREAM
Method: print-html (SELF (eql doctype-html)) STREAM
Method: print-html (SELF tag) STREAM after
Method: print-html (SELF tag) STREAM before
Method: print-html (SELF tag) STREAM
Method: print-html OBJECT STREAM
Method: print-html (CHAR character) STREAM
Method: print-html (STRING string) STREAM
Method: print-html (LIST list) STREAM
Generic Function: render OBJECT
Package

print-html

Source

print-html.lisp (file)

Methods
Method: render OBJECT

Previous: , Up: Exported definitions   [Contents][Index]

5.1.4 Structures

Structure: unsafe ()
Package

print-html

Source

print-html.lisp (file)

Direct superclasses

structure-object (structure)

Direct methods

print-html (method)

Direct slots
Slot: string
Readers

unsafe-string (function)

Writers

(setf unsafe-string) (function)


Previous: , Up: Definitions   [Contents][Index]

5.2 Internal definitions


Next: , Previous: , Up: Internal definitions   [Contents][Index]

5.2.1 Special variables

Special Variable: *self-closing-tags*

List of self closing tags.

Package

print-html

Source

print-html.lisp (file)


Next: , Previous: , Up: Internal definitions   [Contents][Index]

5.2.2 Functions

Function: copy-tag INSTANCE
Package

print-html

Source

print-html.lisp (file)

Function: copy-unsafe INSTANCE
Package

print-html

Source

print-html.lisp (file)

Function: make-tag &key (NAME NAME) (ATTRS ATTRS) (CHILDREN CHILDREN)
Package

print-html

Source

print-html.lisp (file)

Function: tag-attrs INSTANCE
Function: (setf tag-attrs) VALUE INSTANCE
Package

print-html

Source

print-html.lisp (file)

Function: tag-children INSTANCE
Function: (setf tag-children) VALUE INSTANCE
Package

print-html

Source

print-html.lisp (file)

Function: tag-name INSTANCE
Function: (setf tag-name) VALUE INSTANCE
Package

print-html

Source

print-html.lisp (file)

Function: tag-p OBJECT
Package

print-html

Source

print-html.lisp (file)

Function: unsafe-p OBJECT
Package

print-html

Source

print-html.lisp (file)

Function: unsafe-string INSTANCE
Function: (setf unsafe-string) VALUE INSTANCE
Package

print-html

Source

print-html.lisp (file)


Previous: , Up: Internal definitions   [Contents][Index]

5.2.3 Structures

Structure: tag ()
Package

print-html

Source

print-html.lisp (file)

Direct superclasses

structure-object (structure)

Direct methods
Direct slots
Slot: name
Readers

tag-name (function)

Writers

(setf tag-name) (function)

Slot: attrs
Readers

tag-attrs (function)

Writers

(setf tag-attrs) (function)

Slot: children
Readers

tag-children (function)

Writers

(setf tag-children) (function)


Previous: , Up: Top   [Contents][Index]

Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts

Jump to:   F   L   P  
Index Entry  Section

F
File, Lisp, print-html.asd: The print-html<dot>asd file
File, Lisp, print-html/print-html.lisp: The print-html/print-html<dot>lisp file

L
Lisp File, print-html.asd: The print-html<dot>asd file
Lisp File, print-html/print-html.lisp: The print-html/print-html<dot>lisp file

P
print-html.asd: The print-html<dot>asd file
print-html/print-html.lisp: The print-html/print-html<dot>lisp file

Jump to:   F   L   P  

Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   (  
C   F   G   H   M   P   R   T   U  
Index Entry  Section

(
(setf tag-attrs): Internal functions
(setf tag-children): Internal functions
(setf tag-name): Internal functions
(setf unsafe-string): Internal functions

C
copy-tag: Internal functions
copy-unsafe: Internal functions

F
Function, (setf tag-attrs): Internal functions
Function, (setf tag-children): Internal functions
Function, (setf tag-name): Internal functions
Function, (setf unsafe-string): Internal functions
Function, copy-tag: Internal functions
Function, copy-unsafe: Internal functions
Function, make-tag: Internal functions
Function, print-html-to-string: Exported functions
Function, tag-attrs: Internal functions
Function, tag-children: Internal functions
Function, tag-name: Internal functions
Function, tag-p: Internal functions
Function, unsafe: Exported functions
Function, unsafe-p: Internal functions
Function, unsafe-string: Internal functions

G
Generic Function, print-html: Exported generic functions
Generic Function, render: Exported generic functions

H
html: Exported macros

M
Macro, html: Exported macros
make-tag: Internal functions
Method, print-html: Exported generic functions
Method, print-html: Exported generic functions
Method, print-html: Exported generic functions
Method, print-html: Exported generic functions
Method, print-html: Exported generic functions
Method, print-html: Exported generic functions
Method, print-html: Exported generic functions
Method, print-html: Exported generic functions
Method, print-html: Exported generic functions
Method, render: Exported generic functions

P
print-html: Exported generic functions
print-html: Exported generic functions
print-html: Exported generic functions
print-html: Exported generic functions
print-html: Exported generic functions
print-html: Exported generic functions
print-html: Exported generic functions
print-html: Exported generic functions
print-html: Exported generic functions
print-html: Exported generic functions
print-html-to-string: Exported functions

R
render: Exported generic functions
render: Exported generic functions

T
tag-attrs: Internal functions
tag-children: Internal functions
tag-name: Internal functions
tag-p: Internal functions

U
unsafe: Exported functions
unsafe-p: Internal functions
unsafe-string: Internal functions

Jump to:   (  
C   F   G   H   M   P   R   T   U  

Next: , Previous: , Up: Indexes   [Contents][Index]

A.3 Variables

Jump to:   *  
A   C   N   S  
Index Entry  Section

*
*self-closing-tags*: Internal special variables

A
attrs: Internal structures

C
children: Internal structures

N
name: Internal structures

S
Slot, attrs: Internal structures
Slot, children: Internal structures
Slot, name: Internal structures
Slot, string: Exported structures
Special Variable, *self-closing-tags*: Internal special variables
string: Exported structures

Jump to:   *  
A   C   N   S  

Previous: , Up: Indexes   [Contents][Index]

A.4 Data types

Jump to:   P   S   T   U  
Index Entry  Section

P
Package, print-html: The print-html package
print-html: The print-html system
print-html: The print-html package

S
Structure, tag: Internal structures
Structure, unsafe: Exported structures
System, print-html: The print-html system

T
tag: Internal structures

U
unsafe: Exported structures

Jump to:   P   S   T   U