]> gitweb.factorcode.org Git - factor.git/blob - basis/xmode/code2html/code2html.factor
factor: trim more using lists.
[factor.git] / basis / xmode / code2html / code2html.factor
1 USING: accessors io io.encodings.utf8 io.files kernel namespaces
2 sequences xml.syntax xml.writer xmode.catalog xmode.marker ;
3 IN: xmode.code2html
4
5 : htmlize-tokens ( tokens -- xml )
6     [
7         [ str>> ] [ id>> ] bi [
8             name>> swap
9             [XML <span class=<->><-></span> XML]
10         ] when*
11     ] map ;
12
13 : htmlize-line ( line-context line rules -- line-context' xml )
14     tokenize-line htmlize-tokens ;
15
16 : htmlize-lines ( lines mode -- xml )
17     [ f ] 2dip load-mode [ htmlize-line ] curry map nip
18     { "\n" } join ;
19
20 : default-stylesheet ( -- xml )
21     "resource:basis/xmode/code2html/stylesheet.css"
22     utf8 file-contents
23     [XML <style><-></style> XML] ;
24
25 :: htmlize-stream ( path stream -- xml )
26     stream stream-lines
27     [ "" ] [ path over first find-mode htmlize-lines ]
28     if-empty :> input
29     default-stylesheet :> stylesheet
30     <XML <!DOCTYPE html> <html>
31         <head>
32             <-stylesheet->
33             <title><-path-></title>
34         </head>
35         <body>
36             <pre><-input-></pre>
37         </body>
38     </html> XML> ;
39
40 : htmlize-file ( path -- )
41     dup utf8 [
42         dup ".html" append utf8 [
43             input-stream get htmlize-stream write-xml
44         ] with-file-writer
45     ] with-file-reader ;