Class: NanDoc::StreamColorizer::State

Inherits:
Object
  • Object
show all
Includes:
NanDoc::SecretParent, RuleList, Treebis::Colorize
Defined in:
lib/nandoc/support/stream-colorizer.rb

Instance Attribute Summary collapse

Attributes included from RuleList

#rule_list, #state_set

Instance Method Summary collapse

Methods included from RuleList

#add_regex_rule, #add_regex_rule_neg, #define_state, #get_state_or_fail, #rule_list_init, #when, #when_not

Methods included from NanDoc::SecretParent

#parent, #parent=, #parent?

Constructor Details

#initialize(parent, name, &block) ⇒ State

Returns a new instance of State.



115
116
117
118
119
120
121
122
123
# File 'lib/nandoc/support/stream-colorizer.rb', line 115

def initialize parent, name, &block
  self.parent = parent
  rule_list_init
  fail('no') unless Symbol === name
  @name = name
  @style = nil
  @trailing_whitespace_style = nil
  block.call(self)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



124
125
126
# File 'lib/nandoc/support/stream-colorizer.rb', line 124

def name
  @name
end

Instance Method Details

#colorsObject



174
175
176
177
178
179
180
181
182
# File 'lib/nandoc/support/stream-colorizer.rb', line 174

def colors
  @colors ||= begin
    if style.nil?
      []
    else
      parent.stylesheet[style] or style_not_found_failure
    end
  end
end

#next_line(str, alter = false) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/nandoc/support/stream-colorizer.rb', line 125

def next_line str, alter=false
  res = false
  if str == ''
    nil
  elsif alter
    if /\A([^\n]+)(?:\n?)(.*)\Z/m =~ str
      res = $1
      str.replace($2)
    else
      fail("fail: #{str.inspect}")
    end
  else
    if /\A([^\n]+)/ =~ str
      res = $1
    else
      fail("fail: #{str.inspect}")
    end
  end
  res
end

#process(string, out) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/nandoc/support/stream-colorizer.rb', line 145

def process string, out
  ret = nil
  while line = next_line(string)
    if other = rule_list.detect{|x| x.match(string)}
      next_state_name = other.state
      ret = next_state_name
      break
    else
      next_line(string, true) # alter string
      use_line = nil
      if @trailing_whitespace_style
        /\A(|.*[^[:space:]])([[:space:]]*)\Z/ =~ line or fail('oops')
        head, tail = $1, $2
        colored_head = colorize(head, *colors)
        use_line = colored_head.dup
        unless tail.empty?
          ws_style = parent.stylesheet[@trailing_whitespace_style] or
            style_not_found_failure('@trailing_whitespace_style')
          colored_tail = colorize(tail, *ws_style)
          use_line.concat colored_tail
        end
      else
        use_line = colorize(line, *colors)
      end
      out.puts use_line
    end
  end
  ret
end

#style(*a) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/nandoc/support/stream-colorizer.rb', line 183

def style *a
  case a.size
  when 0; @style
  when 1; @style = a.first
  else fail('no')
  end
end

#style_not_found_failure(which = '@style') ⇒ Object



197
198
199
200
# File 'lib/nandoc/support/stream-colorizer.rb', line 197

def style_not_found_failure which = '@style'
  value = instance_variable_get(which)
  fail("#{which} not found: #{value.inspect}")
end

#trailing_whitespace_style(*a) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/nandoc/support/stream-colorizer.rb', line 190

def trailing_whitespace_style *a
  case a.size
  when 0; @trailing_whitespace_style
  when 1; @trailing_whitespace_style = a.first
  else fail('no')
  end
end