3 //script requires Groovy
, Markdown and Calibre. On Ubuntu call
"sudo apt-get install groovy markdown calibre"
5 /*Class TextDumper is required
for Groovy
< 1.7*/
6 class TextDumper implements Runnable
{
10 TextDumper
(input
, sb
) {
16 def isr
= new InputStreamReader
(input
);
17 def br
= new BufferedReader
(isr
);
20 while ((next
= br.readLine
()) != null
) {
24 } catch
(IOException e
) {
25 throw new GroovyRuntimeException
("exception while reading process stream", e
);
29 println
"This script requires Groovy, Markdown and Calibre. On Ubuntu call 'sudo apt-get install groovy markdown calibre'"
30 if (args.size
() == 0) {
31 println
"First parameter must be name of language(=directory). For example: 'en'"
35 def langDir
= new File
("..", lang
)
36 def figures
= new File
("../figures")
37 def dest
= new File
("target")
40 def markdownFile
= ~
/.
*\.markdown
/
41 def filesForIndex
= []
42 langDir.eachDir
{ dir
->
43 dir.eachFileMatch
(markdownFile
) { file ->
44 def preparedFile
= new File
(dest
, file.getName
())
45 println
"Processing ${file}"
46 preparedFile.withWriter
{ writer
->
47 file.withReader
{ reader
->
49 while ( (line
= reader.readLine
()) != null
) {
50 def matches
= line
=~
/^\s
*Insert\s
(.
*)/
51 if (matches.size
() != 0) {
52 def imageFile
= matches
[0][1].trim
().replaceAll
(/(.
*)(\.png
)/, '$1-tn$2')
53 //println
"file:" + imageFile
54 def nextLine
= reader.readLine
()
55 line
= """![${nextLine}](../../figures/${imageFile} "${nextLine}")"""
56 //println "Line
: " + line
63 def htmlFile = new File(dest, file.getName() + ".html
")
64 filesForIndex += htmlFile;
65 def cmd = ["""markdown
""", """${preparedFile}"""]
66 println "Executing
${cmd}"
67 def proc = cmd.execute();
68 def sout = new StringBuffer()
69 def serr = new StringBuffer()
70 def groovyLessThan165 = {
71 Thread thread = new Thread(new TextDumper(proc.getInputStream(), sout));
73 try { thread.join(); } catch (InterruptedException ignore) {}
74 try { proc.waitFor(); } catch (InterruptedException ignore) {}
77 //Groovy >= 1.6.5: proc.waitForProcessOutput(sour, serr)
83 def indexFile = new File(dest, "progit.html
")
84 indexFile.withWriter { writer ->
85 writer << """<html xmlns
="http://www.w3.org/1999/xhtml"><head><title
>Pro Git
- professional version control
</title
></head><body
>"""
86 filesForIndex.sort().each { file ->
87 def fileName = file.getName()
93 writer << """</body
></html
>"""
95 def epubFile = new File(dest, "progit_
${args[0]}.epub
")
96 println "EpubFile
" + epubFile
97 def cmd = ["""ebook-convert
""", indexFile.getPath(), epubFile.getPath(),
98 """--cover""", """title.png
""",
99 """--authors""", "Scott Chacon
",
100 "--comments", "licensed under the Creative Commons Attribution-Non Commercial-Share Alike
3.0 license
",
101 "--level1-toc", "//h
:h1
",
102 "--level2-toc", "//h
:h2
",
103 "--level3-toc", "//h
:h3
"
105 def proc = cmd.execute();