Class: IsoDoc::XrefGen::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/isodoc/xref/xref_counter.rb

Instance Method Summary collapse

Constructor Details

#initialize(num = 0, opts = { numerals: :arabic }) ⇒ Counter

Returns a new instance of Counter.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/isodoc/xref/xref_counter.rb', line 18

def initialize(num = 0, opts = { numerals: :arabic })
  @unnumbered = false
  @num = num
  @letter = ""
  @subseq = ""
  @letter_override = nil
  @number_override = nil
  @style = opts[:numerals]
  @skip_i = opts[:skip_i]
  @base = ""
  if num.is_a? String
    if /^\d+$/.match?(num)
      @num = num.to_i
    else
      @num = nil
      @base = num[0..-2]
      @letter = num[-1]
    end
  end
end

Instance Method Details

#blank?(str) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/isodoc/xref/xref_counter.rb', line 123

def blank?(str)
  str.nil? || str.empty?
end

#increment(node) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/isodoc/xref/xref_counter.rb', line 127

def increment(node)
  @unnumbered = (node["unnumbered"] || node["hidden"]) and return self

  @letter_override = nil
  @number_override = nil
  if node["subsequence"] != @subseq &&
      !(blank?(node["subsequence"]) && blank?(@subseq))
    new_subseq_increment(node)
  elsif @letter.empty? then sequence_increment(node)
  else subsequence_increment(node)
  end
  self
end

#increment_letterObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/isodoc/xref/xref_counter.rb', line 109

def increment_letter
  case @letter
  when "Z"
    @letter = "@"
    @base = string_inc(@base, "A")
  when "z"
    @letter = "`"
    @base = string_inc(@base, "a")
  end
  @letter = (@letter.ord + 1).chr.to_s
  @skip_i && %w(i I).include?(@letter) and
    @letter = (@letter.ord + 1).chr.to_s
end

#listlabel(list, depth) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/isodoc/xref/xref_counter.rb', line 160

def listlabel(list, depth)
  case ol_type(list, depth)
  when :arabic then @num.to_s
  when :alphabet then (96 + @num).chr.to_s
  when :alphabet_upper then (64 + @num).chr.to_s
  when :roman then RomanNumerals.to_roman(@num).downcase
  when :roman_upper then RomanNumerals.to_roman(@num).upcase
  end
end

#new_subseq_increment(node) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/isodoc/xref/xref_counter.rb', line 39

def new_subseq_increment(node)
  @subseq = node["subsequence"]
  @num += 1 unless @num.nil?
  @letter = node["subsequence"] ? "a" : ""
  @base = ""
  new_subseq_increment1(node) if node["number"]
end

#new_subseq_increment1(node) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/isodoc/xref/xref_counter.rb', line 47

def new_subseq_increment1(node)
  /^(?<b>.*?)(?<n>\d*)(?<a>[a-zA-Z]*)$/ =~ node["number"]
  if !n.empty? || !a.empty?
    @letter_override = @letter = a unless a.empty?
    @number_override = @num = n.to_i unless n.empty?
    @base = b
  else
    @letter_override = node["number"]
    @letter = @letter_override if /^[a-zA-Z]$/.match?(@letter_override)
  end
end

#ol_type(list, depth) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/isodoc/xref/xref_counter.rb', line 149

def ol_type(list, depth)
  return list["type"].to_sym if list["type"]
  return :arabic if [2, 7].include? depth
  return :alphabet if [1, 6].include? depth
  return :alphabet_upper if [4, 9].include? depth
  return :roman if [3, 8].include? depth
  return :roman_upper if [5, 10].include? depth

  :arabic
end


141
142
143
144
145
146
147
# File 'lib/isodoc/xref/xref_counter.rb', line 141

def print
  return nil if @unnumbered

  num = @number_override || @num
  out = @style == :roman && !num.nil? ? RomanNumerals.to_roman(num) : num
  "#{@base}#{out}#{@letter_override || @letter}"
end

#sequence_increment(node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/isodoc/xref/xref_counter.rb', line 59

def sequence_increment(node)
  if node["number"]
    @base = @letter_override = @number_override = ""
    /^(?<b>.*?)(?<n>\d+)$/ =~ node["number"]
    if blank?(n)
      @num = nil
      @base = node["number"][0..-2]
      @letter = @letter_override = node["number"][-1]
    else
      @number_override = @num = n.to_i
      @base = b
      @letter = ""
    end
  else @num += 1
  end
end

#string_inc(str, start) ⇒ Object



103
104
105
106
107
# File 'lib/isodoc/xref/xref_counter.rb', line 103

def string_inc(str, start)
  return start if str.empty?

  str[0..-2] + (str[-1].ord + 1).chr.to_s
end

#subsequence_increment(node) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/isodoc/xref/xref_counter.rb', line 76

def subsequence_increment(node)
  return increment_letter unless node["number"]

  @base = ""
  @letter_override = node["number"]
  /^(?<b>.*?)(?<n>\d*)(?<a>[a-zA-Z])$/ =~ node["number"]
  if blank?(a) then subsequence_increment_no_letter(node)
  else
    @letter_override = @letter = a
    @base = b
    @number_override = @num = n.to_i unless n.empty?
  end
end

#subsequence_increment_no_letter(node) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/isodoc/xref/xref_counter.rb', line 90

def subsequence_increment_no_letter(node)
  if /^\d+$/.match?(node["number"])
    @letter_override = @letter = ""
    @number_override = @num = node["number"].to_i
  else
    /^(?<b>.*)(?<a>[a-zA-Z])$/ =~ node["number"]
    unless blank?(a)
      @letter = @letter_override = a
      @base = b
    end
  end
end