Class: Atom::Content::Html

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

Overview

Html content within an Atom document.

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) ⇒ Html

Creates a new Content::Html.

o

An XML::Reader or a HTML string.



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/atom.rb', line 268

def initialize(o)
  case o
  when Nokogiri::XML::Reader
    parse(o, :once => true)
    super(o.read.value)
  when String
    super(o)
    @type = 'html'
  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

:nodoc:



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/atom.rb', line 281

def to_xml(builder = nil, name = 'content', namespace = nil, namespace_handler = nil) # :nodoc:
  builder ||= Nokogiri::XML::Builder.new
  # Reject content that isn't UTF-8. 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.
  #

  self_string = self.to_s

  if self_string.dup.force_encoding("UTF-8").valid_encoding?
    namespace_handler.prefix(builder, Atom::NAMESPACE) if namespace_handler
    attrs = {}
    attrs['type'] = 'html'
    attrs['xml:lang'] = self.xml_lang if self.xml_lang
    builder.send("#{name}_", self_string, attrs)
    builder.doc.root
  else
    raise SerializationError, "Content must be converted to UTF-8 before attempting to serialize to XML: #{self}."
  end
end