Class: Hobix::Out::Atom

Inherits:
BaseOutput show all
Defined in:
lib/hobix/out/atom.rb

Instance Method Summary collapse

Methods inherited from BasePlugin

inherited, start

Constructor Details

#initialize(weblog) ⇒ Atom

Returns a new instance of Atom.



35
36
37
# File 'lib/hobix/out/atom.rb', line 35

def initialize( weblog )
    @path = weblog.skel_path
end

Instance Method Details

#extensionObject



38
39
40
# File 'lib/hobix/out/atom.rb', line 38

def extension
    "atom"
end

#load(file_name, vars) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hobix/out/atom.rb', line 41

def load( file_name, vars )
    rssdoc = REXML::Document.new( <<EOXML )
<feed
  xmlns="http://www.w3.org/2005/Atom"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xml:lang="en">
<title></title>
<link rel="alternate" type="text/html" href="" />
<link rel="self" type="application/atom+xml" href="" />
<updated></updated>
<subtitle></subtitle>
<id></id>
<generator uri="http://hobix.com/" version="#{ Hobix::VERSION }">Hobix</generator>
<rights></rights>
</feed>
EOXML
    uri = vars[:weblog].link
    rssdoc << REXML::XMLDecl.new
    rssdoc.elements['/feed/title'].text = vars[:weblog].title
    alt_uri = vars[:weblog].link.to_s
    REXML::XPath.first(rssdoc, '/atom:feed/atom:link[@rel="alternate"]', 
      { 'atom' => 'http://www.w3.org/2005/Atom' }).attributes['href'] = alt_uri
    self_uri = "#{vars[:weblog].link}#{vars[:page].link}"
    REXML::XPath.first(rssdoc, '/atom:feed/atom:link[@rel="self"]', 
      { 'atom' => 'http://www.w3.org/2005/Atom' }).attributes['href'] = self_uri
    rssdoc.elements['/feed'].attributes['xml:base'] = self_uri
    rssdoc.elements['/feed/subtitle'].text = vars[:weblog].tagline
    rssdoc.elements['/feed/updated'].text = vars[:page].updated.strftime( "%Y-%m-%dT%H:%M:%SZ" )
    rssdoc.elements['/feed/id'].text = "tag:#{ uri.host },#{ Time.now.year }:blog#{ uri.path }"
    rssdoc.elements['/feed/rights'].text = vars[:weblog].copyright || "None"
    ( vars[:entries] || [vars[:entry]] ).each do |e|
        ele = REXML::Element.new 'entry'
        ele.extend XmlQuick
        ele.attributes['xml:base'] = e.link
        ele.x( 'title', e.title )
        ele.x( 'link', nil, {'rel' => 'alternate', 'type' => 'text/html', 'href' => e.link } )
        ele.x( 'id', "tag:#{ uri.host },#{ Time.now.year }:blog#{ CGI.escape(uri.path) }entry#{ CGI.escape( "/#{ e.id }" ) }" )
        ele.x( 'published', e.created.strftime( "%Y-%m-%dT%H:%M:%SZ" ) )
        ele.x( 'updated', (e.modified || e.created).strftime( "%Y-%m-%dT%H:%M:%SZ" ) )
        ele.x( 'dc:subject', e.section_id )
        e.tags.find_all {|t| not (t.nil? or t == '') }.each do |t|
            ele.x( 'category', '', { 'term' => t, 'scheme' => "http://hobix.com/tags" } )
        end
        ele.x( 'summary', 
            e.summary.to_html.gsub( /img src="\//, "img src=\"#{ vars[:weblog].link }/" ),
            {'type' => 'html', 'mode' => 'escaped'} ) if e.respond_to? :summary and e.summary and !e.summary.empty?
        author = vars[:weblog].authors[e.author]
        ele_auth = REXML::Element.new 'author'
        ele_auth.extend XmlQuick
        ele_auth.x( 'name', author['name'] )
        ele_auth.x( 'uri', author['url'] ) if author['url']
        ele_auth.x( 'email', author['email'] ) if author['email']
        ele << ele_auth
        ele.x( 'content', e.content.to_html, {'type' => 'html'} )
        rssdoc.elements['/feed'].add ele
    end
    rssdoc.to_s
end