more documentation
[closure-html.git] / doc / examples.xml
blob9086eaa89838f174f3e773c9a29f98a9a01c5320
1 <page title="Closure HTML Examples">
2   <p>
3     Simple examples using Closure HTML.
4   </p>
6   <toc/>
7   
8   <div style="float: left">
9   <section>Parsing a string</section>
10   <example-box>
11     <fun>parse</fun>
12     <fun>make-lhtml-builder</fun>
13     <fun>serialize-lhtml</fun>
14     <fun>make-string-sink</fun>
15   </example-box>
17   <p>Parse into LHTML:</p>
18   <example>(chtml:parse "&lt;p>nada&lt;/p>" (chtml:make-lhtml-builder))</example>
19   <result>(:HTML NIL (:HEAD NIL) (:BODY NIL (:P NIL "nada")))</result>
21   <p>Serialize LHTML back into a string:</p>
22   <example>(chtml:serialize-lhtml * (chtml:make-string-sink))</example>
23   <result>"&lt;HTML>&lt;HEAD>&lt;/HEAD>&lt;BODY>&lt;P>nada&lt;/P>&lt;/BODY>&lt;/HTML>"</result>
25   <section>Parsing a file</section>
27   <example-box>
28     <fun>parse</fun>
29     <fun>make-lhtml-builder</fun>
30   </example-box>
32   <p>
33     Note that the filename must be passed as a pathname (written
34     using <tt>#p</tt>), not just a
35     string, because a string would be interpreted as a literal HTML
36     document as in the first example above.
37   </p>
38   <example>(chtml:parse #p"example.html" (chtml:make-lhtml-builder))</example>
39   <result>(:HTML NIL (:HEAD NIL) (:BODY NIL (:P NIL "nada")))</result>
41   <section>Cleaning up broken HTML</section>
42   <example-box>
43     <fun>parse</fun>
44     <fun>make-string-sink</fun>
45   </example-box>
47   <p>
48     Many HTML syntax errors are corrected by Closure HTML
49     automatically.  In this example, we parse from a string and
50     serialize it back immediately.
51   </p>
52   <example>(defun clean-html (string)
53     (chtml:parse string (chtml:make-string-sink)))</example>
54   <result>CLEAN-HTML</result>
55   <p>
56     Note the differences between input and output in the following document:
57   </p>
58   <ul>
59     <li>&lt;title> is moved into &lt;head>.</li>
60     <li>The <tt>bogus</tt> attribute is removed.</li>
61     <li>&lt;br is corrected to &lt;br> and &lt;/oops> to &lt;/p>.</li>
62   </ul>
63   <example>(clean-html "&lt;title>cleanup example&lt;/title>
64 &lt;p bogus>
65 &lt;br
66 &lt;/oops>")</example>
67   <result>"&lt;HTML>&lt;HEAD>&lt;TITLE>cleanup example&lt;/TITLE>&lt;/HEAD>&lt;BODY>&lt;P>
68 &lt;BR>&lt;/P>&lt;/BODY>&lt;/HTML>"</result>
70   <section>Translating an HTML file to XHTML</section>
71   <example-box>
72     <fun>parse</fun>
73     <a href="http://common-lisp.net/project/cxml/sax.html#serialization">
74       cxml:make-octet-stream-sink
75     </a>
76   </example-box>
78   <p>
79     In this example, we parse an HTML file and serialize it into XHTML.
80   </p>
81   <p>
82     This example
83     uses <a href="http://common-lisp.net/project/cxml">Closure XML</a>.
84   </p>
85   <example>(defun html2xhtml (file &amp;key (if-exists :error))
86     (with-open-file (out (make-pathname :type "xml" :defaults file)
87                          :element-type '(unsigned-byte 8)
88                          :if-exists if-exists
89                          :direction :output)
90       (chtml:parse (pathname file)
91                    (cxml:make-octet-stream-sink out))))</example>
92   <result>HTML2XHTML</result>
93   Use like this:
94   <example>(html2xhtml "/home/david/test.html" :if-exists :supersede)</example>
95   <p>
96     The following input file and its XHTML version illustrate some of
97     the differences between the two syntaxes.
98   </p>
99   <p>
100     <b>test.html</b>:
101     <pre style="border: solid 1px #9c0000;
102                 padding: 1em;
103                 width: 60%;">&lt;p>foo&lt;/p>
104 &lt;br>
105 &lt;br>
106 &lt;br>
107 &lt;select>
108 &lt;option selected>123
109 &lt;option>456
110 &lt;/select></pre>
111   </p>
112   <p>
113     <b>test.xml</b>:
114     <pre style="border: solid 1px #9c0000;
115                 padding: 1em;
116                 width: 60%;">&lt;?xml version="1.0" encoding="UTF-8"?>
117 &lt;html xmlns="http://www.w3.org/1999/xhtml">&lt;head/>&lt;body>&lt;p>foo&lt;/p>
118 &lt;br/>
119 &lt;br/>
120 &lt;br/>
121 &lt;select>&lt;option selected="selected">123
122 &lt;/option>&lt;option>456
123 &lt;/option>&lt;/select>
124 &lt;/body>&lt;/html></pre>
125   </p>
127   <section>Translating an XHTML file to HTML</section>
128   <example-box>
129     <a href="http://common-lisp.net/project/cxml/sax.html">
130       cxml:parse
131     </a>
132     <fun>make-octet-stream-sink</fun>
133   </example-box>
135   <p>
136     This is a continuation of the opposite example above.  In that
137     example, we converted an HTML file to HTML.   Going back to HTML is
138     just as easy:
139   </p>
140   <example>(defun xhtml2html (file &amp;key (if-exists :error))
141     (with-open-file (out (make-pathname :type "html" :defaults file)
142                          :element-type '(unsigned-byte 8)
143                          :if-exists if-exists
144                          :direction :output)
145       (cxml:parse (pathname file)
146                   (chtml:make-octet-stream-sink out))))</example>
147   <result>XHTML2HTML</result>
148   Running this function on the example above results in a clean-up
149   version of the original document: 
150   <p>
151     <b>test.html</b>:
152     <pre style="border: solid 1px #9c0000;
153                 padding: 1em;
154                 width: 60%;">&lt;html>&lt;head>&lt;/head>&lt;body>&lt;p>foo&lt;/p>
155 &lt;br>
156 &lt;br>
157 &lt;br>
158 &lt;select>&lt;option selected>123
159 &lt;/option>&lt;option>456
160 &lt;/option>&lt;/select>
161 &lt;/body>&lt;/html></pre>
162   </p>
164   <section>Fetching and parsing Google search results</section>
165   <example-box>
166     <fun>parse</fun>
167     <a href="http://weitz.de/drakma/#http-request">
168       drakma:http-request
169     </a>
170     <a href="http://www.lichteblau.com/cxml-stp/doc/pages/cxml-stp__fun__make-builder.html">
171       cxml-stp:make-builder
172     </a>
173     <a href="http://www.lichteblau.com/cxml-stp/doc/pages/cxml-stp__macro__do-recursively.html">
174       cxml-stp:do-recursively
175     </a>
176     <a href="http://www.lichteblau.com/cxml-stp/doc/pages/cxml-stp__class__element.html">
177       cxml-stp:element
178     </a>
179     <a href="http://www.lichteblau.com/cxml-stp/doc/pages/cxml-stp__fun__local-name.html">
180       cxml-stp:local-name
181     </a>
182     <a href="http://www.lichteblau.com/cxml-stp/doc/pages/cxml-stp__fun__attribute-value.html">
183       cxml-stp:attribute-value
184     </a>
185   </example-box>
187   <p>
188     In this example, we perform a google search and print the first ten
189     results by looking for all links of the form &lt;a class="l">.
190   </p>
191   <p>
192     This example
193     uses <a href="http://weitz.de/drakma">Drakma</a> to perform the HTTP
194     request, and the DOM
195     alternative <a href="http://www.lichteblau.com/cxml-stp/">cxml-stp</a>.
196   </p>
197   <example>(defun show-google-hits (term)
198     (let* ((query (list (cons "q" term)))
199            (str (drakma:http-request "http://www.google.com/search"
200                                      :parameters query))
201            (document (chtml:parse str (cxml-stp:make-builder))))
202       (stp:do-recursively (a document)
203         (when (and (typep a 'stp:element)
204                    (equal (stp:local-name a) "a")
205                    (equal (stp:attribute-value a "class") "l"))
206           (format t "~A:~%  ~A~%~%"
207                   (stp:string-value a)
208                   (stp:attribute-value a "href"))))))</example>
209   <result>SHOW-GOOGLE-HITS</result>
210   Searching for "lisp" we get these results:
211   <example>(show-google-hits "lisp")</example>
212   <result>Lisp (programming language) - Wikipedia, the free encyclopedia:
213   http://en.wikipedia.org/wiki/Lisp_programming_language
215 Lisp - Wikipedia, the free encyclopedia:
216   http://en.wikipedia.org/wiki/Lisp
218 Association of Lisp Users:
219   http://www.lisp.org/
221 An Introduction and Tutorial for Common Lisp:
222   http://www.apl.jhu.edu/~hall/lisp.html
224 Lisp:
225   http://www.paulgraham.com/lisp.html
227 The Roots of Lisp:
228   http://www.paulgraham.com/rootsoflisp.html
230 Planet Lisp:
231   http://planet.lisp.org/
233 Practical Common Lisp:
234   http://www.gigamonkeys.com/book/
236 CLISP - an ANSI Common Lisp Implementation:
237   http://clisp.cons.org/
239 Lisp FAQ:
240   http://www.cs.cmu.edu/Groups/AI/html/faqs/lang/lisp/top.html</result>
242 <!--
243   <section>Serializing using WITH-HTML-OUTPUT</section>
244   <example-box>
245     <macro>with-html-output</macro>
246     <macro>with-element</macro>
247     <fun>attribute</fun>
248     <fun>text</fun>
249   </example-box>
252   </div>
253 </page>