Class: Canis::Chunks::ColorParser

Inherits:
Object
  • Object
show all
Defined in:
lib/canis/core/include/colorparser.rb

Overview

This class does not do the actual parsing which must be done by a class

based on the actual format of the marked up document. 
However, this class converts the parsed fragments to a Chunkline
which is an array of Chunks,

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cp) ⇒ ColorParser

Returns a new instance of ColorParser.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/canis/core/include/colorparser.rb', line 223

def initialize cp
  # in some cases like statusline where it requests window to do some parsing, we will never know who
  #  the parent is. We could somehow get the window, and from there the form ???
  @parents = nil

  chunk_parser cp

  if cp.is_a? Hash
    @color = cp[:color]
    @bgcolor = cp[:bgcolor]
    @attr = cp[:attr]
  end
  if cp.is_a? TextPad
    self.form = cp
  end
  @attr     ||= FFI::NCurses::A_NORMAL
  @color      ||= :white
  @bgcolor    ||= :black
  @color_pair = get_color($datacolor, @color, @bgcolor)
  @color_array = [@color]
  @bgcolor_array = [@bgcolor]
  @attrib_array = [@attr]
  @color_pair_array = [@color_pair]
end

Instance Attribute Details

#style_map=(value) ⇒ Object (writeonly)

hash containing color, bgcolor and attr for a given style



222
223
224
# File 'lib/canis/core/include/colorparser.rb', line 222

def style_map=(value)
  @style_map = value
end

#stylesheetObject

Returns the value of attribute stylesheet.



220
221
222
# File 'lib/canis/core/include/colorparser.rb', line 220

def stylesheet
  @stylesheet
end

Instance Method Details

#chunk_parser(f) ⇒ Object

supply with a color parser, if you supplied formatted text



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/canis/core/include/colorparser.rb', line 278

def chunk_parser f
  if f.is_a? Hash
    self.stylesheet = f[:stylesheet]
    content_type = f[:content_type]
  elsif f.is_a? TextPad
    ff = f.document
    self.stylesheet = ff.stylesheet
    content_type = ff.content_type
  elsif f.is_a? Symbol
    content_type = f
  else
    @chunk_parser = f
    return
  end
  @content_type = content_type
  $log.debug "XXX:  chunk_parser setting in CP to #{f}, content+type is #{content_type} "
  require 'canis/core/include/canisparser'
  @chunk_parser ||= CanisParser[content_type]
  raise "colorparser could not find a parser for #{content_type} " unless @chunk_parser
=begin
  if content_type == :tmux
    @chunk_parser = get_default_color_parser()
  elsif content_type == :ansi
    require 'canis/core/util/ansiparser'
    @chunk_parser = AnsiParser.new
  else
    @chunk_parser = f
  end
=end
end

#content_type(ct) ⇒ Object

what if a text pad changes it’s content type, should be not allow it to just

use the same parser with a changed internal parser

UNUSED as yet



272
273
274
275
276
# File 'lib/canis/core/include/colorparser.rb', line 272

def content_type ct
  return if ct == @content_type
  @content_type = ct
  self.chunk_parser ct
end

#convert_to_chunk(s, colorp = $datacolor, att = FFI::NCurses::A_NORMAL) ⇒ Object Also known as: parse_line



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/canis/core/include/colorparser.rb', line 341

