! (c)2010 Joe Groff bsd license USING: accessors arrays assocs calendar calendar.format combinators fry io io.directories io.encodings.utf8 io.files io.files.info io.files.types io.pathnames kernel locals math math.parser sequences sorting strings unicode.categories xml.syntax xml.writer xmode.catalog xmode.marker xmode.tokens ; IN: codebook ! Usage: "my/source/tree" utf8 "dest" codebook ! Writes tree.opf, tree.ncx, and tree.html to the "dest" directory ! Use kindlegen -unicode dest/tree.opf to compile tree.mobi file for Kindle CONSTANT: codebook-style { { COMMENT1 [ [XML <-> XML] ] } { COMMENT2 [ [XML <-> XML] ] } { COMMENT3 [ [XML <-> XML] ] } { COMMENT4 [ [XML <-> XML] ] } { DIGIT [ [XML <-> XML] ] } { FUNCTION [ [XML <-> XML] ] } { KEYWORD1 [ [XML <-> XML] ] } { KEYWORD2 [ [XML <-> XML] ] } { KEYWORD3 [ [XML <-> XML] ] } { KEYWORD4 [ [XML <-> XML] ] } { LABEL [ [XML <-> XML] ] } { LITERAL1 [ [XML <-> XML] ] } { LITERAL2 [ [XML <-> XML] ] } { LITERAL3 [ [XML <-> XML] ] } { LITERAL4 [ [XML <-> XML] ] } { MARKUP [ [XML <-> XML] ] } { OPERATOR [ [XML <-> XML] ] } [ drop ] } : first-line ( filename encoding -- line ) [ readln ] with-file-reader ; : code-files ( dir encoding -- files ) '[ [ link-info type>> +regular-file+ = ] filter [ dup [ ] [ _ first-line ] bi ?find-mode ] { } map>assoc [ nip ] assoc-filter [ first ] sort-with ] with-directory-tree-files ; : anchor-char ( char -- str ) { { [ dup alpha? ] [ 1string ] } { [ dup digit? ] [ 1string ] } [ >hex 6 CHAR: 0 pad-head "_" "_" surround ] } cond ; : file-anchor-name ( file -- name ) [ anchor-char ] { } map-as concat ; : toc-list ( files -- list ) natural-sort [ [ file-anchor-name "#" prepend ] [ ] bi [XML
<-html-lines->