Class: Canis::TextDocument

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ TextDocument

Returns a new instance of TextDocument.



41
42
43
44
45
46
47
48
49
# 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

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



19
20
21
# File 'lib/canis/core/include/textdocument.rb', line 19

def content_type
  @content_type
end

#optionsObject

hash of options passed in constructor including content_type and stylesheet



22
23
24
# File 'lib/canis/core/include/textdocument.rb', line 22

def options
  @options
end

#rendererObject

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



37
38
39
# File 'lib/canis/core/include/textdocument.rb', line 37

def renderer
  @renderer
end

#sourceObject

the source object using this document



39
40
41
# File 'lib/canis/core/include/textdocument.rb', line 39

def source
  @source
end

#stylesheetObject

Returns the value of attribute stylesheet.



20
21
22
# File 'lib/canis/core/include/textdocument.rb', line 20

def stylesheet
  @stylesheet
end

#textObject

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

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


25
26
27
# File 'lib/canis/core/include/textdocument.rb', line 25

def text
  @text
end

Instance Method Details

#create_default_content_type_handlerObject

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

then we create a default handler.


68
69
70
71
72
73
74
# 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_textObject

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



30
31
32
33
34
35
# 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) ⇒ Chunklines

This is now to be called at start when text is set, and whenever there is a data modification. This updates @native_text

Parameters:

  • original (Array<String>)

    content sent in by user which may contain markup

  • config (Hash) (defaults to: nil)

    containing content_type stylesheet

Returns:

  • (Chunklines)

    content in array of chunks.



94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object

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



82
83
84
# 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_requiredObject

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()


52
53
54
# File 'lib/canis/core/include/textdocument.rb', line 52

def parse_required
  @parse_required = true
end

#preprocess_text(data) ⇒ Object

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



77
78
79
# File 'lib/canis/core/include/textdocument.rb', line 77

def preprocess_text data
  parse_formatted_text data
end

#titleObject

returns title of document



105
106
107
# File 'lib/canis/core/include/textdocument.rb', line 105

def title
  return @options[:title]
end

#title=(t) ⇒ Object

set title of document (to be displayed by textpad)



109
110
111
# File 'lib/canis/core/include/textdocument.rb', line 109

def title=(t)
  @options[:title] = t
end