def convert_to_chunk s, colorp=$datacolor, att=FFI::NCurses::A_NORMAL

  raise "You have not set parent of this using form(). Try setting window.form " unless @parents
  @chunk_parser ||= get_default_color_parser()
  res = ChunkLine.new
  # stack the values, so when user issues "/end" we can pop earlier ones

  newblockflag = false
  @chunk_parser.parse_format(s) do |p|
    case p
    when Array
      newblockflag = true
      ## got color / attr info, this starts a new span

      # added style 2014-05-19 - 12:57 maybe should be a hash
      #color, bgcolor, attr , style = *p
      lc, lb, la, ls = *p
      if ls
        #sc, sb, sa = resolve_style ls
        map = resolve_style ls
        $log.debug "  STYLLE #{ls} : #{map} "
        lc ||= map[:color]
        lb ||= map[:bgcolor]
        la ||= map[:attr]
      end
      @_bgcolor = lb
      @_color = lc
      if la
        @attr = get_attrib la
      end
      @_color_pair = nil
      if lc && lb
        # we know only store color_pair if both are mentioned in style or tag
        @_color_pair = get_color(nil, lc, lb)
      end

      #@color_pair_array << @color_pair
      #@attrib_array << @attr
      #$log.debug "XXX: CHUNK start cp=#{@color_pair} , a=#{@attr} :: c:#{lc} b:#{lb} : @c:#{@color} @bg: #{@bgcolor} "
      #$log.debug "XXX: CHUNK start arr #{@color_pair_array} :: #{@attrib_array} ::#{@color_array} ::: #{@bgcolor_array} "

    when :endcolor

      # end the current (last) span
      @parents.pop unless @parents.count == 1
      @_bgcolor = @_color = nil
      @_color_pair = nil
      @attr = nil

      #$log.debug "XXX: CHUNK end parents:#{@parents.count}, last: #{@parents.last} "
    when :reset   # ansi has this
      # end all previous colors
      # end the current (last) span
      # maybe we need to remove all parents except for first
      @parents.pop unless @parents.count == 1
      @_bgcolor = @_color = nil
      @_color_pair = nil
      @attr = nil


    when String

      ## create the chunk
      #$log.debug "XXX:  CHUNK     using on #{p}  : #{@_color_pair} , #{@attr}, fg: #{@_color}, #{@_bgcolor}, parent: #{@parents.last} " # 2011-12-10 12:38:51

      #chunk =  [color_pair, p, attr] 
      chunk = Chunk.new @_color_pair, p, @attr
      chunk.color = @_color
      chunk.bgcolor = @_bgcolor
      chunk.parent = @parents.last
      if newblockflag
        @parents << chunk
        #$log.debug "XXX: CHUNK start parents:#{@parents.count}, #{@parents.last} "
        newblockflag = true
      end
      if block_given?
        yield chunk
      else
        res << chunk
      end
    end
  end # parse
  return res unless block_given?
end

#form=(f) ⇒ Object

this is the widget actually that created the parser



248
249
250
# File 'lib/canis/core/include/colorparser.rb', line 248

def form=(f)
  @parents = [f]
end

#get_default_color_parserObject

returns an instance of the default color parser (tmux parser)



428
429
430
431
432
433
434
# File 'lib/canis/core/include/colorparser.rb', line 428

def get_default_color_parser
  #require 'canis/core/util/defaultcolorparser'
  #@chunk_parser || DefaultColorParser.new

  require 'canis/core/include/canisparser'
  @chunk_parser ||= CanisParser[:tmux]
end

#parse_text(formatted_text) ⇒ Array<Abstractchunkline>

parse array of Strings into array of Chunks (Chunklines)

Parameters:

  • formatted (Array<String>)

    text to be parsed into chunks

Returns:

  • (Array<Abstractchunkline>)

    array of text in our native format (chunklines)



323
324
325
326
327
328
329
# File 'lib/canis/core/include/colorparser.rb', line 323

def parse_text formatted_text
  l = []
  formatted_text.each { |e| 
    l << convert_to_chunk(e) 
  }
  return l
end

#resolve_style(style) ⇒ Object

since 2014-05-19 - 13:14 converts a style name given in a document to color, bg, and attr from a stylesheet



311
312
313
314
315
316
317
318
319
# File 'lib/canis/core/include/colorparser.rb', line 311

def resolve_style style
  if @style_map
    # style_map contains a map for each style
    retval =  @style_map[style]
    raise "Invalid style #{style} in document" unless retval
    return retval
  end
  raise "Style given in document, but no stylesheet provided"
end