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
> (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
> (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.
> (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:
> (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