├── .gitattributes
├── LICENSE
├── README.md
├── attr-processors.lisp
├── clip-logo.png
├── clip.asd
├── clipboard.lisp
├── conditions.lisp
├── docs
├── clip-logo.png
└── index.html
├── example.ctml
├── package.lisp
├── processor.lisp
├── tag-processors.lisp
├── test.ctml
└── toolkit.lisp
/.gitattributes:
--------------------------------------------------------------------------------
1 | doc/ linguist-vendored
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2014 Yukari Hafner
2 |
3 | This software is provided 'as-is', without any express or implied
4 | warranty. In no event will the authors be held liable for any damages
5 | arising from the use of this software.
6 |
7 | Permission is granted to anyone to use this software for any purpose,
8 | including commercial applications, and to alter it and redistribute it
9 | freely, subject to the following restrictions:
10 |
11 | 1. The origin of this software must not be misrepresented; you must not
12 | claim that you wrote the original software. If you use this software
13 | in a product, an acknowledgment in the product documentation would be
14 | appreciated but is not required.
15 | 2. Altered source versions must be plainly marked as such, and must not be
16 | misrepresented as being the original software.
17 | 3. This notice may not be removed or altered from any source distribution.
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## About Clip
2 | Clip is an attempt at a templating library that allows you to write templates in a way that is both accessible to direct webdesign and flexible. The main idea is to incorporate transformation commands into an HTML file through tags and attributes. Clip is heavily dependant on [Plump](https://shinmera.github.io/plump) and [lQuery](https://shinmera.github.io/lquery).
3 |
4 | ## How To
5 | Load Clip through ASDF or Quicklisp.
6 |
7 | ```
8 | (ql:quickload :clip)
9 | ```
10 |
11 | To process a template, simply call `PROCESS`:
12 |
13 | ```
14 | (clip:process #p"my-template.ctml")
15 | ```
16 |
17 | You may also pass in pure strings or plump nodes. Most likely you will want to include some kind of data in your template. Data in Clip is managed through a central `CLIPBOARD`. The additional arguments you pass to `PROCESS` are entered into the initial clipboard like a plist (key and value alternating).
18 |
19 | Depending on the current tag environment and how the template is processed at the time these values can come into play. Most of the time, entering the name as passed into `PROCESS` in the template as an attribute value will then evaluate to the according value using `RESOLVE-VALUE`. In the case of a symbol this then delegates to `CLIP`, which returns the value stored in the clipboard.
20 |
21 | The value returned by `PROCESS` is the node you passed into it. You can parse this back into a string using `PLUMP:SERIALIZE` or lQuery's `WRITE-TO-FILE`.
22 |
23 | ## Standard Tags
24 |
25 | * `C:EXPAND`
26 | This tag expands its attributes and then calls `PROCESS-NODE` on itself again. This is useful to generate attributes to be expanded.
27 | * `C:IF`
28 | Looks for either a `TEST` attribute or a `C:TEST` tag as one of its direct children. If the test as by `RESOLVE-VALUE` is non-NIL, all children of the `C:THEN` tag are spliced in place of the `C:IF` block. Otherwise it looks for `C:ELSEIF` blocks and checks their `TEST` attributes in turn. The contents of the first block whose `TEST` passes is spliced. If none pass, the `C:ELSE` child block, if any, is spliced.
29 | * `C:ITERATE`
30 | Looks for one attribute called `OVER` and then works like the `ITERATE` attribute processor using the value of the `OVER` attribute.
31 | * `C:LET`
32 | Creates a new clipboard environment with all the tag attributes bound in the following manner: The attribute key is put into the clipboard directly and associated with the value of `RESOLVE-VALUE` of the attribute value. Acts like `SPLICE`.
33 | * `C:NOOP`
34 | This tag only processes its attributes, but none of its children.
35 | * `C:SPLICE`
36 | Splices all nodes within it into the parent's child list at the position of itself (essentially replacing it with its children).
37 | * `C:UNLESS`
38 | Same as WHEN, but inverted.
39 | * `C:USING`
40 | Binds the clipboard to the resolved-value of its `VALUE` attribute. Acts like `SPLICE`.
41 | * `C:WHEN`
42 | Looks for a `TEST` attribute and if the value of it as by `RESOLVE-VALUE` is non-NIL, acts like `SPLICE`. Otherwise it removes itself including its children from the DOM.
43 | * `C:CASE`
44 | Takes a `VALUE` attribute that each element in the body is compared against, just like CL's `case`. Each element in the body may have a `VALUE` attribute to compare again, or a `VALUES` attribute that is a space-separated list, too.
45 | * `C:S`
46 | A shorthand for `
48 | A shorthand for `
55 | Simply changes that node's tag-name to the value of this attribute.
56 | * `COUNT`
57 | Inserts the value of `*TARGET-COUNT*` as the attribute value. This is useful to follow processing order during debugging.
58 | * `EVAL`
59 | Simply calls `EVAL` on the value of `READ-FROM-STRING` of the attribute value.
60 | * `ITERATE`
61 | The value (as by `RESOLVE-VALUE`) is used as an iteration list or vector. The first node within the node this attribute belongs to is copied once for each item in the iteration list and processed with that item used as the clipboard.
62 | * `LQUERY`
63 | Calls lQuery functions on the node as if by `($ node ..)`. Note that only lQuery functions are available, not its macros.
64 | * `FILL`
65 | The attribute value is read as a plist, the keys of which designate other attribute names and the values are resolved to the objects to use. For each named attribute, its value is modified by replacing `{thing}` by the result of `clip` on the respective object's field `thing`.
66 |
67 | ## Extending Clip
68 | You can define new tag and attribute processors with the macros `DEFINE-TAG-PROCESSOR` and `DEFINE-ATTRIBUTE-PROCESSOR`. For tag processors you will usually want to make sure to call `PROCESS-ATTRIBUTES` and `PROCESS-CHILDREN` to ensure that tags and attributes within are properly processed. To retrieve values most of the time you need to use `RESOLVE-VALUE` (or its shorthand `RESOLVE-ATTRIBUTE`) unless you want to whip up your own system for one reason or another. All tags that you define will automatically be prefixed with `C:` in order to help highlighting template tags and ensure that there are no collisions with existing tags.
69 |
70 | ## Editor Support
71 | The Emacs extension [Web-Mode](http://web-mode.org/)(version 9.0.77+) provides syntax highlighting for Clip templates. In order for it to recognise the templates, use the `.ctml` file extension. A huge thanks goes to [Bois Francois-Xavier](https://github.com/fxbois) for adding the support.
72 |
73 | ## Tutorials
74 | These are short tutorials to help explaining the effects of each tag and to illustrate some basic templating techniques. The examples will only work with Clip>=0.5.1 and lQuery>=3.1.1 .
75 |
76 | ### Updating a node with values
77 |
78 | (clip:process-to-string
79 | ""
80 | :text "Hi!" :class "clip-text")
81 |
82 | Explanation: The `LQUERY` attribute allows you to perform lQuery operations on the node it is an attribute of. In this case, the `TEXT` function sets the text of the node to the value of `TEXT`, which we told Clip to be `"Hi!"`. Similarly for `ADD-CLASS`. Any non-keyword symbol within the template is automatically resolved to a field on the current clipboard. You may think of the clipboard as a form of lexical environment for the template, which we currently set to have the variables `TEXT` and `CLASS` bound. The default `CLIPBOARD` object is special in the sense that it does not differentiate between accessing it with keywords, symbols or strings and is case-insensitive. This makes it easier to access in templates.
83 |
84 | Please see the [lQuery](https://shinmera.github.io/lquery) documentation for all possible node manipulation functions.
85 |
86 | ### Populating from a list
87 |
88 | (clip:process-to-string
89 | "
An HTML templating engine using Plump.
Clip is an attempt at a templating library that allows you to write templates in a way that is both accessible to direct webdesign and flexible. The main idea is to incorporate transformation commands into an HTML file through tags and attributes. Clip is heavily dependant on Plump and lQuery.
Load Clip through ASDF or Quicklisp.
(ql:quickload :clip)
To process a template, simply call PROCESS
:
(clip:process #p"my-template.ctml")
You may also pass in pure strings or plump nodes. Most likely you will want to include some kind of data in your template. Data in Clip is managed through a central CLIPBOARD
. The additional arguments you pass to PROCESS
are entered into the initial clipboard like a plist (key and value alternating).
Depending on the current tag environment and how the template is processed at the time these values can come into play. Most of the time, entering the name as passed into PROCESS
in the template as an attribute value will then evaluate to the according value using RESOLVE-VALUE
. In the case of a symbol this then delegates to CLIP
, which returns the value stored in the clipboard.
The value returned by PROCESS
is the node you passed into it. You can parse this back into a string using PLUMP:SERIALIZE
or lQuery's WRITE-TO-FILE
.
C:EXPAND
PROCESS-NODE
on itself again. This is useful to generate attributes to be expanded.C:IF
TEST
attribute or a C:TEST
tag as one of its direct children. If the test as by RESOLVE-VALUE
is non-NIL, all children of the C:THEN
tag are spliced in place of the C:IF
block. Otherwise it looks for C:ELSEIF
blocks and checks their TEST
attributes in turn. The contents of the first block whose TEST
passes is spliced. If none pass, the C:ELSE
child block, if any, is spliced.C:ITERATE
OVER
and then works like the ITERATE
attribute processor using the value of the OVER
attribute.C:LET
RESOLVE-VALUE
of the attribute value. Acts like SPLICE
.C:NOOP
C:SPLICE
C:UNLESS
C:USING
VALUE
attribute. Acts like SPLICE
.C:WHEN
TEST
attribute and if the value of it as by RESOLVE-VALUE
is non-NIL, acts like SPLICE
. Otherwise it removes itself including its children from the DOM.C:CASE
VALUE
attribute that each element in the body is compared against, just like CL's case
. Each element in the body may have a VALUE
attribute to compare again, or a VALUES
attribute that is a space-separated list, too.C:S
<c:splice lquery="(text ...)"></c:splice>
where ... is the contents of the element.C:H
<c:splice lquery="(html ...)"></c:splice>
where ... is the contents of the element.If you specify attributes that are not known on a standard tag, a warning of type UNKNOWN-ATTRIBUTE
is signalled. If you do not specify a required attribute on a standard tag, an error of type MISSING-ATTRIBUTE
is signalled.
AS
COUNT
*TARGET-COUNT*
as the attribute value. This is useful to follow processing order during debugging.EVAL
EVAL
on the value of READ-FROM-STRING
of the attribute value.ITERATE
RESOLVE-VALUE
) is used as an iteration list or vector. The first node within the node this attribute belongs to is copied once for each item in the iteration list and processed with that item used as the clipboard.LQUERY
($ node ..)
. Note that only lQuery functions are available, not its macros.FILL
{thing}
by the result of clip
on the respective object's field thing
.You can define new tag and attribute processors with the macros DEFINE-TAG-PROCESSOR
and DEFINE-ATTRIBUTE-PROCESSOR
. For tag processors you will usually want to make sure to call PROCESS-ATTRIBUTES
and PROCESS-CHILDREN
to ensure that tags and attributes within are properly processed. To retrieve values most of the time you need to use RESOLVE-VALUE
(or its shorthand RESOLVE-ATTRIBUTE
) unless you want to whip up your own system for one reason or another. All tags that you define will automatically be prefixed with C:
in order to help highlighting template tags and ensure that there are no collisions with existing tags.
The Emacs extension Web-Mode(version 9.0.77+) provides syntax highlighting for Clip templates. In order for it to recognise the templates, use the .ctml
file extension. A huge thanks goes to Bois Francois-Xavier for adding the support.
These are short tutorials to help explaining the effects of each tag and to illustrate some basic templating techniques. The examples will only work with Clip>=0.5.1 and lQuery>=3.1.1 .
(clip:process-to-string
2 | "<span lquery=\"(text text) (add-class class)\" />"
3 | :text "Hi!" :class "clip-text")
4 |
Explanation: The LQUERY
attribute allows you to perform lQuery operations on the node it is an attribute of. In this case, the TEXT
function sets the text of the node to the value of TEXT
, which we told Clip to be "Hi!"
. Similarly for ADD-CLASS
. Any non-keyword symbol within the template is automatically resolved to a field on the current clipboard. You may think of the clipboard as a form of lexical environment for the template, which we currently set to have the variables TEXT
and CLASS
bound. The default CLIPBOARD
object is special in the sense that it does not differentiate between accessing it with keywords, symbols or strings and is case-insensitive. This makes it easier to access in templates.
Please see the lQuery documentation for all possible node manipulation functions.
(clip:process-to-string
5 | "<ol iterate=\"todo-list\"><li lquery=\"(text *)\"></li></ol>"
6 | :todo-list '("Write tutorials" "Make tiramisu" "Visit grandma"))
7 |
The ITERATE
attribute goes over the list or vector of elements its attribute-value resolves to and uses each item as the current clipboard for the iteration element. Since in this case these values themselves are direct strings we cannot retrieve further values from them and instead need to use *
to refer to the entire clipboard.
(clip:process-to-string
8 | "<ul iterate=\"users\">
9 | <li><c:if test=\"anonymous\"><c:then>Username Hidden</c:then><c:else lquery=\"(text username)\"/></c:if></li>
10 | </ul>"
11 | :users '((:username "Some Guy" :anonymous T) (:username "Some Other Guy" :anonymous NIL) (:username "You" :anonymous NIL)))
12 |
Clip offers a couple of constructs to perform conditionals. These constructs are C:WHEN
C:UNLESS
and C:IF
, after their CL equivalents. Each take an attribute called TEST
that has to resolve to a non-NIL value to be taken as true. In the case of C:IF
, three special local child tags are used: C:THEN
, C:ELSE
and C:TEST
. The C:TEST
tag can be used as an alternative to the test attribute. The other two should be self-explanatory. Note that none of the child-tags or attributes of an unchosen branch are processed.
(clip:process-to-string
13 | "<c:using value=\"num\">
14 | <c:let orig=\"*\" double=\"(* * 2)\" square=\"(expt * 2)\" root=\"(sqrt *)\">
15 | <span lquery=\"(text (list orig double square root))\" />
16 | </c:let>
17 | </c:using>"
18 | :num 2)
19 |
In order to manipulate the clipboard bindings you can use the C:USING
and C:LET
special tags. C:USING
replaces the clipboard environment with what the value of its VALUE
attribute resolves to. C:LET
on the other hand creates a new CLIPBOARD
object, setting the specified symbol/value pairs from its attributes.
(clip:process-to-string
20 | "<ul iterate=\"articles\">
21 | <li><article>
22 | <header><div class=\"author\" lquery=\"(text (** :author))\">AUTHOR</div></header>
23 | <section class=\"content\" lquery=\"(text *)\">CONTENT</section>
24 | </article></li>
25 | </ul>"
26 | :author "Max Mastermind" :articles '("Whoa I am blogging!!" "I don't know what to write, sadface."))
27 |
Sometimes you need to refer to values in clipboards outside of the current binding. No worries, this is easy to do as the clipboards are organised using a stack. You can reach clipboards higher up in the stack using the asterisk symbols. Each asterisk more is one clipboard higher. Using the asterisk symbol as a variable returns the clipboard directly, using it as a function call is akin to doing (CLIP ** 'thing)
. In order to avoid clashing with the *
multiplication function, the asterisk function shorthand is only active for two or more asterisks.
(defun seconds () (decode-universal-time (get-universal-time)))
28 | (clip:process-to-string
29 | "<time lquery=\"(text (seconds))\">TIME</time>")
30 |
Whenever you require to use functions within clip documents, you need to be aware of the current value of *package*
. As values that are resolved are first parsed using read
, they are influenced by *package*
. You can of course use fully qualified symbol names, but often times it is useful to bind the variable to the package you need to reduce verbosity.
You must also be aware of the special resolving for symbols used as function calls within standard resolvings. As mentioned in the previous section, symbols only consisting of asterisks are specially handled. Additionally, the symbols cl:quote
, cl:function
, cl:or
, cl:and
, cl:if
, cl:when
, and cl:unless
are handled to work like their usual macro/special equivalents. Any other symbol is treated as follows: If a function's symbol with the same symbol-name is externalised from the clip
package, the clip
function is used. If not, the function named by the symbol in the symbol's package is used. This is done so that, no matter your package, you will always have access to functions like clip
and clipboard
. As an unfortunate side-effect of a symbol not knowing whether it was fully qualified or not, this means that even if you use the full symbol name with package in your template, as long as the name is external in clip
, the clip
function is used instead. You will have to use a combination of funcall
and #'
to circumvent this limitation..
Global registry of attribute processors. 31 | 32 | This has to be an EQUALP hash-table with the attribute name as keys 33 | and functions that accept two arguments (node attribute-value) as 34 | values. Binding this variable can be useful to establish local 35 | attributes.
Template storage stack. When new clipboards are bound, they are pushed onto the stack. 36 | Once the binding is left, they are popped off the stack again.
Global registry of tag processors. 37 | 38 | This has to be an EQUALP hash-table with the tag name as keys 39 | and functions that accept one argument (the node) as 40 | values. Binding this variable can be useful to establish local 41 | tags.
This variable is bound to whatever node is currently being processed.
Special class for clipboard environments. Use CLIPBOARD or CLIP to access and set values within. 42 | Field names are automatically transformed into strings as per STRING. Access is case-insensitive.
Superclass for all conditions related to problems with a node's attribute. 43 | 44 | See NODE-CONDITION
Superclass for all conditions related to Clip.
Condition signalled when a required attribute is missing. 45 | 46 | See ATTRIBUTE-CONDITION
Superclass for all conditions related to problems with a node. 47 | 48 | See CLIP-CONDITION
Condition signalled when an unknown attribute is present. 49 | 50 | See ATTRIBUTE-CONDITION
Returns the processor function for the requested attribute if one is registered. 51 | Otherwise returns NIL. See *ATTRIBUTE-PROCESSORS*.
Sets the attribute-processor bound to the given attribute to the specified function. 52 | See *ATTRIBUTE-PROCESSORS*.
Checks whether the given attribute is present on the node. 53 | 54 | If it is, the attribute's value is returned. 55 | Otherwise, an error of type MISSING-ATTRIBUTE is signalled. 56 | 57 | See MISSING-ATTRIBUTE
Checks whether there are any unknown attributes present on the node. 58 | 59 | If an unknown attribute is present, a warning of type 60 | UNKNOWN-ATTRIBUTE is signalled. Otherwise, NIL is returned. 61 | 62 | See UNKNOWN-ATTRIBUTE
Checks whether the given attribute is the only attribute on the node. 63 | 64 | If it is not present, or not the only one, an error is signalled. 65 | Otherwise, the attribute's value is returned. 66 | 67 | See CHECK-NO-UNKNOWN-ATTRIBUTES 68 | See CHECK-ATTRIBUTE
Shorthand for (CLIP (FIRST *CLIPBOARD-STACK*) FIELD)
Shorthand for (SETF (CLIP (FIRST *CLIPBOARD-STACK*) FIELD) VALUE)
Creates a new clipboard using the specified fields (like a plist).
If the passed value is a STRING it is parsed using READ-FROM-STRING and subsequently passed to RESOLVE-VALUE. 69 | If it is not a string, the value itself is returned.
Processes all clip markup on the target with the given FIELDS used to initialise the clipboard.
Processes all clip markup on the target with the given CLIPBOARD.
Processes the specified attribute using the given value. 70 | If no attribute processor can be found, nothing is done. 71 | See *ATTRIBUTE-PROCESSORS*.
Processes all attributes on the node. 72 | See PROCESS-ATTRIBUTE.
Calls PROCESS-NODE on all childrens of the passed node. 73 | 74 | This takes some care to make sure that splicing into the childrens array 75 | of the node is possible. However, note that inserting children before the 76 | node that is currently being processed will most likely lead to horrors. 77 | If such functionality is indeed ever needed (I hope not), this system 78 | needs to be rewritten to somehow be able to cope with such scenarios.
Processes the passed node. 79 | 80 | Depending on type the following is done: 81 | PLUMP:ELEMENT PROCESS-TAG is called. 82 | PLUMP:NESTING-NODE PROCESS-CHILDREN is called. 83 | PLUMP:NODE Nothing is done. 84 | T An error is signalled. 85 | Any call to this also increases the *TARGET-COUNTER* regardless of what 86 | is done.
Processes the specified node as the given tag. 87 | If no tag processor can be found, PROCESS-ATTRIBUTES and PROCESS-CHILDREN is called. 88 | See *TAG-PROCESSORS*.
Same as PROCESS, but automatically performs PLUMP:SERIALIZE on the result to a string.
Shorthand to resolve the value of an attibute. 89 | See RESOLVE-VALUE.
Returns the processor function for the requested tag if one is registered. 90 | Otherwise returns NIL. See *TAG-PROCESSORS*.
Sets the tag-processor bound to the given tag-name to the specified function. 91 | See *TAG-PROCESSORS*.
Generic object accessor. 92 | If you want to get special treatment of objects or types, define your own methods on this.
Generic object setter. 93 | If you want to get special treatment of objects or types, define your own methods on this.
Attempts to resolve the object to a specific value. 94 | This is usually used in combination with READ-FROM-STRING of an attribute value.
Defines a new attribute processor. 95 | 96 | ATTRIBTUE --- A symbol or string that matches the attribute to process (case-insensitive) 97 | NODE --- The current node is bound to this symbol. 98 | VALUE --- The value of the attribute is bound to this symbol. 99 | BODY ::= form*
Defines a new attribute processor. 100 | 101 | TAG --- A symbol or string that matches the tag name to process (case-insensitive) 102 | NODE --- The node to process is bound to this symbol 103 | BODY ::= form*
Executes the body with the new clipboard on the *CLIPBOARD-STACK*. 104 | 105 | If fields are provided, they are set on the NEW-CLIPBOARD in plist fashion as per consecutive SETF. 106 | This means that side-effects of an early field set affect later fields. The fields are evaluated 107 | before the NEW-CLIPBOARD is pushed onto the *CLIPBOARD-STACK*.