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      ||= $def_fg_color
  @bgcolor    ||= $def_bg_color
  @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



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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/canis/core/include/colorparser.rb', line 343

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

  _color, _bgcolor = ColorMap.get_colors_for_pair colorp # 2014-08-31 - 13:31 
  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
      # 2014-09-01 should i set these to _color and _bgcolor if nil. NOTE
      @_bgcolor = lb
      @_color = lc
      if la
        @attr = get_attrib la
      end
      @_color_pair = nil
      @_color_pair = colorp # 2014-08-31 - 13:20 
      # 2014-09-01 - 11:52 if only one of the two is given we were ignoring, now we 
      #  need to derive the other one
      if lc && lb
        # we know only store color_pair if both are mentioned in style or tag
        @_color_pair = get_color(colorp, lc, lb)
        # added next two conditions on 2014-09-01 - 11:56 to take care of only one mentioned in
        # #{ block.
      elsif lc
        @_color_pair = get_color(colorp, lc, _bgcolor)
      elsif lb
        @_color_pair = get_color(colorp, _color, lb)
      end


    when :endcolor

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

      # trying out 2014-08-31 - 13:09 since we have to respect passed in colors
      @_color ,  @_bgcolor = _color, _bgcolor
      @_color_pair = colorp
      @attr = att

      #$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
      
      # trying out 2014-08-31 - 13:09 since we have to respect passed in colors
      @_color, @_bgcolor = _color, _bgcolor
      @_color_pair = colorp
      @attr = att


    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

      # trying out 2014-08-31 - 13:09 since we have to respect passed in colors
      @_color ||= _color
      @_bgcolor ||= _bgcolor
      @color_pair ||= colorp
      @attr ||= att
      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)



451
452
453
454
455
456
457
# File 'lib/canis/core/include/colorparser.rb', line 451

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, colorp = nil, attr = nil) ⇒ Array<Abstractchunkline>

parse array of Strings into array of Chunks (Chunklines) 2014-09-11 - 19:48 added colorp and attr so that incorrect default is not picked

object such as textpad can send in default, textdocument not does so.

Parameters:

  • formatted (Array<String>)

    text to be parsed into chunks

Returns:

  • (Array<Abstractchunkline>)

    array of text in our native format (chunklines)



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

def parse_text formatted_text, colorp=nil, attr=nil
  l = []
  formatted_text.each { |e| 
    l << convert_to_chunk(e, colorp, attr)
  }
  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