chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Egg for simple generation of HTML


From: Alejandro Forero Cuervo
Subject: [Chicken-users] Egg for simple generation of HTML
Date: Mon, 19 Jul 2004 14:00:02 -0500
User-agent: Mutt/1.5.6+20040523i

Hello.

I am attaching  an egg that I created for  generation of HTML.  I
am aware of other packages for  generation of HTML but decided to
create this for the following reasons:

- It  returns  a  srfi-40   stream  of  characters,  which  means
  the  resulting  HTML is  generated  as  needed (so  the  memory
  requirements  of  the  applications  are  relatively  constant,
  regardless of the size of the HTML generated)

- It uses  a simple  format that makes  me feel  very comfortable
  while  writing  long HTML  interspersed  with  Scheme code.   I
  didn't  like  the  formats  I  saw  for  alternative  packages.
  Granted, this is very subjective.

The library is implemented entirely as a macro.  You can see some
examples in the documentation attached.

This egg requires my stream-ext egg.

I hope others find it useful.

Alejo.
http://bachue.com/alejo

---=(  Comunidad de Usuarios de Software Libre en Colombia  )=---               
                                               
---=(  http://bachue.com/colibri )=--=( address@hidden  )=---                   
                                           
back

html-stream

Description:

Tiny, functional and highly optimized HTML generator, generating srfi-40 streams of characters.

Author:

Alejandro Forero Cuervo

Version:

This file: $Id: html-stream.html 866 2004-07-19 18:12:38Z azul $

Usage:

(require-extension html-stream)

Download:

http://anonymous:@svn.afc.no-ip.info/src/home/src/chicken-eggs/html-stream/html-stream.scm

Documentation:

[macro] (html-stream tag1 ...)

The only symbol exported from this egg, html-stream is used to generate HTML from a simple and straight-forward format. A (html-stream ...) form will evaluate to a srfi-40 stream of characters with its associated contents.

(stream->string
 (html-stream
  (p (i "The squares of the first " (b "five") " integers are:"))
  (ul
   (apply stream-append
     (map (lambda (x) (html-stream (li (* x x)))) (iota 5 1))))))
==> "<p><i>The squares of the first <b>five</b> integers are:</i></p><ul><li>1</li><li>4</li><li>9</li><li>16</li><li>25</li></ul>"

Each argument to a html-stream macro must have any of the following forms:

((tag param0 ...) arg0 ...)

If tag is the name of an HTML tag, an HTML tag with the arguments specified by the param0 ... list. This list must have an even number of elements: the even elements must be symbols for the parameter names, followed by their actual values (which must have a type accepted by ->stream-char). The forms arg0 ... are parsed recursively and their values included inside the HTML tag.

Examples:

(html-stream
 ((a href "index.html" title "Some Title") "Home Page"))
==> #,(stream "<a href='' title='Some Title'>Home Page</a>")
(html-stream
 ((img src "photo.jpeg" height 150 width 150 alt "Yes")))
==> #,(stream "<img src='' height=150 width=150 alt='Yes'/>")
(tag arg0 ...)

If tag is the name of an HTML tag, an HTML tag with no parameters is generated. The arguments arg0 ... are parsed recursively.

Note that this form is just an alias for ((tag) arg0 ...).

Examples:

(html-stream (p "My name is " (b "Alejo") "."))

Other values are evaluated (at runtime) and their results are passed to ->stream-char. The resulting streams are added to the result.

The library recognizes the following HTML tags: style, html, head, title, body, p, a, b, i, ul, ol, li, dl, dt, dd, table, tr, td, thead, tbody, h1, h2, h3, h4, h5, h6, div, span, small, big, script, select, option, textarea, center, form, pre, img, br, hr, meta, input.

Consider the code:

(html-stream
  (html
    ((body bgcolor "#ffffff")
       (p "See " ((a href url id "somelink") "page " (current-seconds) " (now)") "."))))

This is expanded (at compile time) to:

(stream-delay
 (->stream-char
  "<html><body bgcolor='#ffffff'><p>See <a href='' id='somelink'>page "
      (stream-delay
       (->stream-char
        (current-seconds)
        (stream-delay
         (->stream-char
          " (now)</a>.</p></body></html>"
          stream-null))))))))))

Note that if you don't traverse the stream, computations inside the html-stream form (such as the call to current-seconds in the example) will not get evaluated.

License:

The html-stream egg for Chicken Scheme is in the public domain and may be reproduced or copied without permission from its author. Citation of the source is appreciated.


back

Attachment: html-stream.egg
Description: Binary data

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]