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.



308
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
# File 'lib/atom.rb', line 308

def initialize(o)
  case o
  when String
    super(o)
    @type = "xhtml"
  when 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 != XML::Reader::TYPE_ELEMENT; end        

    if xml.local_name == 'div' && xml.namespace_uri == XHTML        
      set_content(xml.read_inner_xml.strip.gsub(/\s+/, ' '))
    else
      set_content(xml.read_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(nodeonly = true, name = 'content', namespace = nil, namespace_map = Atom::Xml::NamespaceMap.new) ⇒ Object



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

def to_xml(nodeonly = true, name = 'content', namespace = nil, namespace_map = Atom::Xml::NamespaceMap.new)
  node = XML::Node.new("#{namespace_map.prefix(Atom::NAMESPACE, name)}")
  node['type'] = 'xhtml'
  node['xml:lang'] = self.xml_lang.to_s
  
  div = XML::Node.new('div')
  div['xmlns'] = XHTML
  
  p = XML::Parser.string(to_s)
  content = p.parse.root.copy(true)
  div << content
  
  node << div
  node
end