Class: Atom::Content

Inherits:
Element show all
Defined in:
lib/atomutil.rb

Overview

Content class

Instance Attribute Summary

Attributes inherited from Element

#elem

Instance Method Summary collapse

Methods inherited from Element

#add, element_attr_accessor, element_attr_accessors, element_datetime_accessor, element_datetime_accessors, element_name, element_ns, element_object_list_accessor, element_text_accessor, element_text_accessors, element_text_list_accessor, #get, #get_attr, #get_object, #get_objects, #getlist, new, ns, #set, #set_attr, #to_s

Constructor Details

#initialize(params = {}) ⇒ Content

Returns a new instance of Content.



753
754
755
756
757
# File 'lib/atomutil.rb', line 753

def initialize(params={})
  super(params)
  #self.body = params[:body] if params.has_key?(:body)
  self.type = params[:type] if params.has_key?(:type)
end

Instance Method Details

#bodyObject



781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# File 'lib/atomutil.rb', line 781

def body
  if @body.nil?
    mode = self.type == 'xhtml'       ? 'xml'\
         : self.type =~ %r{[\/+]xml$} ? 'xml'\
         : self.type == 'html'        ? 'escaped'\
         : self.type == 'text'        ? 'escaped'\
         : self.type =~ %r{^text}     ? 'escaped'\
         :                              'base64'
    case(mode)
      when 'xml'
        unless @elem.elements.empty?
          if @elem.elements.size == 1 && @elem.elements[1].name == 'div'
            @body = @elem.elements[1].collect{ |c| c.to_s }.join('')
          else
            @body = @elem.collect{ |c| c.to_s }.join('')
          end
        else
          @body = @elem.text
        end
      when 'escaped'
        @body = @elem.text
      when 'base64'
        text = @elem.text
        @body = text.nil?? nil : text.unpack('m').first
      else
        @body = nil
    end
  end
  @body
end

#body=(value) ⇒ Object



759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/atomutil.rb', line 759

def body=(value)

  if is_utf8?(value)
    copy = "<div xmlns=\"http://www.w3.org/1999/xhtml\">#{value}</div>"
    is_valid = true
    begin
      node = REXML::Document.new(copy).elements[1][0]
    rescue
      is_valid = false
    end
    if is_valid && node.instance_of?(REXML::Element)
      @elem.add_element node
      self.type = 'xhtml'
    else
      @elem.add_text value
      self.type = (value =~ /^\s*</) ? 'html' : 'text'
    end
  else
    @elem.add_text([value].pack('m').chomp)
  end
end