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 ;
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
17 CONSTANT: codebook-style
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] ] }
39 : first-line ( filename encoding -- line )
40 [ readln ] with-file-reader ;
45 : include-file-name? ( name -- ? )
47 [ path-components [ "." head? ] none? ]
48 [ link-info regular-file? ]
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
57 ] map [ mode>> ] filter [ name>> ] sort-with ;
59 : html-name-char ( char -- str )
61 { [ dup alpha? ] [ 1string ] }
62 { [ dup digit? ] [ 1string ] }
63 [ >hex 6 CHAR: 0 pad-head "_" "_" surround ]
66 : file-html-name ( name -- name )
67 [ html-name-char ] { } map-as concat ".html" append ;
69 : toc-list ( files -- list )
70 [ name>> ] map natural-sort [
71 [ file-html-name ] keep
72 [XML <li><a href=<->><-></a></li> XML]
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 ;
79 ! We wrap every line in <tt> because Kindle tends to forget the font when
81 : htmlize-tokens ( tokens line# -- html-tokens )
83 [ str>> zwnj ] [ id>> ] bi codebook-style case
84 ] map [XML <tt><font size="-2" color="#666666"><-></font> <-></tt> XML]
87 : line#>string ( i line#len -- i-string )
88 [ number>string ] [ CHAR: \s pad-head ] bi* ;
90 :: code>html ( dir file -- page )
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>
100 <title><-name-></title>
101 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
105 <pre><-html-lines-></pre>
106 <mbp:pagebreak xmlns:mbp="http://www.mobipocket.com/mbp" />
110 :: code>toc-html ( dir name files -- html )
111 "Generating HTML table of contents" print flush
113 now timestamp>rfc822 :> timestamp
114 dir absolute-path :> source
116 files toc-list :> toc
118 <XML <!DOCTYPE html> <html>
120 <title><-name-></title>
121 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
125 <font size="-2">Generated from<br/>
126 <b><tt><-source-></tt></b><br/>
127 at <-timestamp-></font><br/>
130 <mbp:pagebreak xmlns:mbp="http://www.mobipocket.com/mbp" />
135 :: code>ncx ( dir name files -- xml )
136 "Generating NCX table of contents" print flush
140 name file-html-name :> filename
141 i 2 + number>string :> istr
143 [XML <navPoint class="book" id=<-filename-> playOrder=<-istr->>
144 <navLabel><text><-name-></text></navLabel>
145 <content src=<-filename-> />
147 ] map-index :> file-nav-points
149 <XML <?xml version="1.0" encoding="UTF-8" ?>
150 <ncx version="2005-1" xmlns="http://www.daisy.org/z3986/2005/ncx/">
152 <navPoint class="book" id="toc" playOrder="1">
153 <navLabel><text>Table of Contents</text></navLabel>
154 <content src="_toc.html" />
160 :: code>opf ( dir name files -- xml )
161 "Generating OPF manifest" print flush
162 name ".ncx" append :> ncx-name
165 name>> file-html-name dup
166 [XML <item id=<-> href=<-> media-type="text/html" /> XML]
167 ] map :> html-manifest
169 files [ name>> file-html-name [XML <itemref idref=<-> /> XML] ] map :> html-spine
171 <XML <?xml version="1.0" encoding="UTF-8" ?>
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" />
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" />
185 <item id="toc" href=<-ncx-name-> media-type="application/x-dtbncx+xml" />
188 <itemref idref="html-toc" />
192 <reference type="toc" title="Table of Contents" href="_toc.html" />
196 : write-dest-file ( xml name ext -- )
197 append utf8 [ write-xml ] with-file-writer ;
199 SYMBOL: kindlegen-path
200 kindlegen-path [ "kindlegen" ] initialize
202 SYMBOL: codebook-output-path
203 codebook-output-path [ "resource:codebooks" ] initialize
205 : kindlegen ( path -- )
206 [ kindlegen-path get "-unicode" ] dip 3array try-process ;
208 : kindle-path ( directory name extension -- path )
209 [ append-path ] dip append ;
211 :: codebook ( src-dir -- )
212 codebook-output-path get normalize-path :> dest-dir
214 "Generating ebook for " write src-dir write " in " write dest-dir print flush
216 dest-dir make-directories
219 src-dir file-name :> name
220 src-dir code-files :> files
222 src-dir name files code>opf
223 name ".opf" write-dest-file
225 "vocab:codebook/cover.jpg" "." copy-file-into
227 src-dir name files code>ncx
228 name ".ncx" write-dest-file
230 src-dir name files code>toc-html
231 "_toc.html" "" write-dest-file
234 src-dir file code>html
235 file name>> file-html-name "" write-dest-file
238 "." name ".opf" kindle-path kindlegen
239 "." name ".mobi" kindle-path dest-dir copy-file-into
241 dest-dir name ".mobi" kindle-path :> mobi-path
243 "Job's finished: " write mobi-path print flush
244 ] cleanup-unique-directory
245 ] with-temp-directory ;