Class: Atom::Content::Xhtml

Inherits:
Base show all
Defined in:
lib/atom.rb

Overview

XHTML content within an Atom document.

Constant Summary collapse

XHTML =
'http://www.w3.org/1999/xhtml'

Instance Method Summary collapse

Methods inherited from Base

#==

Methods included from Xml::Parseable

#==, #accessor_name, #current_node_is?, included, #next_node_is?, #parse

Methods inherited from String

#constantize, #demodulize, #singularize

Constructor Details

#initialize(o) ⇒ Xhtml

Returns a new instance of Xhtml.



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/atom.rb', line 309

def initialize(o)
  case o
  when String
    super(o)
    @type = "xhtml"
  when Nokogiri::XML::Reader
    super("")
    xml = o
    parse(xml, :once => true)
    starting_depth = xml.depth

    # Get the next element - should be a div according to the atom spec
    while xml.read && xml.node_type != Nokogiri::XML::Reader::TYPE_ELEMENT; end

    if xml.local_name == 'div' && xml.namespace_uri == XHTML
      set_content(xml.inner_xml.strip.gsub(/\s+/, ' '))
    else
      set_content(xml.outer_xml)
    end

    # get back to the end of the element we were created with
    while xml.read == 1 && xml.depth > starting_depth; end
  else
    raise ArgumentError, "Got #{o} which isn't a String or XML::Reader"
  end
end

Instance Method Details

#to_xml(builder = nil, name = 'content', namespace = nil, namespace_handler = nil) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/atom.rb', line 336

def to_xml(builder = nil, name = 'content', namespace = nil, namespace_handler = nil)
  builder ||= Nokogiri::XML::Builder.new
  namespace_handler.prefix(builder, Atom::NAMESPACE) if namespace_handler
  attrs = {}
  attrs['type'] = 'xhtml'
  attrs['xml:lang'] = self.xml_lang.to_s
  builder.send("#{name}_", attrs) do |node|
    node.div('xmlns' => XHTML) do |div|
      div << to_s
    end
  end
  builder.doc.root
end