class Canis::Chunks::ColorParser

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,

Attributes

style_map[W]

hash containing color, bgcolor and attr for a given style

stylesheet[R]

Public Class Methods

new(cp) click to toggle source
# 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

Public Instance Methods

chunk_parser(f) click to toggle source

supply with a color parser, if you supplied formatted text

# 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) click to toggle source

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

# 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) { |chunk| ... } click to toggle source

Takes a formatted string and converts the parsed parts to chunks.

@param [String] takes the entire line or string and breaks into an array of chunks @yield chunk if block @return [ChunkLine] # [Array] array of chunks @since 1.4.1 2011-11-3 experimental, can change 2014-05-24 - 12:54 NEW As of now since this is called at parse time

colors must not be hardcoded, unless both fg and bg are given for a chunk.
The color_pair should be resolved at render time using parent.
# 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
Also aliased as: parse_line
form=(f) click to toggle source

this is the widget actually that created the parser

# File lib/canis/core/include/colorparser.rb, line 248
def form=(f)
  @parents = [f]
end
get_default_color_parser() click to toggle source

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

# 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_line(s, colorp=$datacolor, att=FFI::NCurses::A_NORMAL)
Alias for: convert_to_chunk
parse_text(formatted_text, colorp=nil, attr=nil) click to toggle source

parse array of Strings into array of Chunks (Chunklines) @param [Array<String>] formatted text to be parsed into chunks @return [Array<Abstractchunkline>] array of text in our native format (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.
# 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) click to toggle source

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

# 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
stylesheet=(s) click to toggle source

set a stylesheet – this is a file path containing yaml a style_map is loaded from the stylesheet Sending a symbol such as :help will load style_help.yml @param [String, Symbol] s is a pathname for stylesheet or symbol pointing to a stylesheet

# File lib/canis/core/include/colorparser.rb, line 256
def stylesheet=(s)
  return unless s
  if s.is_a? Symbol
    s = CANIS_DOCPATH + "style_#{s}.yml"
  end
  @stylesheet = s
  if File.exist? s
    require 'yaml'
    @style_map = YAML::load( File.open( File.expand_path(s) ))
  else
    raise "Could not find stylesheet file #{s}"
  end
end