Class: Atom::Text
- Defined in:
- lib/atom/text.rb,
lib/atom/yaml.rb
Overview
An Atom::Element representing a text construct. It has a single attribute, “type”, which specifies how to interpret the element’s content. Different types are:
- text
-
a plain string, without any markup (default)
- html
-
a chunk of HTML
- xhtml
-
a chunk of well-formed XHTML
You should set this attribute appropriately after you set a Text element (entry.content, entry.title or entry.summary).
This content of this element can be retrieved in different formats, see #html and #xml
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Element
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
:nodoc:.
-
#html ⇒ Object
returns a string suitable for dumping into an HTML document.
-
#initialize(value, name) ⇒ Text
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#taguri ⇒ Object
:nodoc:.
-
#to_element ⇒ Object
:nodoc:.
-
#to_s ⇒ Object
convenient, but not overly useful.
-
#to_yaml(opts = {}) ⇒ Object
:nodoc:.
-
#to_yaml_style ⇒ Object
:nodoc:.
-
#xml ⇒ Object
attempts to parse the content of this element as XML and return it as an array of REXML::Elements.
Methods inherited from Element
#[], attrb, attrs, define_accessor, element, elements, inherited, #local_name, required, #to_xml, #to_yaml_properties
Constructor Details
#initialize(value, name) ⇒ Text
:nodoc:
23 24 25 26 27 28 29 |
# File 'lib/atom/text.rb', line 23 def initialize value, name # :nodoc: @content = value @content ||= "" # in case of nil self["type"] = "text" super name end |
Instance Method Details
#[]=(key, value) ⇒ Object
:nodoc:
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/atom/text.rb', line 82 def []= key, value # :nodoc: if key == "type" unless valid_type? value raise "atomTextConstruct type '#{value}' is meaningless" end if value == "xhtml" begin parse_xhtml_content rescue REXML::ParseException raise "#{@content.inspect} can't be parsed as XML" end end end super(key, value) end |
#html ⇒ Object
returns a string suitable for dumping into an HTML document.
(or nil if that's impossible)
if you’re storing the content of a Text construct, you probably want this representation.
45 46 47 48 49 50 51 |
# File 'lib/atom/text.rb', line 45 def html if self["type"] == "xhtml" or self["type"] == "html" to_s elsif self["type"] == "text" REXML::Text.new(to_s).to_s end end |
#inspect ⇒ Object
:nodoc:
78 79 80 |
# File 'lib/atom/text.rb', line 78 def inspect # :nodoc: "'#{to_s}'##{self['type']}" end |
#taguri ⇒ Object
:nodoc:
44 45 |
# File 'lib/atom/yaml.rb', line 44 def taguri # :nodoc: end |
#to_element ⇒ Object
:nodoc:
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/atom/text.rb', line 100 def to_element # :nodoc: e = super if self["type"] == "text" e.attributes.delete "type" end # this should be done via inheritance unless self.class == Atom::Content and self["src"] c = convert_contents e if c.is_a? String e.text = c elsif c.is_a? REXML::Element e << c.dup else raise RuntimeError, "atom:#{local_name} can't contain type #{@content.class}" end end e end |
#to_s ⇒ Object
convenient, but not overly useful. see #html instead.
32 33 34 35 36 37 38 |
# File 'lib/atom/text.rb', line 32 def to_s if self["type"] == "xhtml" @content.children.to_s else @content.to_s end end |
#to_yaml(opts = {}) ⇒ Object
:nodoc:
47 48 49 50 51 |
# File 'lib/atom/yaml.rb', line 47 def to_yaml( opts = {} ) # :nodoc: YAML::quick_emit( object_id, opts ) do |out| out.scalar(taguri, to_s, to_yaml_style) end end |
#to_yaml_style ⇒ Object
:nodoc:
52 53 54 55 56 57 58 |
# File 'lib/atom/yaml.rb', line 52 def to_yaml_style # :nodoc: if @content.to_s.match("\n") :fold else :plain end end |
#xml ⇒ Object
attempts to parse the content of this element as XML and return it as an array of REXML::Elements.
If self is “html” and Hpricot is installed, it will be converted to XHTML first.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/atom/text.rb', line 58 def xml if self["type"] == "xhtml" @content.children elsif self["type"] == "text" [self.to_s] elsif self["type"] == "html" begin require "hpricot" rescue raise "Turning HTML content into XML requires Hpricot." end fixed = Hpricot(self.to_s, :xhtml_strict => true) REXML::Document.new("<div>#{fixed}</div>").root.children else # XXX check that @type is an XML mimetype and parse it raise "I haven't implemented this yet" end end |