Class: Atom::Content::Html

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

Overview

Html content within an Atom document.

Instance Method Summary collapse

Methods inherited from Base

#==

Constructor Details

#initialize(o) ⇒ Html

Creates a new Content::Html.

o

An XML::Reader or a HTML string.



225
226
227
228
229
230
231
232
233
234
# File 'lib/atom.rb', line 225

def initialize(o)
  case o
  when XML::Reader
    super(o.read_string.gsub(/\s+/, ' ').strip)
    parse(o, :once => true)
  when String
    super(o)
    @type = 'html'
  end        
end

Instance Method Details

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

:nodoc:



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/atom.rb', line 236

def to_xml(nodeonly = true, name = 'content', namespace = nil, namespace_map = Atom::Xml::NamespaceMap.new) # :nodoc:
  require 'iconv'
  # Convert from utf-8 to utf-8 as a way of making sure the content is UTF-8.
  #
  # This is a pretty crappy way to do it but if we don't check libxml just
  # fails silently and outputs the content element without any content. At
  # least checking here and raising an exception gives the caller a chance
  # to try and recitfy the situation.
  #
  begin
    node = XML::Node.new("#{namespace_map.get(Atom::NAMESPACE)}:#{name}")
    node << Iconv.iconv('utf-8', 'utf-8', self.to_s, namespace_map = nil)
    node['type'] = 'html'
    node['xml:lang'] = self.xml_lang        
    node
  rescue Iconv::IllegalSequence => e
    raise SerializationError, "Content must be converted to UTF-8 before attempting to serialize to XML: #{e.message}."
  end
end