Class: Omniboard::StyledText

Inherits:
Object
  • Object
show all
Defined in:
lib/omniboard/styled_text.rb

Overview

Represents some HTML-esque styled text

The incredibly naive parser assumes all sorts of things about your notes, and faithfully turns them into a StyledText object.

Will probably crash horridly if you try something nasty on it. Don’t do that. Things it will choke on (off the top of my head):

  • Nested <run>s

  • <run>s with attributes

  • <text> or <note> tags within the body of this actual note

  • All kinds of other things?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStyledText

Returns a new instance of StyledText.



50
51
52
# File 'lib/omniboard/styled_text.rb', line 50

def initialize()
  @elements = []
end

Instance Attribute Details

#elementsObject

All the elements that make up this styled text



16
17
18
# File 'lib/omniboard/styled_text.rb', line 16

def elements
  @elements
end

Class Method Details

.parse(styled_html) ⇒ Object

Parse some styled html



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/omniboard/styled_text.rb', line 19

def self.parse(styled_html)
  # Remove outlying HTML - surrounding <note> and <text> elements
  superfluous_tags = /<\/?(note|text)>/
  styled_html = styled_html.gsub(superfluous_tags, "")

  return_value = self.new

  # Run on all text
  until styled_html.empty?

    next_run_index = styled_html.index("<run>")

    if next_run_index.nil?
      return_value << styled_html
      styled_html = ""
    
    else
      # Get rid of any plain html!
      if next_run_index != 0
        return_value << styled_html[0...next_run_index]
        styled_html = styled_html[next_run_index..-1]
      end

      run_end = styled_html.index("</run>") + "</run>".length
      return_value << Omniboard::StyledTextElement.new(styled_html[0...run_end])
      styled_html = styled_html[run_end..-1]
    end
  end
  return_value
end

Instance Method Details

#<<(element) ⇒ Object

Add an element to the elements array



55
56
57
# File 'lib/omniboard/styled_text.rb', line 55

def << element
  @elements << element
end

#to_htmlObject

Turn this styled text into html!



60
61
62
# File 'lib/omniboard/styled_text.rb', line 60

def to_html
  @elements.map{ |e| e.is_a?(String) ? e : e.to_html }.join("")
end