]> gitweb.factorcode.org Git - factor.git/blob - extra/codebook/codebook.factor
factor: trim using lists
[factor.git] / extra / codebook / codebook.factor
1 ! Copyright (C) 2010 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays calendar calendar.format combinators
4 combinators.short-circuit io io.backend io.directories
5 io.encodings.binary io.encodings.detect io.encodings.utf8
6 io.files io.files.info io.files.temp io.files.unique io.launcher
7 io.pathnames kernel math math.parser namespaces sequences
8 sorting strings unicode xml.syntax xml.writer xmode.catalog
9 xmode.marker xmode.tokens ;
10 IN: codebook
11
12 ! Usage: "my/source/tree" codebook
13 ! Writes tree.opf, tree.ncx, and tree.html to a temporary directory
14 ! Writes tree.mobi to resource:codebooks
15 ! Requires kindlegen to compile tree.mobi for Kindle
16
17 CONSTANT: codebook-style
18     {
19         { COMMENT1 [ [XML <i><font color="#555555"><-></font></i> XML] ] }
20         { COMMENT2 [ [XML <i><font color="#555555"><-></font></i> XML] ] }
21         { COMMENT3 [ [XML <i><font color="#555555"><-></font></i> XML] ] }
22         { COMMENT4 [ [XML <i><font color="#555555"><-></font></i> XML] ] }
23         { DIGIT    [ [XML    <font color="#333333"><-></font>     XML] ] }
24         { FUNCTION [ [XML <b><font color="#111111"><-></font></b> XML] ] }
25         { KEYWORD1 [ [XML <b><font color="#111111"><-></font></b> XML] ] }
26         { KEYWORD2 [ [XML <b><font color="#111111"><-></font></b> XML] ] }
27         { KEYWORD3 [ [XML <b><font color="#111111"><-></font></b> XML] ] }
28         { KEYWORD4 [ [XML <b><font color="#111111"><-></font></b> XML] ] }
29         { LABEL    [ [XML <b><font color="#333333"><-></font></b> XML] ] }
30         { LITERAL1 [ [XML    <font color="#333333"><-></font>     XML] ] }
31         { LITERAL2 [ [XML    <font color="#333333"><-></font>     XML] ] }
32         { LITERAL3 [ [XML    <font color="#333333"><-></font>     XML] ] }
33         { LITERAL4 [ [XML    <font color="#333333"><-></font>     XML] ] }
34         { MARKUP   [ [XML <b><font color="#333333"><-></font></b> XML] ] }
35         { OPERATOR [ [XML <b><font color="#111111"><-></font></b> XML] ] }
36         [ drop ]
37     }
38
39 : first-line ( filename encoding -- line )
40     [ readln ] with-file-reader ;
41
42 TUPLE: code-file
43     name encoding mode ;
44
45 : include-file-name? ( name -- ? )
46     {
47         [ path-components [ "." head? ] none? ]
48         [ link-info regular-file? ]
49     } 1&& ;
50
51 : code-files ( dir -- files )
52     recursive-directory-files
53     [ include-file-name? ] filter [
54         dup detect-file dup binary?
55         [ f ] [ 2dup dupd first-line find-mode ] if
56         code-file boa
57     ] map [ mode>> ] filter [ name>> ] sort-with ;
58
59 : html-name-char ( char -- str )
60     {
61         { [ dup alpha? ] [ 1string ] }
62         { [ dup digit? ] [ 1string ] }
63         [ >hex 6 CHAR: 0 pad-head "_" "_" surround ]
64     } cond ;
65
66 : file-html-name ( name -- name )
67     [ html-name-char ] { } map-as concat ".html" append ;
68
69 : toc-list ( files -- list )
70     [ name>> ] map natural-sort [
71         [ file-html-name ] keep
72         [XML <li><a href=<->><-></a></li> XML]
73     ] map ;
74
75 ! insert zero-width non-joiner between all characters so words can wrap anywhere
76 : zwnj ( string -- s|t|r|i|n|g )
77     [ CHAR: \u00200c "" 2sequence ] { } map-as concat ;
78
79 ! We wrap every line in <tt> because Kindle tends to forget the font when
80 ! moving back pages
81 : htmlize-tokens ( tokens line# -- html-tokens )
82     swap [
83         [ str>> zwnj ] [ id>> ] bi codebook-style case
84     ] map [XML <tt><font size="-2" color="#666666"><-></font> <-></tt> XML]
85     "\n" 2array ;
86
87 : line#>string ( i line#len -- i-string )
88     [ number>string ] [ CHAR: \s pad-head ] bi* ;
89
90 :: code>html ( dir file -- page )
91     file name>> :> name
92     "Generating HTML for " write name write "..." print flush
93     dir [ file [ name>> ] [ encoding>> ] bi file-lines ] with-directory :> lines
94     lines length 1 + number>string length :> line#len
95     file mode>> load-mode :> rules
96     f lines [| l i | l rules tokenize-line i 1 + line#len line#>string htmlize-tokens ]
97     map-index concat nip :> html-lines
98     <XML <!DOCTYPE html> <html>
99         <head>
100             <title><-name-></title>
101             <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
102         </head>
103         <body>
104             <h2><-name-></h2>
105             <pre><-html-lines-></pre>
106             <mbp:pagebreak xmlns:mbp="http://www.mobipocket.com/mbp" />
107         </body>
108     </html> XML> ;
109
110 :: code>toc-html ( dir name files -- html )
111     "Generating HTML table of contents" print flush
112
113     now timestamp>rfc822 :> timestamp
114     dir absolute-path :> source
115     dir [
116         files toc-list :> toc
117
118         <XML <!DOCTYPE html> <html>
119             <head>
120                 <title><-name-></title>
121                 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
122             </head>
123             <body>
124                 <h1><-name-></h1>
125                 <font size="-2">Generated from<br/>
126                 <b><tt><-source-></tt></b><br/>
127                 at <-timestamp-></font><br/>
128                 <br/>
129                 <ul><-toc-></ul>
130                 <mbp:pagebreak xmlns:mbp="http://www.mobipocket.com/mbp" />
131             </body>
132         </html> XML>
133     ] with-directory ;
134
135 :: code>ncx ( dir name files -- xml )
136     "Generating NCX table of contents" print flush
137
138     files [| file i |
139         file name>> :> name
140         name file-html-name :> filename
141         i 2 + number>string :> istr
142
143         [XML <navPoint class="book" id=<-filename-> playOrder=<-istr->>
144             <navLabel><text><-name-></text></navLabel>
145             <content src=<-filename-> />
146         </navPoint> XML]
147     ] map-index :> file-nav-points
148
149     <XML <?xml version="1.0" encoding="UTF-8" ?>
150     <ncx version="2005-1" xmlns="http://www.daisy.org/z3986/2005/ncx/">
151         <navMap>
152             <navPoint class="book" id="toc" playOrder="1">
153                 <navLabel><text>Table of Contents</text></navLabel>
154                 <content src="_toc.html" />
155             </navPoint>
156             <-file-nav-points->
157         </navMap>
158     </ncx> XML> ;
159
160 :: code>opf ( dir name files -- xml )
161     "Generating OPF manifest" print flush
162     name ".ncx"  append :> ncx-name
163
164     files [
165         name>> file-html-name dup
166         [XML <item id=<-> href=<-> media-type="text/html" /> XML]
167     ] map :> html-manifest
168
169     files [ name>> file-html-name [XML <itemref idref=<-> /> XML] ] map :> html-spine
170
171     <XML <?xml version="1.0" encoding="UTF-8" ?>
172     <package
173         version="2.0"
174         xmlns="http://www.idpf.org/2007/opf"
175         unique-identifier=<-name->>
176         <metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
177             <dc:title><-name-></dc:title>
178             <dc:language>en</dc:language>
179             <meta name="cover" content="my-cover-image" />
180         </metadata>
181         <manifest>
182             <item href="cover.jpg" id="my-cover-image" media-type="image/jpeg" />
183             <item id="html-toc" href="_toc.html" media-type="text/html" />
184             <-html-manifest->
185             <item id="toc" href=<-ncx-name-> media-type="application/x-dtbncx+xml" />
186         </manifest>
187         <spine toc="toc">
188             <itemref idref="html-toc" />
189             <-html-spine->
190         </spine>
191         <guide>
192             <reference type="toc" title="Table of Contents" href="_toc.html" />
193         </guide>
194     </package> XML> ;
195
196 : write-dest-file ( xml name ext -- )
197     append utf8 [ write-xml ] with-file-writer ;
198
199 SYMBOL: kindlegen-path
200 kindlegen-path [ "kindlegen" ] initialize
201
202 SYMBOL: codebook-output-path
203 codebook-output-path [ "resource:codebooks" ] initialize
204
205 : kindlegen ( path -- )
206     [ kindlegen-path get "-unicode" ] dip 3array try-process ;
207
208 : kindle-path ( directory name extension -- path )
209     [ append-path ] dip append ;
210
211 :: codebook ( src-dir -- )
212     codebook-output-path get normalize-path :> dest-dir
213
214     "Generating ebook for " write src-dir write " in " write dest-dir print flush
215
216     dest-dir make-directories
217     [
218         [
219             src-dir file-name :> name
220             src-dir code-files :> files
221
222             src-dir name files code>opf
223             name ".opf" write-dest-file
224
225             "vocab:codebook/cover.jpg" "." copy-file-into
226
227             src-dir name files code>ncx
228             name ".ncx" write-dest-file
229
230             src-dir name files code>toc-html
231             "_toc.html" "" write-dest-file
232
233             files [| file |
234                 src-dir file code>html
235                 file name>> file-html-name "" write-dest-file
236             ] each
237
238             "." name ".opf" kindle-path kindlegen
239             "." name ".mobi" kindle-path dest-dir copy-file-into
240
241             dest-dir name ".mobi" kindle-path :> mobi-path
242
243             "Job's finished: " write mobi-path print flush
244         ] cleanup-unique-directory
245     ] with-temp-directory ;