-
Notifications
You must be signed in to change notification settings - Fork 23
/
Rakefile
218 lines (192 loc) · 6.74 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
require 'rake/clean'
require_relative 'lib/lib_renumber.rb'
require_relative 'lib/lib_src2md.rb'
require_relative 'lib/lib_gen_main_tex.rb'
require_relative 'lib/lib_mk_html_template.rb'
require_relative 'lib/lib_change_relative_link.rb'
# Usually, the suffix of xxx.src.md is .md because a suffix/extension is the part after the last period.
# But, we want the Rakefile to recognize that the suffix of xxx.src.md is .src.md.
# So we define some methods for that.
# Redefine String#ext and FileList#ext
class String
alias :_org_ext :ext
def ext suffix
file = self.sub(/\.src\.md\Z/, "")._org_ext(suffix)
end
end
class FileList
def ext suffix
self.map{|fl| fl.ext(suffix)}
end
end
# get section number from src/secXX.src.md
def get_sec_num(src)
src.match(/sec(\d+)\.src\.md$/).to_a[1].to_i # no number => 0
end
# get C file names in the .src.md file.
def c_files path
dir = path.pathmap("%d")
File.read(path).scan(/^@@@include\n(.*?)@@@\n/m).flatten\
.map{|s| s.each_line.to_a}.flatten\
.map{|line| "#{dir}/#{(line.match(/^\S*/)[0])}"}
end
# source files
secfiles = FileList['src/sec*.src.md']
renumber(secfiles)
secfiles = FileList['src/sec*.src.md']
secfiles.sort!{|f,g| f.match(/\d+\.src\.md$/).to_a[0].to_i <=> g.match(/\d+\.src\.md$/).to_a[0].to_i}
otherfiles = FileList[]
srcfiles = secfiles + otherfiles
abstract = "src/abstract.src.md"
# imagesrcfiles are the image files used in the srcfiles.
# They are absolute paths.
imagefiles = srcfiles.map do |file|
d = file.pathmap("%d")
File.read(file)\
.gsub(/^~~~.*?^~~~\n/m,'').gsub(/^ .*\n/,'')\
.scan(/!\[.*?\]\((.*?)\)/).flatten.sort.uniq\
.map{|img| File.absolute_path("#{d}/#{img}")}
end.flatten.sort.uniq
CLEAN.append(FileList["latex/*.tex", "latex/*.aux", "latex/*.log", "latex/*.toc"])
CLOBBER.append("Readme.md").append(FileList["gfm/*.md"])
CLOBBER.append(FileList["docs/*.html"])
CLOBBER.append(FileList["docs/image/*"])
CLOBBER.append(FileList["latex/*.pdf"])
# tasks
task default: :md
task all: [:md, :html, :pdf]
mdfiles = srcfiles.pathmap("%f").ext(".md").map{|f| "gfm/#{f}"}
task md: %w[Readme.md] + mdfiles
file "Readme.md" => [abstract] + secfiles do |t|
abstract_md = "gfm/"+abstract.pathmap("%f").ext(".md")
src2md(abstract, "gfm")
buf = File.readlines(abstract_md)\
+ ["\n## Table of contents\n\n"]
File.delete(abstract_md)
secfiles.each do |secfile|
h = File.open(secfile){|file| file.readline}.sub(/^#* */,"").chomp
buf << "1. [#{h}](#{secfile.pathmap('%f').ext(".md")})\n"
end
readme_md = buf.join
readme_md = change_relative_link(readme_md, "gfm", ".")
File.write(t.name, readme_md)
end
# srcfiles => mdfiles
srcfiles.each do |src|
dst = "gfm/"+src.pathmap("%f").ext(".md")
file dst => [src] + c_files(src) do |t|
src2md(src, "gfm")
i = get_sec_num(src)
if secfiles.include?(src)
if secfiles.size == 1
nav = "Up: [Readme.md](../Readme.md)\n"
elsif i == 1
nav = "Up: [Readme.md](../Readme.md), Next: [Section 2](sec2.md)\n"
elsif i == secfiles.size
nav = "Up: [Readme.md](../Readme.md), Prev: [Section #{i-1}](sec#{i-1}.md)\n"
else
nav = "Up: [Readme.md](../Readme.md), Prev: [Section #{i-1}](sec#{i-1}.md), Next: [Section #{i+1}](sec#{i+1}.md)\n"
end
File.write(t.name, nav + "\n" + File.read(dst) + "\n" + nav)
end
end
end
htmlfiles = srcfiles.pathmap("%f").ext(".html").map{|f| "docs/#{f}"}
htmlimagefiles = imagefiles.pathmap("%f").map{|f| "docs/image/#{f}"}
task html: %W[docs/index.html docs/.nojekyll docs] + htmlfiles + htmlimagefiles
file "docs/index.html" => [abstract] + secfiles do
abstract_md = "docs/"+abstract.pathmap("%f").ext(".md")
src2md(abstract, "html")
buf = [ "# GObject Tutorial for beginners\n\n" ]\
+ File.readlines(abstract_md)\
+ ["\n## Table of contents\n\n"]
File.delete(abstract_md)
secfiles.each do |secfile|
h = File.open(secfile){|file| file.readline}.sub(/^#* */,"").chomp
buf << "1. [#{h}](#{secfile.pathmap("%f").ext("html")})\n"
end
buf << "\nThis website uses [Bootstrap](https://getbootstrap.jp/)."
File.write("docs/index.md", buf.join)
mk_html_template(nil, nil, nil)
sh "pandoc -s --template=docs/template.html --metadata=title:\"GObject tutorial\" -o docs/index.html docs/index.md"
File.delete "docs/index.md"
File.delete "docs/template.html"
end
file "docs/.nojekyll" => "docs" do |t|
touch t.name
end
srcfiles.each do |src|
dst = "docs/"+src.pathmap("%f").ext("html")
file dst => [src, "docs"] + c_files(src) do |t|
html_md = "docs/"+src.pathmap("%f").ext(".md")
src2md(src, "html")
i = get_sec_num(src)
if secfiles.include?(src)
if secfiles.size == 1
mk_html_template("index.html", nil, nil)
elsif i == 1
mk_html_template("index.html", nil, "sec2.html")
elsif i == secfiles.size
mk_html_template("index.html", "sec#{i-1}.html", nil)
else
mk_html_template("index.html", "sec#{i-1}.html", "sec#{i+1}.html")
end
else
mk_html_template("index.html", nil, nil)
end
sh "pandoc -s --template=docs/template.html --metadata=title:\"GObject tutorial\" -o #{t.name} #{html_md}"
File.delete(html_md)
File.delete "docs/template.html"
end
end
imagefiles.each do |src|
dst = "docs/image/"+src.pathmap("%f")
file dst => [src, "docs/image"] do |t|
cp t.source, t.name
end
end
task pdf: %w[latex/main.tex] do
sh "cd latex; lualatex main.tex"
sh "cd latex; lualatex main.tex"
sh "mv latex/main.pdf latex/gobject_tutorial.pdf"
end
texfiles = srcfiles.pathmap("%f").ext(".tex").map{|f| "latex/#{f}"}
sec_texfiles = secfiles.pathmap("%f").ext(".tex").map{|f| "latex/#{f}"}
other_texfiles = otherfiles.pathmap("%f").ext(".tex").map{|f| "latex/#{f}"}
abstract_tex = "latex/"+abstract.pathmap("%f").ext(".tex")
file "latex/main.tex" => [abstract_tex] + texfiles do
gen_main_tex "latex", abstract_tex, sec_texfiles, other_texfiles
end
file abstract_tex => abstract do |t|
abstract_md = "latex/"+abstract.pathmap("%f").ext(".md")
src2md(abstract, "latex")
sh "pandoc --listings -o #{t.name} #{abstract_md}"
File.delete(abstract_md)
end
srcfiles.each do |src|
dst = "latex/"+src.pathmap("%f").ext(".tex")
file dst => [src] + c_files(src) do |t|
tex_md = "latex/"+src.pathmap("%f").ext(".md")
src2md(src, "latex")
if src == "src/Readme_for_developers.src.md"
sh "pandoc -o #{t.name} #{tex_md}"
else
sh "pandoc --listings -o #{t.name} #{tex_md}"
end
File.delete(tex_md)
end
end
["gfm", "docs", "docs/image", "latex"].each do|d|
directory d
end
task :clean
task :clobber
task :cleangfm do
remove_entry_secure("gfm")
end
task :cleanhtml do
remove_entry_secure("docs")
end
task :cleanlatex do
remove_entry_secure("latex")
end