public
Description: Yet Another Planet Refactoring
Homepage: http://intertwingly.net/blog/2007/12/19/Yet-Another-Planet-Refactoring
Clone URL: git://github.com/rubys/mars.git
mars / planet / publisher.rb
100644 41 lines (31 sloc) 1.124 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
41
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,
                    'xslt' => XsltFormatter }
  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].new
 
        # 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