]> gitweb.factorcode.org Git - factor.git/blob - basis/xmode/code2html/code2html.factor
Fix permission bits
[factor.git] / basis / xmode / code2html / code2html.factor
1 USING: xmode.tokens xmode.marker xmode.catalog kernel
2 html.elements io io.files sequences words io.encodings.utf8
3 namespaces xml.entities accessors ;
4 IN: xmode.code2html
5
6 : htmlize-tokens ( tokens -- )
7     [
8         [ str>> ] [ id>> ] bi [
9             <span name>> =class span> escape-string write </span>
10         ] [
11             escape-string write
12         ] if*
13     ] each ;
14
15 : htmlize-line ( line-context line rules -- line-context' )
16     tokenize-line htmlize-tokens ;
17
18 : htmlize-lines ( lines mode -- )
19     f swap load-mode [ htmlize-line nl ] curry reduce drop ;
20
21 : default-stylesheet ( -- )
22     <style>
23         "resource:basis/xmode/code2html/stylesheet.css"
24         utf8 file-contents escape-string write
25     </style> ;
26
27 : htmlize-stream ( path stream -- )
28     lines swap
29     <html>
30         <head>
31             default-stylesheet
32             <title> dup escape-string write </title>
33         </head>
34         <body>
35             <pre>
36                 over empty?
37                 [ 2drop ]
38                 [ over first find-mode htmlize-lines ] if
39             </pre>
40         </body>
41     </html> ;
42
43 : htmlize-file ( path -- )
44     dup utf8 [
45         dup ".html" append utf8 [
46             input-stream get htmlize-stream
47         ] with-file-writer
48     ] with-file-reader ;