Class: Atom::Text
Overview
An Atom::Element representing a text construct. It has a single attribute, “type”, which specifies how to interpret the element’s content. Different types are:
- text
-
a plain string, without any markup (default)
- html
-
a chunk of HTML
- xhtml
-
a chunk of well-formed XHTML
You should set this attribute appropriately after you set a Text element (entry.content, entry.title or entry.summary).
This content of this element can be retrieved in different formats, see #html and #xml
Instance Attribute Summary
Attributes inherited from Element
Instance Method Summary collapse
-
#html ⇒ Object
returns a string suitable for dumping into an HTML document.
-
#initialize(value = nil) ⇒ Text
constructor
A new instance of Text.
-
#inspect ⇒ Object
:nodoc:.
- #to_s ⇒ Object
- #type ⇒ Object
- #type=(value) ⇒ Object
-
#xml ⇒ Object
attempts to parse the content of this element as XML and return it as an array of REXML::Elements.
Methods included from AttrEl
Methods inherited from Element
#append_elem, attributes, #build, builders, def_get, def_set, do_parsing, #get, #get_atom_attrb, #get_atom_elem, #get_atom_elems, #get_elem, #get_elems, is_atom_element, is_element, on_build, on_init, parse, run_initters, #set, #set_atom_attrb, #to_xml
Methods included from Parsers
#on_parse, #on_parse_attr, #on_parse_many, #on_parse_root, #parse_plain
Methods included from Converters
#atom_attrb, #atom_element, #atom_elements, #atom_link, #atom_string, #atom_time, #attrb, #build_plain, #element, #elements, #strings, #time
Constructor Details
#initialize(value = nil) ⇒ Text
Returns a new instance of Text.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/atom/text.rb', line 73 def initialize value = nil super() @content = if value.respond_to? :to_xml value.to_xml[0] elsif value value else '' end end |
Instance Method Details
#html ⇒ Object
returns a string suitable for dumping into an HTML document.
(or nil if that's impossible)
if you’re storing the content of a Text construct, you probably want this representation.
102 103 104 105 106 107 108 |
# File 'lib/atom/text.rb', line 102 def html if self["type"] == "xhtml" or self["type"] == "html" to_s elsif self["type"] == "text" REXML::Text.new(to_s).to_s end end |
#inspect ⇒ Object
:nodoc:
139 140 141 |
# File 'lib/atom/text.rb', line 139 def inspect # :nodoc: "'#{to_s}'##{self['type']}" end |
#to_s ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/atom/text.rb', line 89 def to_s if type == 'xhtml' and @content and @content.name == 'div' @content.children.to_s else @content.to_s end end |
#type ⇒ Object
85 86 87 |
# File 'lib/atom/text.rb', line 85 def type @type ? @type : 'text' end |
#type=(value) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/atom/text.rb', line 143 def type= value unless valid_type? value raise Atom::ParseError, "atomTextConstruct type '#{value}' is meaningless" end @type = value if @type == "xhtml" begin parse_xhtml_content rescue REXML::ParseException raise Atom::ParseError, "#{@content.inspect} can't be parsed as XML" end end end |
#xml ⇒ Object
attempts to parse the content of this element as XML and return it as an array of REXML::Elements.
If self is “html” and Hpricot is installed, it will be converted to XHTML first.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/atom/text.rb', line 115 def xml xml = REXML::Element.new 'div' if self["type"] == "xhtml" @content.children.each { |child| xml << child } elsif self["type"] == "text" xml.text = self.to_s elsif self["type"] == "html" begin require "hpricot" rescue raise "Turning HTML content into XML requires Hpricot." end fixed = Hpricot(self.to_s, :xhtml_strict => true) xml = REXML::Document.new("<div>#{fixed}</div>").root else # Not XHTML, HTML, or text - return the REXML::Element, leave it up to the user to parse the content xml = @content end xml end |