<h4>A Very Simple HSP Example</h4>
<p>The following example highlights the basics of mixing XML and Haskell together. This example uses HSP, but not HAppS.</p>

<p>The first thing to notice is the extra options we pass to GHC, namely <code>-F -pgmF trhsx</code>. This tells GHC (and GHCi) to call <code>trhsx</code> to pre-process the source code before trying to compile it. <code>trhsx</code> will automatically translate the XML syntax into normal Haskell code which the compiler can understand.</p>

> {-# OPTIONS_GHC -F -pgmF trhsx #-}
> module Main where
>
> import Data.Char (toUpper)
> import HSP
> import HSP.HTML

 <p>Next we see how to write a simple function which generates XML using the special XML syntax as well as dynamically creating some of the XML using ordinary Haskell expressions.</p>

>
> -- |hello creates a simple hello <noun> page 
> hello :: String -> HSP XML
> hello noun =
>     <html>
>      <head>
>       <title>Hello, <% noun %></title>
>      </head>
>      <body>
>       <p>Hello, <% map toUpper noun %></p>
>      </body>
>     </html>
>

<p>To use the XML syntax, we just use it -- no special escaping is required to insert XML in the middle of a Haskell expression. On the other hand, if we want to evaluate a Haskell expression and insert it into the XML, then we need the special <code>&lt;% %&gt;</code> escape clause. If we had just written, &lt;title&gt; Hello, noun&lt;/title&gt; then the <code>title</code> would have been <kbd>Hello, noun</kbd>. Likewise, if we did not have the <code>&lt;% %&gt;</code> around <code>map toUpper noun</code>, the page would say, "Hello map toUpper noun" instead of evaluating <code>map toUpper noun</code> and substituting the result in.</p>

<p>We can evaluate any arbitrary expression inside the <code>&lt;% %&gt;</code>, provided HSP knows how to turn the result into an XML value. By default, HSP knows how to handle <code>String</code>, <code>Char</code>, numbers, and some other common Haskell data-types. You can add additional class instances if you want to convert other datatypes (including your own) to XML automatically.</p>


 <p>Next we have a simple <code>main</code>. The first line evaluates
 <code>hello</code> and gets back the generated XML. The second
 line uses <code>renderAsHtml</code> to turn the XML into
 HTML. <code>renderAsHtml</code> expects you to pass in valid XHTML
 which it will transform into valid HTML. However, it does not check
 the validity of the input or output. We will do that using a more
 general mechanism in the HAppS code.</p>

>
> main :: IO ()
> main = 
>     do (_, xml) <- evalHSP (hello "World") Nothing
>        putStrLn (renderAsHTML xml)
>

<p>If we run this example, we get the following output:</p>
<pre>
&lt;html
&gt;&lt;head
  &gt;&lt;title
    &gt;Hello, World&lt;/title
    &gt;&lt;/head
  &gt;&lt;body
  &gt;&lt;p
    &gt;Hello, WORLD&lt;/p
    &gt;&lt;/body
  &gt;&lt;/html
&gt;
</pre>

<p>The formatting probably looks a bit funny to you -- but the web browser will read it fine. In HTML, the whitespace between the open and close tags is sometimes significant. However, the whitespace inside the open or close tag itself is never significant. The rendering algorithm is designed to exploit those properties to ensure it never adds significant whitespace where you didn't explicit have it in the input file.</p>
