class Lbt::PCS

When you create an instance of the PCS class, it keeps the structure of Part-Chapter-Section files. For example,

+-part1-+-chap1-+-sec1.tex
|       |       +-sec2.tex`
|       +-chap2-+-sec1.tex`
|       |       +-sec2.tex`
+-part2-+-chap1-+-sec1.tex
        |       +-sec2.tex`

If the files are as above, the PCS instance keeps the six filenames whch are sorted alphabetically.

Attributes

dir[R]

The name of the top direcotory

type[R]

Type is one of :PCS, :CS and :S

  • :PCS => The file structure has part, chap directories.

  • :CS => The file structure has chap directories.

  • :S => The file structure doesn’t have any directories.

Public Class Methods

new(dir=".") click to toggle source

Create a new PCS instance. You can give a top directory as an argument.

# File lib/lbt/utils.rb, line 44
def initialize dir="."
  @dir = dir.dup
  @dir.freeze
  @files = Dir.glob("part*/chap*/sec*", base: @dir).select{|f| f.match?(/^part\d+(\.\d+)?\/chap\d+(\.\d+)?\/sec\d+(\.\d+)?\.\w+$/)}
  unless @files.empty?
    @type = :PCS
    @files.sort! do |a, b|
      m = /^part(\d+(\.\d+)?)\/chap(\d+(\.\d+)?)\/sec(\d+(\.\d+)?)\.\w*$/.match(a)
      ap = m[1].to_f; ac= m[3].to_f; as = m[5].to_f
      m = /^part(\d+(\.\d+)?)\/chap(\d+(\.\d+)?)\/sec(\d+(\.\d+)?)\.\w*$/.match(b)
      bp = m[1].to_f; bc= m[3].to_f; bs = m[5].to_f
      if ap != bp
        ap <=> bp
      elsif ac != bc
        ac <=> bc
      else
        as <=> bs
      end
    end
    return
  end
  @files = Dir.glob("chap*/sec*", base: @dir).select{|f| f.match?(/^chap\d+(\.\d+)?\/sec\d+(\.\d+)?\.\w+$/)}
  unless @files.empty?
    @type = :CS
    @files.sort! do |a, b|
      m = /^chap(\d+(\.\d+)?)\/sec(\d+(\.\d+)?)\.\w*$/.match(a)
      ac= m[1].to_f; as = m[3].to_f
      m = /^chap(\d+(\.\d+)?)\/sec(\d+(\.\d+)?)\.\w*$/.match(b)
      bc= m[1].to_f; bs = m[3].to_f
      if ac != bc
        ac <=> bc
      else
        as <=> bs
      end
    end
    return
  end
  @files = Dir.glob("sec*", base: @dir).select{|f| f.match?(/^sec\d+(\.\d+)?\.\w+$/)}
  unless @files.empty?
    @type = :S
    @files.sort! do |a, b|
      m = /^sec(\d+(\.\d+)?)\.\w*$/.match(a)
      as = m[1].to_f
      m = /^sec(\d+(\.\d+)?)\.\w*$/.match(b)
      bs = m[1].to_f
      as <=> bs
    end
    return
  end
  raise "No [[part/]chap/]sec files exist.\n" if @files.empty?
end

Public Instance Methods

each() { |f| ... } click to toggle source

Executes the block with each pathname.

# File lib/lbt/utils.rb, line 103
def each
  @files.each {|f| yield(f)}
end
to_a() click to toggle source

Return an array of pathnames which are relative from the top directory. The returned pathnames are the copies. So, even if a user changes them, the original pathnames in the PCS instance are not changed.

# File lib/lbt/utils.rb, line 99
def to_a
  @files.map {|s| s.dup}
end