2020-12-21 21:12:05 +09:00
require 'rake/clean'
2022-04-16 15:46:29 +09:00
require 'fileutils'
2022-04-24 12:13:40 +09:00
require_relative 'lib/lib_renumber.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-25 10:33:47 +09:00
require_relative 'lib/lib_change_relative_link.rb'
2020-12-21 21:12:05 +09:00
2022-04-25 10:33:47 +09:00
def basename(file, suffix=nil)
suffix ? File.basename(file, suffix) : File.basename(file)
2022-04-24 12:13:40 +09:00
end
2022-04-25 10:33:47 +09:00
def dirname(file)
File.dirname(file)
2022-04-24 12:13:40 +09:00
end
2022-04-25 10:33:47 +09:00
# Get dstination file(s) from source file(s).
def mdst src
"gfm/#{basename(src, ".src.md")}.md"
2022-04-24 12:13:40 +09:00
end
2022-04-25 10:33:47 +09:00
def hdst src
"docs/#{basename(src, ".src.md")}.html"
2022-04-24 12:13:40 +09:00
end
2022-04-25 10:33:47 +09:00
def tdst src
"latex/#{basename(src, ".src.md")}.tex"
end
def idst src
"docs/image/#{basename(src)}"
end
def mdsts srcs
srcs.map{|src| mdst(src)}
end
def hdsts srcs
srcs.map{|src| hdst(src)}
end
def tdsts srcs
srcs.map{|src| tdst(src)}
end
def idsts(srcs)
srcs.map{|src| idst(src)}
end
def to_md file
basename(file).sub(/\..*$/,'.md')
end
def to_html file
basename(file).sub(/\..*$/,'.html')
end
# get C file names in the .src.md file.
2022-04-24 12:13:40 +09:00
def c_files path
2022-04-25 10:33:47 +09:00
dir = dirname(path)
2022-04-24 12:13:40 +09: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
2022-04-25 10:33:47 +09:00
def is_secfile?(file)
file =~ /sec\d+\.src\.md$/
end
2022-04-24 12:13:40 +09:00
# source files
secfiles = FileList['src/sec*.src.md']
renumber(secfiles)
2021-04-27 21:08:30 +09:00
otherfiles = ["src/turtle/turtle_doc.src.md",
"src/tfetextview/tfetextview_doc.src.md",
2022-04-24 12:13:40 +09:00
"src/Readme_for_developers.src.md"]
2021-04-27 21:08:30 +09:00
srcfiles = secfiles + otherfiles
2022-04-24 12:13:40 +09:00
abstract = "src/abstract.src.md"
# imagesrcfiles are the image files used in the srcfiles.
# They are absolute paths.
2022-04-25 10:33:47 +09:00
imagefiles = srcfiles.map do |file|
d = dirname(file)
2022-04-24 12:13:40 +09:00
File.read(file)\
2022-04-25 10:33:47 +09:00
.gsub(/^~~~.*?^~~~\n/m,'').gsub(/^ .*\n/,'')\
.scan(/!\[.*?\]\((.*?)\)/).flatten.sort.uniq\
2022-04-24 12:13:40 +09:00
.map{|img| File.absolute_path("#{d}/#{img}")}
2022-04-25 10:33:47 +09:00
end.flatten.sort.uniq
2020-12-21 21:12:05 +09:00
2022-04-25 10:33:47 +09:00
["gfm", "docs", "latex"].each{|d| Dir.mkdir(d) unless Dir.exist?(d)}
2022-06-06 21:38:52 +09:00
Dir.mkdir('docs/image') unless Dir.exist?('docs/image')
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"])
2022-04-25 10:33:47 +09:00
CLOBBER.append("Readme.md").append(FileList["gfm/*.md"])
2022-04-21 23:53:28 +09:00
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
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
2022-04-25 10:33:47 +09:00
task md: %w[Readme.md] + mdsts(srcfiles)
2021-01-09 15:35:06 +09:00
2021-04-27 21:08:30 +09:00
file "Readme.md" => [abstract] + secfiles do
2022-04-25 10:33:47 +09:00
abstract_md = mdst(abstract)
src2md(abstract, "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-24 12:13:40 +09:00
+ File.readlines(abstract_md)\
2022-04-21 23:53:28 +09:00
+ ["\n## Table of contents\n\n"]
2022-04-24 12:13:40 +09:00
File.delete(abstract_md)
2022-04-16 15:46:29 +09:00
secfiles.each_with_index do |secfile, i|
2022-04-24 12:13:40 +09:00
h = File.open(secfile){|file| file.readline}.sub(/^#* */,"").chomp
2022-04-25 10:33:47 +09:00
buf << "1. [#{h}](#{to_md(secfile)})\n"
2021-01-09 15:35:06 +09:00
end
2022-04-25 10:33:47 +09:00
readme_md = buf.join
readme_md = change_relative_link(readme_md, "gfm", ".")
File.write("Readme.md", readme_md)
2021-01-09 15:35:06 +09:00
end
2020-12-21 21:12:05 +09:00
2022-04-16 15:46:29 +09:00
# srcfiles => mdfiles
2022-04-25 10:33:47 +09:00
srcfiles.each_with_index do |src, i|
dst = mdst(src)
2022-04-24 12:13:40 +09:00
file dst => [src] + c_files(src) do
2022-04-25 10:33:47 +09:00
src2md(src, "gfm")
if is_secfile?(src)
2021-04-27 21:08:30 +09:00
if secfiles.size == 1
nav = "Up: [Readme.md](../Readme.md)\n"
elsif i == 0
2022-04-25 10:33:47 +09:00
nav = "Up: [Readme.md](../Readme.md), Next: [Section 2](sec2.md)\n"
2021-04-27 21:08:30 +09:00
elsif i == secfiles.size - 1
2022-04-25 10:33:47 +09:00
nav = "Up: [Readme.md](../Readme.md), Prev: [Section #{i}](sec#{i}.md)\n"
2021-04-27 21:08:30 +09:00
else
2022-04-25 10:33:47 +09:00
nav = "Up: [Readme.md](../Readme.md), Prev: [Section #{i}](sec#{i}.md), Next: [Section #{i+2}](sec#{i+2}.md)\n"
2021-04-27 21:08:30 +09:00
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-08-26 10:08:09 +09:00
task html: %W[docs/index.html docs/.nojekyll] + hdsts(srcfiles) + idsts(imagefiles)
2021-01-09 15:35:06 +09:00
2022-04-25 10:33:47 +09:00
file "docs/index.html" => [abstract] + secfiles do
abstract_md = "docs/#{to_md(abstract)}"
src2md(abstract, "html")
2022-04-21 23:53:28 +09:00
buf = [ "# Gtk4 Tutorial for beginners\n\n" ]\
2022-04-25 10:33:47 +09:00
+ File.readlines(abstract_md)\
2022-04-21 23:53:28 +09:00
+ ["\n## Table of contents\n\n"]
2022-04-25 10:33:47 +09:00
File.delete(abstract_md)
2022-04-16 15:46:29 +09:00
secfiles.each_with_index do |secfile, i|
2022-04-24 12:13:40 +09:00
h = File.open(secfile){|file| file.readline}.sub(/^#* */,"").chomp
2022-04-25 10:33:47 +09:00
buf << "1. [#{h}](#{to_html(secfile)})\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-25 10:33:47 +09:00
File.write("docs/index.md", buf.join)
2022-04-21 23:53:28 +09:00
mk_html_template(nil, nil, nil)
2022-04-25 10:33:47 +09: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 23:53:28 +09:00
File.delete "docs/template.html"
2021-01-09 15:35:06 +09:00
end
2022-08-26 10:08:09 +09:00
file "docs/.nojekyll" do |t|
touch t.name
end
2022-04-25 10:33:47 +09:00
srcfiles.each_with_index do |src, i|
dst = hdst(src)
2022-04-24 12:13:40 +09:00
file dst => [src] + c_files(src) do
2022-04-25 10:33:47 +09:00
html_md = "docs/#{to_md(src)}"
src2md(src, "html")
if is_secfile?(src)
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-25 10:33:47 +09:00
imagefiles.each do |src|
dst = idst(src)
2022-04-24 12:13:40 +09:00
file dst => src do
cp src, dst
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-25 10:33:47 +09:00
file "latex/main.tex" => [tdst(abstract)] + tdsts(srcfiles) do
gen_main_tex "latex", tdst(abstract), tdsts(secfiles), tdsts(otherfiles)
2021-02-09 18:16:52 +09:00
end
2022-04-25 10:33:47 +09:00
file tdst(abstract) => abstract do
abstract_md = "latex/#{to_md(abstract)}"
src2md(abstract, "latex")
sh "pandoc --listings -o #{tdst(abstract)} #{abstract_md}"
File.delete(abstract_md)
2021-03-07 11:36:51 +09:00
end
2022-04-25 10:33:47 +09:00
srcfiles.each do |src|
dst = tdst(src)
2022-04-24 12:13:40 +09:00
file dst => [src] + c_files(src) do
2022-04-25 10:33:47 +09:00
tex_md = "latex/#{to_md(src)}"
src2md(src, "latex")
2022-04-17 12:52:12 +09:00
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
2022-04-25 10:33:47 +09: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