factor/basis/xmode/code2html/code2html.factor

49 lines
1.2 KiB
Factor
Raw Normal View History

2008-05-27 02:18:38 -04:00
USING: xmode.tokens xmode.marker xmode.catalog kernel
2008-03-11 04:39:09 -04:00
html.elements io io.files sequences words io.encodings.utf8
namespaces xml.entities accessors ;
IN: xmode.code2html
: htmlize-tokens ( tokens -- )
[
[ str>> ] [ id>> ] bi [
<span name>> =class span> escape-string write </span>
] [
escape-string write
] if*
] each ;
: htmlize-line ( line-context line rules -- line-context' )
tokenize-line htmlize-tokens ;
2007-12-06 00:23:18 -05:00
: htmlize-lines ( lines mode -- )
2007-12-06 01:03:27 -05:00
f swap load-mode [ htmlize-line nl ] curry reduce drop ;
: default-stylesheet ( -- )
<style>
2008-09-04 21:11:28 -04:00
"resource:basis/xmode/code2html/stylesheet.css"
2008-05-23 18:45:14 -04:00
utf8 file-contents escape-string write
</style> ;
2007-12-06 00:23:18 -05:00
: htmlize-stream ( path stream -- )
lines swap
<html>
<head>
default-stylesheet
2008-05-23 18:45:14 -04:00
<title> dup escape-string write </title>
2007-12-06 00:23:18 -05:00
</head>
<body>
2007-12-06 01:03:27 -05:00
<pre>
over empty?
[ 2drop ]
[ over first find-mode htmlize-lines ] if
</pre>
2007-12-06 00:23:18 -05:00
</body>
</html> ;
: htmlize-file ( path -- )
2008-03-11 04:39:09 -04:00
dup utf8 [
dup ".html" append utf8 [
input-stream get htmlize-stream
2008-03-11 04:39:09 -04:00
] with-file-writer
] with-file-reader ;