factor/basis/xmode/code2html/code2html.factor

46 lines
1.2 KiB
Factor
Raw Normal View History

USING: xmode.tokens xmode.marker xmode.catalog kernel locals
2008-03-11 04:39:09 -04:00
html.elements io io.files sequences words io.encodings.utf8
namespaces xml.entities accessors xml.interpolate locals xml.writer ;
IN: xmode.code2html
: htmlize-tokens ( tokens -- xml )
[
[ str>> ] [ id>> ] bi [
name>> swap
[XML <span class=<->><-></span> XML]
] [ ] if*
] map ;
: htmlize-line ( line-context line rules -- line-context' xml )
tokenize-line htmlize-tokens ;
: htmlize-lines ( lines mode -- xml )
f -rot load-mode [ htmlize-line ] curry map nip ;
: default-stylesheet ( -- xml )
"resource:basis/xmode/code2html/stylesheet.css"
utf8 file-contents
[XML <style><-></style> XML] ;
:: htmlize-stream ( path stream -- xml )
stream lines
[ "" ] [ first find-mode path swap htmlize-lines ]
if-empty :> input
default-stylesheet :> stylesheet
<XML <html>
2007-12-06 00:23:18 -05:00
<head>
<-stylesheet->
<title><-path-></title>
2007-12-06 00:23:18 -05:00
</head>
<body>
<pre><-input-></pre>
2007-12-06 00:23:18 -05:00
</body>
</html> XML> ;
2007-12-06 00:23:18 -05:00
: htmlize-file ( path -- )
2008-03-11 04:39:09 -04:00
dup utf8 [
dup ".html" append utf8 [
input-stream get htmlize-stream write-xml
2008-03-11 04:39:09 -04:00
] with-file-writer
] with-file-reader ;