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, initters, is_atom_element, is_element, on_build, on_init, parse, #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.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/atom/text.rb', line 64 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.
93 94 95 96 97 98 99 |
# File 'lib/atom/text.rb', line 93 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:
130 131 132 |
# File 'lib/atom/text.rb', line 130 def inspect # :nodoc: "'#{to_s}'##{self['type']}" end |
#to_s ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/atom/text.rb', line 80 def to_s if type == 'xhtml' and @content and @content.name == 'div' @content.children.to_s else @content.to_s end end |
#type ⇒ Object
76 77 78 |
# File 'lib/atom/text.rb', line 76 def type @type ? @type : 'text' end |
#type=(value) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/atom/text.rb', line 134 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.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/atom/text.rb', line 106 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 # XXX check that @type is an XML mimetype and parse it raise "I haven't implemented this yet" end xml end |