Gtk4-tutorial/lib/lib_src_file.rb

169 lines
4.1 KiB
Ruby
Raw Normal View History

class Src_file <String
2020-12-21 13:12:05 +01:00
def initialize path
unless path.is_a?(String)
raise "Src_file class initialization error: The argument is not String type."
2020-12-21 13:12:05 +01:00
end
unless File.exist?(path)
raise "Src_file class initialization error: File #{path} is not exist."
end
unless path =~ /\.src\.md$/
raise "Src_file class initialization error: The argment \"#{path}\" doesn't have .src.md suffix."
end
@name = File.basename path, ".src.md"
@dirname = File.dirname path
super(path)
2020-12-21 13:12:05 +01:00
end
def replace path
unless path.is_a?(String)
raise "Replace error: The argument is not String type."
end
unless File.exist?(path)
raise "Replace error: File #{path} is not exist."
end
unless path =~ /\.src\.md$/
raise "Replace error: The argment \"#{path}\" doesn't have .src.md suffix."
end
super(path)
@name = File.basename path, ".src.md"
@dirname = File.dirname path
end
2020-12-21 13:12:05 +01:00
def path
self
end
def basename
@name+".src.md"
2020-12-21 13:12:05 +01:00
end
def dirname
@dirname
end
def to_md
@name+".md"
end
def to_html
@name+".html"
end
def to_tex
@name+".tex"
end
2020-12-21 13:12:05 +01:00
def c_files
buf = IO.readlines(self)
files = []
in_include = false
buf.each do |line|
if in_include
if line == "@@@\n"
in_include = false
else
files << @dirname+"/"+line.match(/^ *(\S*)/)[1]
2020-12-21 13:12:05 +01:00
end
elsif line == "@@@include\n"
in_include = true
else
# lines out of @@@include command is thrown away.
2020-12-21 13:12:05 +01:00
end
end
raise "Syntax error: @@@include didn't end (no @@@ line)." if in_include
files
2020-12-21 13:12:05 +01:00
end
end
class Sec_file < Src_file
def initialize path
unless path =~ /sec\d+(\.\d+)?\.src\.md$/
raise "Sec_file class initialization error: The argment \"#{path}\" doesn't have secXX.src.md form. XX is int or float."
end
super(path)
end
def num # the return value is String
@name.match(/\d+(\.\d+)?/)[0]
end
def to_f
self.num.to_f
2020-12-21 13:12:05 +01:00
end
def <=> other
if other.instance_of?(Sec_file)
self.to_f <=> other.to_f
2020-12-21 13:12:05 +01:00
else
nil
end
end
2020-12-22 03:30:06 +01:00
# Note: is_i? indicates the number is integer mathematically. For example, 2.0 is an integer.
# It doesn't mean the class of the number is Integer.
2020-12-21 13:12:05 +01:00
def is_i?
self.to_f == self.to_f.floor
2020-12-21 13:12:05 +01:00
end
def renum! n
2020-12-21 13:12:05 +01:00
if n.instance_of?(String)
n = n.to_i if n =~ /^\d+$/
n = n.to_f if n =~ /^\d+\.\d+/
end
if n.instance_of?(Integer) || n.instance_of?(Float)
2020-12-22 03:30:06 +01:00
n = n.to_i if n == n.floor
2020-12-21 13:12:05 +01:00
old = self
new = self.gsub(/\d+(\.\d+)?\.src\.md$/, "#{n}.src.md")
2020-12-22 03:30:06 +01:00
if old != new
File.rename old, new
self.replace new
end
2020-12-21 13:12:05 +01:00
end
end
end
class Sec_files < Array
def initialize sec_files
if sec_files.instance_of? Array
sec_files.each do |sec_file|
unless sec_file.instance_of? Sec_file
raise "#{sec_file} is not an instance of Sec_file."
end
end
super(sec_files)
else
raise "#{sec_files} is not an array."
end
end
def renum!
2020-12-21 13:12:05 +01:00
self.sort!
tbl = []
n = 1
2020-12-21 13:12:05 +01:00
self.each do |sec_file|
tbl << [ sec_file.num, n, sec_file.to_f == n ? true : false ]
n += 1
2020-12-21 13:12:05 +01:00
end
while any_diff?(tbl)
unless try_renum(tbl)
break
end
end
if any_diff?(tbl)
raise "Renumbering failed."
end
end
private
def any_diff? tbl
tbl.find_index { |row| row[2] == false }
2020-12-21 13:12:05 +01:00
end
def try_renum tbl
changed = false
(self.size - 1).downto 0 do |i|
if tbl[i][2] == false
n = tbl[i][1] # number to substitute
found = self.find_index { |sec_file| sec_file.to_f == n }
2020-12-21 13:12:05 +01:00
unless found # OK to replace
self[i].renum! n
2020-12-21 13:12:05 +01:00
tbl[i][2] = true
# tbl[0] (old number (String) is kept in the array 'tbl')
2020-12-21 13:12:05 +01:00
changed = true
self.each do |sec_file|
buf = IO.readlines sec_file
buf_n = buf.map { |line| line.gsub(/((S|s)ection *)#{tbl[i][0]}/, "\\1#{n}").gsub(/((S|s)ec *)#{tbl[i][0]}/, "\\1#{n}") }
IO.write sec_file, buf_n.join
end
2020-12-21 13:12:05 +01:00
end
end
end
changed
end
end