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.



749
750
751
752
753
# File 'lib/atomutil.rb', line 749

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



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# File 'lib/atomutil.rb', line 792

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



755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/atomutil.rb', line 755

def body=(value)

  if value =~ Regexp.new("^(?:
    [[:print:]]
    |[\xc0-\xdf][\x80-\xbf]
    |[\xe0-\xef][\x80-\xbf]{2}
    |[\xf0-\xf7][\x80-\xbf]{3}
    |[\xf8-\xfb][\x80-\xbf]{4}
    |[\xfc-\xfd][\x80-\xbf]{5}
    )*$", Regexp::EXTENDED, 'n')
  #if value =~ /^(?:
  #   [[:print:]]
  #  |[\xc0-\xdf][\x80-\xbf]
  #  |[\xe0-\xef][\x80-\xbf]{2}
  #  |[\xf0-\xf7][\x80-\xbf]{3}
  #  |[\xf8-\xfb][\x80-\xbf]{4}
  #  |[\xfc-\xfd][\x80-\xbf]{5}
  #  )*$/x
    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