Class: Swordfish::Node::Text

Inherits:
Inline show all
Defined in:
lib/swordfish/nodes/text.rb

Instance Attribute Summary

Attributes inherited from Base

#children, #content, #style

Instance Method Summary collapse

Methods inherited from Base

#clear_children, #find_nodes_by_type, #inform!, #initialize, #replace, #replace_with, #stylize, #wrap_children

Constructor Details

This class inherits a constructor from Swordfish::Node::Base

Instance Method Details

#append(node) ⇒ Object

Override Base append because a text node should never have children

Raises:



8
9
10
# File 'lib/swordfish/nodes/text.rb', line 8

def append(node)
  raise BadContentError
end

#to_htmlObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/swordfish/nodes/text.rb', line 12

def to_html
  @content ||= ""
  @content.gsub!(/[[:space:]]/, ' ')
  leading_space = !!@content.lstrip!  # If there is a leading or trailing space,
  trailing_space = !!@content.rstrip! # shift it outside of any formatting tags
  html = CGI::escapeHTML(@content)
  if html.length > 0
    html = "<i>#{html}</i>" if @style.italic?
    html = "<b>#{html}</b>" if @style.bold?
    html = "<u>#{html}</u>" if @style.underline?
    html = "<strike>#{html}</strike>" if @style.strikethrough?
    html = "<sup>#{html}</sup>" if @style.superscript?
    html = "<sub>#{html}</sub>" if @style.subscript?
    html = "<strong>#{html}</strong>" if @style.strong?
    html = "<em>#{html}</em>" if @style.emphasis?
  end
  html = "#{' ' if leading_space}#{html}#{' ' if trailing_space}"
  html
end