Class | Atom::Text |
In: |
lib/atom/text.rb
|
Parent: | Atom::Element |
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
# 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
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.
# 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
# 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
# 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
attempts to parse the content of this element as XML and return it as an array of REXML::Elements.
If self["type"] is "html" and Hpricot is installed, it will be converted to XHTML first.
# 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