]> gitweb.factorcode.org Git - factor.git/blob - basis/xmode/code2html/code2html.factor
6938f40bebae8b90bad222e35aae061926460636
[factor.git] / basis / xmode / code2html / code2html.factor
1 USING: xmode.tokens xmode.marker xmode.catalog kernel io io.files
2 sequences words io.encodings.utf8 namespaces xml.entities accessors
3 xml.syntax 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         ] when*
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     { "\n" } join ;
20
21 : default-stylesheet ( -- xml )
22     "resource:basis/xmode/code2html/stylesheet.css"
23     utf8 file-contents
24     [XML <style><-></style> XML] ;
25
26 :: htmlize-stream ( path stream -- xml )
27     stream stream-lines
28     [ "" ] [ path over first find-mode htmlize-lines ]
29     if-empty :> input
30     default-stylesheet :> stylesheet
31     <XML <!DOCTYPE html> <html>
32         <head>
33             <-stylesheet->
34             <title><-path-></title>
35         </head>
36         <body>
37             <pre><-input-></pre>
38         </body>
39     </html> XML> ;
40
41 : htmlize-file ( path -- )
42     dup utf8 [
43         dup ".html" append utf8 [
44             input-stream get htmlize-stream write-xml
45         ] with-file-writer
46     ] with-file-reader ;