forked from progit/progit2-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
136 lines (121 loc) · 3.94 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
namespace :book do
desc 'build basic book formats'
task :build do
puts "Generating contributors list"
`git shortlog -s --all| grep -v -E "(Straub|Chacon)" | cut -f 2- | column -c 120 > book/contributors.txt`
puts "Converting to HTML..."
`bundle exec asciidoctor progit.asc`
puts " -- HTML output at progit.html"
puts "Converting to EPub..."
`bundle exec asciidoctor-epub3 progit.asc`
puts " -- Epub output at progit.epub"
# sh "epubcheck progit.epub"
puts "Converting to Mobi (kf8)..."
`bundle exec asciidoctor-epub3 -a ebook-format=kf8 progit.asc`
puts " -- Mobi output at progit.mobi"
puts "Converting to PDF... (this one takes a while)"
`bundle exec asciidoctor-pdf progit.asc 2>/dev/null`
puts " -- PDF output at progit.pdf"
end
desc 'convert book to asciidoctor compatibility'
task:convert do
#`cp -aR ../progit2/images .`
#`sed -i -e 's!/images/!!' .gitignore`
#`git add images`
#`git rm -r book/*/images`
chapters = [
["01", "introduction" ],
["02", "git-basics" ],
["03", "git-branching" ],
["04", "git-server" ],
["05", "distributed-git" ],
["06", "github" ],
["07", "git-tools" ],
["08", "customizing-git" ],
["09", "git-and-other-scms" ],
["10", "git-internals" ],
["A", "git-in-other-environments" ],
["B", "embedding-git" ],
["C", "git-commands" ]
]
chaps =[
["C", "git-commands" ]
]
crossrefs = {}
chapters.each { | num, title |
if num =~ /[ABC]/
chap = "#{num}-#{title}"
else
chap = "ch#{num}-#{title}"
end
Dir[File.join ["book","#{num}-#{title}" , "sections","*.asc"]].map { |filename|
File.read(filename).scan(/\[\[(.*?)\]\]/)
}.flatten.each { |ref|
crossrefs[ref] = "#{chap}"
}
}
headrefs = {}
chapters.each { | num, title |
if num =~ /[ABC]/
chap = "#{num}-#{title}"
else
chap = "ch#{num}-#{title}"
end
Dir[File.join ["book","#{num}-#{title}", "*.asc"]].map { |filename|
File.read(filename).scan(/\[\[(.*?)\]\]/)
}.flatten.each { |ref|
headrefs[ref] = "#{chap}"
}
}
# transform all internal cross refs
chapters.each { | num, title |
if num =~ /[ABC]/
chap = "#{num}-#{title}"
else
chap = "ch#{num}-#{title}"
end
files = Dir[File.join ["book","#{num}-#{title}" , "sections","*.asc"]] +
Dir[File.join ["book","#{num}-#{title}" ,"1-*.asc"]]
p files
files.each { |filename|
content = File.read(filename)
new_contents = content.gsub(/\[\[(.*?)\]\]/, '[[r\1]]').gsub(/<<(.*?)>>/) { |match|
ch = crossrefs[$1]
h = headrefs[$1]
# p " #{match} -> #{ch}, #{h}"
if ch
# if local do not add the file
if ch==chap
"<<r#{$1}>>"
else
"<<#{ch}#r#{$1}>>"
end
elsif h
if h==chap
"<<#{chap}>>"
else
"<<#{h}##{h}>>"
end
end
}
File.open(filename, "w") {|file| file.puts new_contents }
}
}
chapters.each { | num, title |
if num =~ /[ABC]/
chap = "#{num}-#{title}"
else
chap = "ch#{num}-#{title}"
end
Dir[File.join ["book","#{num}-#{title}" ,"1*.asc"]].map { |filename|
content = File.read (filename)
new_contents = content.gsub(/include::(.*?)asc/) {|match|
"include::book/#{num}-#{title}/#{$1}asc"}
File.open("#{chap}.asc", "w") {|file|
file.puts "[##{chap}]\n"
file.puts new_contents }
}
}
end
end
task :default => "book:build"