2020-12-21 13:12:05 +01:00
require 'rake/clean'
2022-04-24 05:13:40 +02:00
require_relative 'lib/lib_renumber.rb'
2020-12-21 13:12:05 +01:00
require_relative 'lib/lib_src2md.rb'
2021-02-06 15:50:02 +01:00
require_relative 'lib/lib_gen_main_tex.rb'
2022-04-21 16:53:28 +02:00
require_relative 'lib/lib_mk_html_template.rb'
2022-04-25 03:33:47 +02:00
require_relative 'lib/lib_change_relative_link.rb'
2020-12-21 13:12:05 +01:00
2022-08-27 14:43:28 +02:00
# 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
2022-04-25 03:33:47 +02:00
end
2022-08-27 14:43:28 +02:00
class FileList
def ext suffix
self.map{|fl| fl.ext(suffix)}
end
2022-04-25 03:33:47 +02:00
end
2022-08-27 14:43:28 +02:00
# 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
2022-04-25 03:33:47 +02:00
end
2022-08-27 14:43:28 +02:00
2022-04-25 03:33:47 +02:00
# get C file names in the .src.md file.
2022-04-24 05:13:40 +02:00
def c_files path
2022-08-27 14:43:28 +02:00
dir = path.pathmap("%d")
2022-04-24 05:13:40 +02:00
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)
2022-08-27 14:43:28 +02:00
# Some files may be renamed. Redefine sectiles.
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["src/turtle/turtle_doc.src.md",
2021-04-27 14:08:30 +02:00
"src/tfetextview/tfetextview_doc.src.md",
2022-04-24 05:13:40 +02:00
"src/Readme_for_developers.src.md"]
2021-04-27 14:08:30 +02:00
srcfiles = secfiles + otherfiles
2022-04-24 05:13:40 +02:00
abstract = "src/abstract.src.md"
# imagesrcfiles are the image files used in the srcfiles.
# They are absolute paths.
2022-04-25 03:33:47 +02:00
imagefiles = srcfiles.map do |file|
2022-08-27 14:43:28 +02:00
d = file.pathmap("%d")
2022-04-24 05:13:40 +02:00
File.read(file)\
2022-04-25 03:33:47 +02:00
.gsub(/^~~~.*?^~~~\n/m,'').gsub(/^ .*\n/,'')\
.scan(/!\[.*?\]\((.*?)\)/).flatten.sort.uniq\
2022-04-24 05:13:40 +02:00
.map{|img| File.absolute_path("#{d}/#{img}")}
2022-04-25 03:33:47 +02:00
end.flatten.sort.uniq
2022-08-27 14:43:28 +02:00
imagefiles = FileList[*imagefiles]
2021-01-11 03:15:04 +01:00
2022-04-21 16:53:28 +02:00
CLEAN.append(FileList["latex/*.tex", "latex/*.aux", "latex/*.log", "latex/*.toc"])
2022-04-25 03:33:47 +02:00
CLOBBER.append("Readme.md").append(FileList["gfm/*.md"])
2022-04-21 16:53:28 +02:00
CLOBBER.append(FileList["docs/*.html"])
2022-04-22 05:04:27 +02:00
CLOBBER.append(FileList["docs/image/*"])
2022-04-21 16:53:28 +02:00
CLOBBER.append(FileList["latex/*.pdf"])
2020-12-21 13:12:05 +01:00
2021-01-09 07:35:06 +01:00
# tasks
2020-12-21 13:12:05 +01:00
task default: :md
2021-01-11 15:32:09 +01:00
task all: [:md, :html, :pdf]
2020-12-21 13:12:05 +01:00
2022-08-27 14:43:28 +02:00
mdfiles = srcfiles.pathmap("%f").ext(".md").map{|f| "gfm/#{f}"}
task md: %w[Readme.md] + mdfiles
2021-01-09 07:35:06 +01:00
2022-08-27 14:43:28 +02:00
file "Readme.md" => [abstract] + secfiles do |t|
abstract_md = "gfm/"+abstract.pathmap("%f").ext(".md")
2022-04-25 03:33:47 +02:00
src2md(abstract, "gfm")
2022-04-21 16:53:28 +02: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-24 05:13:40 +02:00
+ File.readlines(abstract_md)\
2022-04-21 16:53:28 +02:00
+ ["\n## Table of contents\n\n"]
2022-04-24 05:13:40 +02:00
File.delete(abstract_md)
2022-08-27 14:43:28 +02:00
secfiles.each do |secfile|
2022-04-24 05:13:40 +02:00
h = File.open(secfile){|file| file.readline}.sub(/^#* */,"").chomp
2022-08-27 14:43:28 +02:00
buf << "1. [#{h}](#{secfile.ext(".md")})\n"
2021-01-09 07:35:06 +01:00
end
2022-04-25 03:33:47 +02:00
readme_md = buf.join
readme_md = change_relative_link(readme_md, "gfm", ".")
2022-08-27 14:43:28 +02:00
File.write(t.name, readme_md)
2021-01-09 07:35:06 +01:00
end
2020-12-21 13:12:05 +01:00
2022-04-16 08:46:29 +02:00
# srcfiles => mdfiles
2022-08-27 14:43:28 +02:00
srcfiles.each do |src|
dst = "gfm/"+src.pathmap("%f").ext(".md")
file dst => [src] + c_files(src) + ["gfm"] do |t|
src2md(t.source, "gfm")
if secfiles.include?(t.source)
i = get_sec_num(src)
2021-04-27 14:08:30 +02:00
if secfiles.size == 1
nav = "Up: [Readme.md](../Readme.md)\n"
2022-08-27 14:43:28 +02:00
elsif i == 1
2022-04-25 03:33:47 +02:00
nav = "Up: [Readme.md](../Readme.md), Next: [Section 2](sec2.md)\n"
2022-08-27 14:43:28 +02:00
elsif i == secfiles.size
nav = "Up: [Readme.md](../Readme.md), Prev: [Section #{i-1}](sec#{i-1}.md)\n"
2021-04-27 14:08:30 +02:00
else
2022-08-27 14:43:28 +02:00
nav = "Up: [Readme.md](../Readme.md), Prev: [Section #{i-1}](sec#{i-1}.md), Next: [Section #{i+1}](sec#{i+1}.md)\n"
2021-04-27 14:08:30 +02:00
end
2022-08-27 14:43:28 +02:00
File.write(t.name, nav + "\n" + File.read(t.name) + "\n" + nav)
2020-12-21 13:12:05 +01:00
end
end
end
2022-08-27 14:43:28 +02:00
htmlfiles = srcfiles.pathmap("%f").ext(".html").map{|f| "docs/#{f}"}
htmlimagefiles = imagefiles.pathmap("%f").map{|f| "docs/image/#{f}"}
2021-01-09 07:35:06 +01:00
2022-08-27 14:43:28 +02:00
task html: %W[docs/index.html docs/.nojekyll] + htmlfiles + htmlimagefiles
file "docs/index.html" => [abstract, "docs"] + secfiles do
abstract_md = "docs/"+abstract.pathmap("%f").ext(".md")
src2md(abstract, "html")
2022-04-21 16:53:28 +02:00
buf = [ "# Gtk4 Tutorial for beginners\n\n" ]\
2022-04-25 03:33:47 +02:00
+ File.readlines(abstract_md)\
2022-04-21 16:53:28 +02:00
+ ["\n## Table of contents\n\n"]
2022-04-25 03:33:47 +02:00
File.delete(abstract_md)
2022-08-27 14:43:28 +02:00
secfiles.each do |secfile|
2022-04-24 05:13:40 +02:00
h = File.open(secfile){|file| file.readline}.sub(/^#* */,"").chomp
2022-08-27 14:43:28 +02:00
buf << "1. [#{h}](#{secfile.pathmap('%f').ext('.html')})\n"
2021-01-09 07:35:06 +01:00
end
2022-04-21 16:53:28 +02:00
buf << "\nThis website uses [Bootstrap](https://getbootstrap.jp/)."
2022-04-25 03:33:47 +02:00
File.write("docs/index.md", buf.join)
2022-04-21 16:53:28 +02:00
mk_html_template(nil, nil, nil)
2022-04-25 03:33:47 +02:00
sh "pandoc -s --template=docs/template.html --metadata=title:\"Gtk4 tutorial\" -o docs/index.html docs/index.md"
File.delete "docs/index.md"
2022-04-21 16:53:28 +02:00
File.delete "docs/template.html"
2021-01-09 07:35:06 +01:00
end
2022-08-27 14:43:28 +02:00
file "docs/.nojekyll" => "docs" do |t|
2022-08-26 03:08:09 +02:00
touch t.name
end
2022-08-27 14:43:28 +02:00
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")
2022-04-25 03:33:47 +02:00
src2md(src, "html")
2022-08-27 14:43:28 +02:00
if secfiles.include?(t.source)
i = get_sec_num(src)
2021-04-27 14:08:30 +02:00
if secfiles.size == 1
2022-04-21 16:53:28 +02:00
mk_html_template("index.html", nil, nil)
2022-08-27 14:43:28 +02:00
elsif i == 1
2022-04-21 16:53:28 +02:00
mk_html_template("index.html", nil, "sec2.html")
2022-08-27 14:43:28 +02:00
elsif i == secfiles.size
mk_html_template("index.html", "sec#{i-1}.html", nil)
2021-04-27 14:08:30 +02:00
else
2022-08-27 14:43:28 +02:00
mk_html_template("index.html", "sec#{i-1}.html", "sec#{i+1}.html")
2021-04-27 14:08:30 +02:00
end
2022-04-21 16:53:28 +02:00
else
mk_html_template("index.html", nil, nil)
2021-01-09 07:35:06 +01:00
end
2022-08-27 14:43:28 +02:00
sh "pandoc -s --template=docs/template.html --metadata=title:\"Gtk4 tutorial\" -o #{t.name} #{html_md}"
2021-02-06 15:50:02 +01:00
File.delete(html_md)
2022-04-21 16:53:28 +02:00
File.delete "docs/template.html"
2020-12-21 13:12:05 +01:00
end
end
2022-04-25 03:33:47 +02:00
imagefiles.each do |src|
2022-08-27 14:43:28 +02:00
dst = "docs/image/"+src.pathmap("%f")
file dst => [src, "docs/image"] do
2022-04-24 05:13:40 +02:00
cp src, dst
end
end
2022-04-16 08:46:29 +02:00
task pdf: %w[latex/main.tex] do
2021-03-03 15:45:54 +01:00
sh "cd latex; lualatex main.tex"
sh "cd latex; lualatex main.tex"
2021-01-11 03:15:04 +01:00
sh "mv latex/main.pdf latex/gtk4_tutorial.pdf"
end
2022-08-27 14:43:28 +02:00
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
2021-02-09 10:16:52 +01:00
end
2022-08-27 14:43:28 +02:00
file abstract_tex => abstract do |t|
abstract_md = "latex/"+abstract.pathmap("%f").ext(".md")
2022-04-25 03:33:47 +02:00
src2md(abstract, "latex")
2022-08-27 14:43:28 +02:00
sh "pandoc --listings -o #{t.name} #{abstract_md}"
2022-04-25 03:33:47 +02:00
File.delete(abstract_md)
2021-03-07 03:36:51 +01:00
end
2022-04-25 03:33:47 +02:00
srcfiles.each do |src|
2022-08-27 14:43:28 +02:00
dst = "latex/"+src.pathmap("%f").ext(".tex")
file dst => [src] + c_files(src) do |t|
tex_md = "latex/"+src.pathmap("%f").ext(".md")
2022-04-25 03:33:47 +02:00
src2md(src, "latex")
2022-04-17 05:52:12 +02:00
if src == "src/Readme_for_developers.src.md"
2022-08-27 14:43:28 +02:00
sh "pandoc -o #{t.name} #{tex_md}"
2021-06-30 16:52:33 +02:00
else
2022-08-27 14:43:28 +02:00
sh "pandoc --listings -o #{t.name} #{tex_md}"
2021-06-30 16:52:33 +02:00
end
2021-04-27 14:08:30 +02:00
File.delete(tex_md)
2021-01-11 03:15:04 +01:00
end
end
2022-08-27 14:43:28 +02:00
["gfm", "docs", "docs/image", "latex"].each do |d|
directory d
end
2020-12-21 13:12:05 +01:00
task :clean
2022-04-21 16:53:28 +02:00
task :clobber
2022-04-25 03:33:47 +02:00
task :cleangfm do
remove_entry_secure("gfm")
end
task :cleanhtml do
remove_entry_secure("docs")
end
task :cleanlatex do
remove_entry_secure("latex")
end