[plt-scheme] string->html/response
maxigas wrote:
> i'm developing a little on the Continue.. tutorial: i am storing
> blog posts in HTML format and
> read them as snippets into the page. but for this i would need a
> string->html/response function,
> or something similar.
>
> e.g.: "<b>hello!</b>" -> '(b "hello!")
>
> i am sure there is such a function but i don't find it in the
> documentation.. :o
Hi,
One option is to parse the post HTML into a more Schemely
representation. There are libraries like HTMLPrag.plt and others in
recent discussion on this list that do this. It might be a more
complex solution than you require, though.
A simpler option might be to use an output format that allows you to
include raw unquoted strings in your X(H)TML output. This way so you
never need to parse the HTML from the blog post. Mirrors.plt allows
this via its "!raw" syntax.
Here's an example:
----------
#lang scheme
(require (planet untyped/mirrors:1/xml/xml))
; Here's your list of posts:
(define blog-posts
(list "Post containing <b>bold</b> text."
"Post containing <i>italics</i>."))
(pretty-print blog-posts)
(newline)
; Here's some Mirrors XML markup to create a simple web page.
; The !raw form instructs Mirrors not to quote the "<" and ">"
characters
; in the blog post: try omitting it to see the difference:
(define html
(xml (html (body (p "List of the blog posts:")
(ul ,@(for/list ([post (in-list blog-posts)])
(xml (li (!raw ,post)))))))))
; Note that Mirrors uses its own prefab-struct-based XML representation
; rather than the usual list-based representations:
(pretty-print html)
(newline)
; You can convert the structs to a string like this:
(define html-as-string
(xml->string html))
(pretty-print html-as-string)
(newline)
; Or a web-server response like this:
(define web-server-response
(make-html-response html))
(pretty-print html)
(newline)
; There's currently no way of converting between Mirrors and list-
based XML
; representations, but it's trivial code to write (feature requests
welcome on
; PLaneT Trac; code contributions welcome by email).
----------
Everything above is described on this doc page if you're interested:
http://planet.plt-scheme.org/package-source/untyped/mirrors.plt/1/3/planet-docs/mirrors/xml.html
Hope this helps,
-- Dave