Merge remote branch 'GArik/garik'
[progit-mk.git] / epub / make_epub.sh
blobe4e743fb7eb70806721a59f511ff3477695f7186
1 #!/usr/bin/env groovy
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 {
7 InputStream input;
8 StringBuffer sb;
10 TextDumper(input, sb) {
11 this.input = input;
12 this.sb = sb;
15 void run() {
16 def isr = new InputStreamReader(input);
17 def br = new BufferedReader(isr);
18 String next;
19 try {
20 while ((next = br.readLine()) != null) {
21 sb.append(next);
22 sb.append("\n");
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'"
32 return
34 def lang = args[0]
35 def langDir = new File("..", lang)
36 def figures = new File("../figures")
37 def dest = new File("target")
38 dest.deleteDir()
39 dest.mkdirs()
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 ->
48 String line = null
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
58 writer.print(line)
59 writer.print("\n")
60 } //while
61 } // reader
62 } //writer
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));
72 thread.start();
73 try { thread.join(); } catch (InterruptedException ignore) {}
74 try { proc.waitFor(); } catch (InterruptedException ignore) {}
76 groovyLessThan165()
77 //Groovy >= 1.6.5: proc.waitForProcessOutput(sour, serr)
78 htmlFile.withWriter {
79 it << sout.toString()
81 } //file
82 } //dir
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()
88 file.eachLine {
89 writer << it
90 writer << '\n'
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();
106 proc.waitFor();