]> gitweb.factorcode.org Git - factor.git/blob - basis/xmode/code2html/code2html.factor
Merge branch 'master' into experimental
[factor.git] / basis / xmode / code2html / code2html.factor
1 USING: xmode.tokens xmode.marker xmode.catalog kernel locals
2 html.elements io io.files sequences words io.encodings.utf8
3 namespaces xml.entities accessors xml.interpolate locals xml.writer ;
4 IN: xmode.code2html
5
6 : htmlize-tokens ( tokens -- xml )
7     [
8         [ str>> ] [ id>> ] bi [
9             name>> swap
10             [XML <span class=<->><-></span> XML]
11         ] [ ] if*
12     ] map ;
13
14 : htmlize-line ( line-context line rules -- line-context' xml )
15     tokenize-line htmlize-tokens ;
16
17 : htmlize-lines ( lines mode -- xml )
18     [ f ] 2dip load-mode [ htmlize-line ] curry map nip ;
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 lines
27     [ "" ] [ first find-mode path swap htmlize-lines ]
28     if-empty :> input
29     default-stylesheet :> stylesheet
30     <XML <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 ;