On this page:
xexpr->pollen
html->pollen
url->pollen
6.12

15.3 Convert

 (require pollen/unstable/convert) package: pollen

Helper functions for converting well-made X-expressions or HTML into Pollen markup.

procedure

(xexpr->pollen x [#:white-p? white-p?])  string?

  x : xexpr?
  white-p? : boolean? = #f
Convert x to Pollen markup, using setup:command-char as the leading character. If white-p? is #t, convert p tags by taking the content out of the tag, and inserting setup:paragraph-separator in front.

Examples:
> (xexpr->pollen '(div))

"◊(div)"

> (xexpr->pollen '(div ((class "pups"))))

"◊div[#:class \"pups\"]"

> (xexpr->pollen '(div ((class "pups")) "Lex"))

"◊div[#:class \"pups\"]{Lex}"

> (xexpr->pollen '(div ((class "pups")) (p "Roxy") (p "Sidney")))

"◊div[#:class \"pups\"]{◊p{Roxy}◊p{Sidney}}"

> (xexpr->pollen #:white-p? #t '(div ((class "pups")) (p "Roxy") (p "Sidney")))

"◊div[#:class \"pups\"]{\n\nRoxy\n\nSidney}"

procedure

(html->pollen html [#:white-p? white-p?])  string?

  html : string?
  white-p? : boolean? = #f
Convert html to Pollen markup, using setup:command-char as the leading character. If white-p? is #t, convert p tags by taking the content out of the tag, and inserting setup:paragraph-separator in front.

Examples:
> (html->pollen "<hr />")

"◊(hr)"

> (html->pollen "<div class=\"pups\" />")

"◊div[#:class \"pups\"]"

> (html->pollen "<div class=\"pups\">Lex</div>")

"◊div[#:class \"pups\"]{Lex}"

> (html->pollen "<div class=\"pups\"><p>Roxy</p><p>Sidney</p></div>")

"◊div[#:class \"pups\"]{◊p{Roxy}◊p{Sidney}}"

> (html->pollen #:white-p? #t "<div class=\"pups\"><p>Roxy</p><p>Sidney</p></div>")

"◊div[#:class \"pups\"]{\n\nRoxy\n\nSidney}"

This function treats html as an XML-ish data structure. Meaning, html is expected to be syntactically well-formed (in terms of attributes and tags). But it doesn’t need to comply with a certain HTML specification. This casual approach suffices in most cases. Be aware, however, that this function won’t handle HTML style or script blocks correctly, if they contain reserved XML characters.

Examples:
> (html->pollen "<script type=\"javascript\">x < 3</script>")

"◊script[#:type \"javascript\"]{x ◊()}"

> (html->pollen "<style type=\"css\">div{content: \"<&>\";}</style>")

"◊style[#:type \"css\"]{div{content: \"◊&{\";}}}"

But if the interiors of the style or script blocks are wrapped with CDATA designations, they will convert correctly:

Examples:
> (html->pollen "<script type=\"javascript\"><![CDATA[x < 3]]></script>")

"◊script[#:type \"javascript\"]{x < 3}"

> (html->pollen "<style type=\"css\"><![CDATA[div{content: \"<&>\";}]]></style>")

"◊style[#:type \"css\"]{div{content: \"<&>\";}}"

procedure

(url->pollen url [#:white-p? white-p?])  string?

  url : (or/c string? url?)
  white-p? : boolean? = #f
Like html->pollen, but takes a url as input and fetches its HTML.