joshu / mars forked from rubys/mars

Yet Another Planet Refactoring

This URL has Read+Write access

joshu (author)
Sun Apr 20 14:37:57 -0700 2008
commit  f09cbe485c4ded878a85ae29e47775468404c307
tree    259d311ce05f595c0ed8dcf711c0820559c769e0
parent  127bdf5d62b9f25fa140dc9b73294e99fbe87d71
mars / planet / publisher.rb
100644 40 lines (31 sloc) 1.119 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'planet/config'
require 'planet/hamlformatter'
require 'planet/xsltformatter'
 
# a TemplatePublisher coordinates processing of the feed with provided templates
class TemplatePublisher
 
  ALLOWED_TEMPLATES = %w[xslt haml]
 
  def initialize
    @formatters = { 'haml' => HamlFormatter.new,
                    'xslt' => XsltFormatter.new }
  end
  
  def publish_feed(template_files, feed)
    config = Planet.config['Planet']
    output_dir = config['output_dir'] || '.'
    
      # loop through the listed templates
      template_files.split.each do |template|
        next unless template =~ /^(.* \. .*) \. (\w+)/x
 
        # skip templates that aren't supported
        unless ALLOWED_TEMPLATES.include?($2) then
          Planet.log.warn "#{$2}: not yet supported"
          next
        end
 
        # pick the formatter
        @formatter = @formatters[$2]
 
        # process the template
        File.open(File.join(output_dir,$1),'w') do |file|
          Planet.log.info "Processing template #{template}"
          file.write @formatter.process(template, feed)
        end
      end
    end
    
  end