Class: IsoDoc::XrefGen::Counter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Counter.



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

def initialize(num = 0, opts = { numerals: :arabic })
  @unnumbered = false
  @num = num
  @letter = ""
  @subseq = ""
  reset_overrides
  @style = opts[:numerals]
  @skip_i = opts[:skip_i]
  @prefix = opts[:prefix]
  @separator = opts[:separator] || "."
  @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 Attribute Details

#prefix_overrideObject

Returns the value of attribute prefix_override.



19
20
21
# File 'lib/isodoc/xref/xref_counter.rb', line 19

def prefix_override
  @prefix_override
end

Instance Method Details

#blank?(str) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/isodoc/xref/xref_counter.rb', line 139

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

#clock_letterObject



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

def clock_letter
  case @letter
  when "Z"
    @letter = "@"
    @base = string_inc(@base, "A")
  when "z"
    @letter = "`"
    @base = string_inc(@base, "a")
  end
end

#increment(node) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/isodoc/xref/xref_counter.rb', line 143

def increment(node)
  @unnumbered = node["unnumbered"] == "true" ||
    node["hidden"] == "true" and return self
  reset_overrides
  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



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

def increment_letter
  clock_letter
  @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



186
187
188
189
190
191
192
193
194
195
# File 'lib/isodoc/xref/xref_counter.rb', line 186

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
  when :japanese then num.localize(:ja).spellout
  end
end

#new_subseq_increment(node) ⇒ Object



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

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



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/isodoc/xref/xref_counter.rb', line 57

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



175
176
177
178
179
180
181
182
183
184
# File 'lib/isodoc/xref/xref_counter.rb', line 175

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


165
166
167
168
169
170
171
172
173
# File 'lib/isodoc/xref/xref_counter.rb', line 165

def print
  @unnumbered and return nil
  @prefix_override and return @prefix_override
  num = @number_override || @num
  out = style_number(num)
  prefix = @prefix
  prefix &&= "#{prefix}#{@separator}"
  "#{prefix}#{@base}#{out}#{@letter_override || @letter}"
end

#reset_overridesObject



43
44
45
46
47
# File 'lib/isodoc/xref/xref_counter.rb', line 43

def reset_overrides
  @letter_override = nil
  @number_override = nil
  @prefix_override = nil
end

#sequence_increment(node) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/isodoc/xref/xref_counter.rb', line 69

def sequence_increment(node)
  if node["branch-number"]
    @prefix_override = node["branch-number"]
  elsif 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



115
116
117
118
119
# File 'lib/isodoc/xref/xref_counter.rb', line 115

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

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

#style_number(num) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/isodoc/xref/xref_counter.rb', line 156

def style_number(num)
  num.nil? and return num
  case @style
  when :roman then RomanNumerals.to_roman(num)
  when :japanese then num.localize(:ja).spellout
  else num
  end
end

#subsequence_increment(node) ⇒ Object



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

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



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/isodoc/xref/xref_counter.rb', line 102

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