class Canis::TextDocument

In an attempt to keep TextPad simple, and move complexity of complex content out of it,

I am trying to move specialized processing and rendering to a Document class which manages the same.
I would also like to keep content, and content_type etc together. This should percolate to multibuffers
to.
An application may create a TextDocument object and pass it to TextPad using the +text+ method.
Or an app may send in a hash, which +text+ uses to create this object.

Attributes

content_type[RW]
options[RW]

hash of options passed in constructor including content_type and stylesheet

renderer[RW]

specify a renderer if you do not want the DefaultRenderer to be installed.

source[R]

the source object using this document

stylesheet[RW]
text[RW]

text is the original Array<String> which contains markup of some sort

which source will retrieve. Changes happen to this (row added, deleted, changed)

Public Class Methods

new(hash) click to toggle source
# File lib/canis/core/include/textdocument.rb, line 41
def initialize hash
  @parse_required = true
  @options = hash
  @content_type = hash[:content_type]
  @stylesheet = hash[:stylesheet]
  @text = hash[:text]
  $log.debug "  TEXTDOCUMENT created with #{@content_type} , #{@stylesheet} "
  raise "textdoc recieves nil content_type in constructor" unless @content_type
end

Public Instance Methods

create_default_content_type_handler() click to toggle source

if there is a content_type specfied but nothing to handle the content

then we create a default handler.
# File lib/canis/core/include/textdocument.rb, line 68
def create_default_content_type_handler
  raise "source is nil in textdocument" unless @source
  require 'canis/core/include/colorparser'
  # cp will take the content+type from self and select actual parser
  cp = Chunks::ColorParser.new @source
  @content_type_handler = cp
end
native_text() click to toggle source

returns the native or transformed format of original content. text gets transformed into

native text. The renderer knows how to display native_text.

NOTE: native_text is currently Chunklines - chunks of text with information of color

# File lib/canis/core/include/textdocument.rb, line 30
def native_text
  unless @native_text
    preprocess_text @text
  end
  return @native_text
end
parse_formatted_text(formatted_text, config=nil) click to toggle source

This is now to be called at start when text is set, and whenever there is a data modification. This updates @native_text @param [Array<String>] original content sent in by user

which may contain markup

@param [Hash] config containing

content_type
stylesheet

@return [Chunklines] content in array of chunks.

# File lib/canis/core/include/textdocument.rb, line 94
def parse_formatted_text(formatted_text, config=nil)
  return unless @parse_required

  unless @content_type_handler
    create_default_content_type_handler
  end
  @parse_required = false
  # 2014-09-11 - 19:47 sending in color from here, otherwise the wrong default is picked. TEST
  @native_text = @content_type_handler.parse_text formatted_text, @source.color_pair, @source.attr
end
parse_line(lineno) click to toggle source

transform a given line number from original content to internal format. Called by textpad when a line changes (update)

# File lib/canis/core/include/textdocument.rb, line 82
def parse_line(lineno)
  @native_text[lineno] = @content_type_handler.parse_line( @list[lineno]) 
end
parse_required() click to toggle source

declare that transformation of entire content is required. Currently called by fire_dimension_changed event

of textpad. NOTE: not called from event, now called in text()
# File lib/canis/core/include/textdocument.rb, line 52
def parse_required
  @parse_required = true
end
preprocess_text(data) click to toggle source

called by textpad to do any parsing or conversion on data since a textdocument by default does some transformation on the content

# File lib/canis/core/include/textdocument.rb, line 77
def preprocess_text data
  parse_formatted_text data
end
source=(sou) click to toggle source

set the object that is using this textdocument (typically TextPad). This allows us to bind to events such as adding or deleting a row, or modification of data.

# File lib/canis/core/include/textdocument.rb, line 57
def source=(sou)
  @source = sou
  if @renderer
    @source.renderer = @renderer
  end
  @source.bind :ROW_CHANGED  do | o, ix|  parse_line ix ; end
  @source.bind :DIMENSION_CHANGED do | o, _meth|  parse_required() ; end
  @source.title = self.title() if self.title()
end
title() click to toggle source

returns title of document

# File lib/canis/core/include/textdocument.rb, line 105
def title
  return @options[:title]
end
title=(t) click to toggle source

set title of document (to be displayed by textpad)

# File lib/canis/core/include/textdocument.rb, line 109
def title=(t)
  @options[:title] = t
end