2020-12-21 21:12:05 +09:00
require 'rake/clean'
2022-04-16 15:46:29 +09:00
require 'fileutils'
2021-04-27 21:08:30 +09:00
require_relative 'lib/lib_src_file.rb'
2020-12-21 21:12:05 +09:00
require_relative 'lib/lib_src2md.rb'
2021-02-06 23:50:02 +09:00
require_relative 'lib/lib_gen_main_tex.rb'
2022-04-21 23:53:28 +09:00
require_relative 'lib/lib_mk_html_template.rb'
2022-04-22 12:04:27 +09:00
require_relative 'lib/lib_cp_images.rb'
2020-12-21 21:12:05 +09:00
2022-04-16 15:46:29 +09:00
secfiles = Sec_files.new(FileList['src/sec*.src.md'].to_a.map{|file| Sec_file.new(file)})
2021-04-27 21:08:30 +09:00
secfiles.renum!
otherfiles = ["src/turtle/turtle_doc.src.md",
"src/tfetextview/tfetextview_doc.src.md",
2021-05-14 21:34:11 +09:00
"src/Readme_for_developers.src.md"].map {|file| Src_file.new file}
2021-04-27 21:08:30 +09:00
srcfiles = secfiles + otherfiles
2022-04-16 15:46:29 +09:00
abstract = Src_file.new "src/abstract.src.md"
2021-04-27 21:08:30 +09:00
2022-04-17 20:54:42 +09:00
# docs is a directory for html files.
html_dir = 'docs'
2021-04-27 21:08:30 +09:00
mdfiles = srcfiles.map {|file| "gfm/" + file.to_md}
2022-04-17 20:54:42 +09:00
htmlfiles = srcfiles.map {|file| "#{html_dir}/" + file.to_html}
2021-04-27 21:08:30 +09:00
sectexfiles = secfiles.map {|file| "latex/" + file.to_tex}
othertexfiles = otherfiles.map {|file| "latex/" + file.to_tex}
2022-04-16 15:46:29 +09:00
texfiles = sectexfiles + othertexfiles
abstract_tex = "latex/"+abstract.to_tex
2020-12-21 21:12:05 +09:00
2022-04-17 20:54:42 +09:00
["gfm", html_dir, "latex"].each{|d| Dir.mkdir(d) unless Dir.exist?(d)}
2021-01-11 11:15:04 +09:00
2022-04-21 23:53:28 +09:00
CLEAN.append(FileList["latex/*.tex", "latex/*.aux", "latex/*.log", "latex/*.toc"])
CLOBBER.append("Readme.md").append(*mdfiles)
CLOBBER.append(FileList["docs/*.html"])
2022-04-22 12:04:27 +09:00
CLOBBER.append(FileList["docs/image/*"])
2022-04-21 23:53:28 +09:00
CLOBBER.append(FileList["latex/*.pdf"])
2020-12-21 21:12:05 +09:00
2022-04-17 12:52:12 +09:00
def pair array1, array2
n = [array1.size, array2.size].max
(0...n).map{|i| [array1[i], array2[i], i]}
end
2021-01-09 15:35:06 +09:00
# tasks
2020-12-21 21:12:05 +09:00
task default: :md
2021-01-11 23:32:09 +09:00
task all: [:md, :html, :pdf]
2020-12-21 21:12:05 +09:00
2021-04-27 21:08:30 +09:00
task md: %w[Readme.md] + mdfiles
2021-01-09 15:35:06 +09:00
2021-04-27 21:08:30 +09:00
file "Readme.md" => [abstract] + secfiles do
2022-04-16 15:46:29 +09:00
src2md abstract, abstract.to_md, "gfm"
2022-04-21 23:53:28 +09:00
buf = ["# Gtk4 Tutorial for beginners\n\nThe github page of this tutorial is also available. Click [here](https://toshiocp.github.io/Gtk4-tutorial/).\n\n"]\
2022-04-16 15:46:29 +09:00
+ File.readlines(abstract.to_md)\
2022-04-21 23:53:28 +09:00
+ ["\n## Table of contents\n\n"]
2021-04-27 21:08:30 +09:00
File.delete(abstract.to_md)
2022-04-16 15:46:29 +09:00
secfiles.each_with_index do |secfile, i|
h = File.open(secfile.path){|file| file.readline}.sub(/^#* */,"").chomp
buf << "1. [#{h}](gfm/#{secfile.to_md})\n"
2021-01-09 15:35:06 +09:00
end
File.write("Readme.md", buf.join)
end
2020-12-21 21:12:05 +09:00
2022-04-16 15:46:29 +09:00
# srcfiles => mdfiles
2022-04-17 12:52:12 +09:00
pair(srcfiles, mdfiles).each do |src, dst, i|
file dst => [src] + src.c_files do
src2md src, dst, "gfm"
if src.instance_of? Sec_file
2021-04-27 21:08:30 +09:00
if secfiles.size == 1
nav = "Up: [Readme.md](../Readme.md)\n"
elsif i == 0
nav = "Up: [Readme.md](../Readme.md), Next: [Section 2](#{secfiles[1].to_md})\n"
elsif i == secfiles.size - 1
nav = "Up: [Readme.md](../Readme.md), Prev: [Section #{i}](#{secfiles[i-1].to_md})\n"
else
nav = "Up: [Readme.md](../Readme.md), Prev: [Section #{i}](#{secfiles[i-1].to_md}), Next: [Section #{i+2}](#{secfiles[i+1].to_md})\n"
end
2022-04-17 12:52:12 +09:00
File.write(dst, nav + "\n" + File.read(dst) + "\n" + nav)
2020-12-21 21:12:05 +09:00
end
end
end
2022-04-22 12:04:27 +09:00
task html: %W[#{html_dir}/index.html] + htmlfiles do
cp_images srcfiles, "docs/image"
end
2021-01-09 15:35:06 +09:00
2022-04-17 20:54:42 +09:00
file "#{html_dir}/index.html" => [abstract] + secfiles do
abstract_md = "#{html_dir}/#{abstract.to_md}"
2022-04-16 15:46:29 +09:00
src2md abstract, abstract_md, "html"
2022-04-21 23:53:28 +09:00
buf = [ "# Gtk4 Tutorial for beginners\n\n" ]\
2022-04-16 15:46:29 +09:00
+ File.readlines(abstract_md)\
2022-04-21 23:53:28 +09:00
+ ["\n## Table of contents\n\n"]
2021-04-27 21:08:30 +09:00
File.delete(abstract_md)
2022-04-16 15:46:29 +09:00
secfiles.each_with_index do |secfile, i|
h = File.open(secfile.path){|file| file.readline}.sub(/^#* */,"").chomp
buf << "1. [#{h}](#{secfile.to_html})\n"
2021-01-09 15:35:06 +09:00
end
2022-04-21 23:53:28 +09:00
buf << "\nThis website uses [Bootstrap](https://getbootstrap.jp/)."
2022-04-17 20:54:42 +09:00
File.write("#{html_dir}/index.md", buf.join)
2022-04-21 23:53:28 +09:00
mk_html_template(nil, nil, nil)
sh "pandoc -s --template=docs/template.html --metadata=title:\"Gtk4 tutorial\" -o #{html_dir}/index.html #{html_dir}/index.md"
2022-04-17 20:54:42 +09:00
File.delete "#{html_dir}/index.md"
2022-04-21 23:53:28 +09:00
File.delete "docs/template.html"
2021-01-09 15:35:06 +09:00
end
2022-04-17 12:52:12 +09:00
pair(srcfiles, htmlfiles).each do |src, dst, i|
file dst => [src] + src.c_files do
2022-04-17 20:54:42 +09:00
html_md = "#{html_dir}/#{src.to_md}"
2022-04-17 12:52:12 +09:00
src2md src, html_md, "html"
if src.instance_of? Sec_file
2021-04-27 21:08:30 +09:00
if secfiles.size == 1
2022-04-21 23:53:28 +09:00
mk_html_template("index.html", nil, nil)
2021-04-27 21:08:30 +09:00
elsif i == 0
2022-04-21 23:53:28 +09:00
mk_html_template("index.html", nil, "sec2.html")
2021-04-27 21:08:30 +09:00
elsif i == secfiles.size - 1
2022-04-21 23:53:28 +09:00
mk_html_template("index.html", "sec#{i}.html", nil)
2021-04-27 21:08:30 +09:00
else
2022-04-21 23:53:28 +09:00
mk_html_template("index.html", "sec#{i}.html", "sec#{i+2}.html")
2021-04-27 21:08:30 +09:00
end
2022-04-21 23:53:28 +09:00
else
mk_html_template("index.html", nil, nil)
2021-01-09 15:35:06 +09:00
end
2022-04-21 23:53:28 +09:00
sh "pandoc -s --template=docs/template.html --metadata=title:\"Gtk4 tutorial\" -o #{dst} #{html_md}"
2021-02-06 23:50:02 +09:00
File.delete(html_md)
2022-04-21 23:53:28 +09:00
File.delete "docs/template.html"
2020-12-21 21:12:05 +09:00
end
end
2022-04-16 15:46:29 +09:00
task pdf: %w[latex/main.tex] do
2021-03-03 23:45:54 +09:00
sh "cd latex; lualatex main.tex"
sh "cd latex; lualatex main.tex"
2021-01-11 11:15:04 +09:00
sh "mv latex/main.pdf latex/gtk4_tutorial.pdf"
end
2022-04-16 17:31:29 +09:00
file "latex/main.tex" => [abstract_tex] + texfiles do
2022-04-16 15:46:29 +09:00
gen_main_tex "latex", abstract.to_tex, sectexfiles, othertexfiles
2021-02-09 18:16:52 +09:00
end
2021-04-27 21:08:30 +09:00
file abstract_tex => abstract do
abstract_md = "latex/"+abstract.to_md
2022-04-16 15:46:29 +09:00
src2md abstract, abstract_md, "latex"
2021-04-27 21:08:30 +09:00
sh "pandoc --listings -o #{abstract_tex} #{abstract_md}"
File.delete(abstract_md)
2021-03-07 11:36:51 +09:00
end
2022-04-17 12:52:12 +09:00
pair(srcfiles, texfiles).each do |src, dst, i|
file dst => [src] + src.c_files do
tex_md = "latex/#{src.to_md}"
src2md src, tex_md, "latex"
if src == "src/Readme_for_developers.src.md"
sh "pandoc -o #{dst} #{tex_md}"
2021-06-30 23:52:33 +09:00
else
2022-04-17 12:52:12 +09:00
sh "pandoc --listings -o #{dst} #{tex_md}"
2021-06-30 23:52:33 +09:00
end
2021-04-27 21:08:30 +09:00
File.delete(tex_md)
2021-01-11 11:15:04 +09:00
end
end
2020-12-21 21:12:05 +09:00
task :clean
2022-04-21 23:53:28 +09:00
task :clobber