Class: Atom::Content::Xhtml

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

Overview

XHTML content within an Atom document.

Constant Summary

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

- (Xhtml) initialize(o)



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/atom.rb', line 284

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

- (Object) to_xml(nodeonly = true, name = 'content',, namespace = nil, namespace_map = Atom::Xml::NamespaceMap.new)



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/atom.rb', line 311

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