2020-12-21 13:12:05 +01:00
|
|
|
class Sec_file < String
|
|
|
|
def initialize path
|
2021-02-08 14:24:54 +01:00
|
|
|
unless path.instance_of?(String)
|
|
|
|
raise "Sec_file class initialization error: The argument is not String type."
|
2020-12-21 13:12:05 +01:00
|
|
|
end
|
2021-02-08 14:24:54 +01:00
|
|
|
unless File.exist?(path)
|
|
|
|
raise "Sec_file class initialization error: File #{path} is not exist."
|
|
|
|
end
|
|
|
|
unless path =~ /sec\d+(\.\d+)?\.src\.md$/
|
|
|
|
raise "Sec_file class initialization error: The argment \"#{path}\" doesn't have secXX.src.md form. XX are digits."
|
|
|
|
end
|
|
|
|
@name = File.basename path
|
|
|
|
@dirname = File.dirname path
|
|
|
|
super(path)
|
2020-12-21 13:12:05 +01:00
|
|
|
end
|
|
|
|
def path
|
|
|
|
self
|
|
|
|
end
|
2021-02-08 14:24:54 +01:00
|
|
|
def basename
|
2020-12-21 13:12:05 +01:00
|
|
|
@name
|
|
|
|
end
|
|
|
|
def dirname
|
|
|
|
@dirname
|
|
|
|
end
|
|
|
|
def c_files
|
2021-02-08 14:24:54 +01:00
|
|
|
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
|
2021-02-08 14:24:54 +01:00
|
|
|
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
|
2021-02-08 14:24:54 +01:00
|
|
|
files
|
2020-12-21 13:12:05 +01:00
|
|
|
end
|
|
|
|
def to_md
|
|
|
|
@name.gsub(/\.(src\.md|md|html|tex)$/, ".md")
|
|
|
|
end
|
|
|
|
def to_html
|
|
|
|
@name.gsub(/\.(src\.md|md|html|tex)$/, ".html")
|
|
|
|
end
|
|
|
|
def to_tex
|
|
|
|
@name.gsub(/\.(src\.md|md|html|tex)$/, ".tex")
|
|
|
|
end
|
2021-01-11 15:32:09 +01:00
|
|
|
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)
|
2021-01-11 15:32:09 +01:00
|
|
|
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?
|
2021-01-11 15:32:09 +01:00
|
|
|
self.to_f == self.to_f.floor
|
2020-12-21 13:12:05 +01:00
|
|
|
end
|
2021-01-13 09:01:35 +01:00
|
|
|
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
|
2021-02-08 14:24:54 +01:00
|
|
|
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
|
2021-02-08 14:24:54 +01:00
|
|
|
@name = File.basename new
|
|
|
|
@dirname = File.dirname new
|
2020-12-22 03:30:06 +01:00
|
|
|
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
|
2021-01-13 09:01:35 +01:00
|
|
|
def renum!
|
2020-12-21 13:12:05 +01:00
|
|
|
self.sort!
|
|
|
|
tbl = []
|
2021-01-11 15:32:09 +01:00
|
|
|
n = 1
|
2020-12-21 13:12:05 +01:00
|
|
|
self.each do |sec_file|
|
2021-01-11 15:32:09 +01:00
|
|
|
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
|
2021-02-08 14:24:54 +01:00
|
|
|
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
|
2021-02-08 14:24:54 +01:00
|
|
|
found = self.find_index { |sec_file| sec_file != self && sec_file.to_f == n }
|
2020-12-21 13:12:05 +01:00
|
|
|
unless found # OK to replace
|
2021-01-13 09:01:35 +01:00
|
|
|
self[i].renum! n
|
2020-12-21 13:12:05 +01:00
|
|
|
tbl[i][2] = true
|
2021-01-11 15:32:09 +01:00
|
|
|
# tbl[0] (old number (String) is kept in the array 'tbl')
|
2020-12-21 13:12:05 +01:00
|
|
|
changed = true
|
2021-01-11 15:32:09 +01:00
|
|
|
self.each do |sec_file|
|
|
|
|
buf = IO.readlines sec_file
|
2021-02-08 14:24:54 +01:00
|
|
|
buf_n = buf.map { |line| line.gsub(/((S|s)ection *)#{tbl[i][0]}/, "\\1#{n}").gsub(/((S|s)ec *)#{tbl[i][0]}/, "\\1#{n}") }
|
2021-01-13 05:29:35 +01:00
|
|
|
IO.write sec_file, buf_n.join
|
2021-01-11 15:32:09 +01:00
|
|
|
end
|
2020-12-21 13:12:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
changed
|
|
|
|
end
|
|
|
|
end
|