sandal (owner)

Revisions

gist: 4177 Download_button fork
public
Public Clone URL: git://gist.github.com/4177.git
Embed All Files: show embed
blaag.rb #
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
["rubygems","builder","yaml", "fileutils", "redcloth", "erb","uri"].each { |l| require l }
 
class Blaag
  TITLE = "Majestic Sea Creature"
  DOMAIN = "blog.majesticseacreature.com"
  AUTHOR = "Gregory Brown"
  DESCRIPTION = "Technical Ramblings from #{AUTHOR}"
  BASEDIR = File.dirname(File.expand_path(__FILE__)) + "/../"
        
  def self.generate
    entries = Dir["#{Blaag::BASEDIR}/entries/**/*"].
                reject { |f| File.directory?(f) }
                
    entries.map! { |e| Entry.load(e) }
        
    @blaag = Blaag.new(entries)
    
    File.open("#{Blaag::BASEDIR}/www/index.html","w") { |f|
      f << layout { @blaag.to_html }
    }
    
    File.open("#{Blaag::BASEDIR}/www/rss.xml","w") { |f|
      f << @blaag.to_rss
    }
          
    @blaag.entries.each do |e|
      FileUtils.mkdir_p "#{Blaag::BASEDIR}/www/archives/#{e.archive}"
      File.open("#{Blaag::BASEDIR}/www/archives/#{e.archive}/#{e.file}.html","w") { |f|
        f << layout { e.to_html }
      }
    end
    
    build_archive_indices(@blaag.entries)
    
  end
  
  def self.build_archive_indices(entries)
    archives = Hash.new { |h,k| h[k] = [] }
    entries.each do |e|
      archives[e.archive] << e
    end
    archives.each do |arch, entries|
      @top_entries = entries.sort_by { |e| e.published_date }.reverse
      File.open("#{Blaag::BASEDIR}/www/archives/#{arch}/index.html","w") { |f|
        f << layout { ProcessTemplate("blog") }
      }
    end
  end
  
  attr_reader :entries
  
  def initialize(entries)
    @entries = entries
    @top_entries = entries.sort_by { |e| e.published_date }.reverse[0..14]
  end
  
  def to_html
     ProcessTemplate("blog")
  end
  
  def to_rss
    xml = Builder::XmlMarkup.new
    xml.instruct!
    xml.rss :version => "2.0" do
      xml.channel do
        xml.title TITLE
        xml.link "http://#{DOMAIN}/"
        xml.description DESCRIPTION
        xml.language "en-us"
 
        @entries.each do |entry|
          xml.item do
            xml.title entry.title
            xml.description entry.description
            xml.author AUTHOR
            xml.pubDate entry.published_date
            xml.link entry.url
            xml.guid entry.url
          end
        end
      end
    end
  end
  
  class Entry
    
    def self.registry
      @registry ||= YAML.load_file("#{Blaag::BASEDIR}/data/registry.yml")
    end
    
    def self.update_registry(filename,mtime)
      registry[filename] ||= mtime
      File.open("#{Blaag::BASEDIR}/data/registry.yml","w") do |f|
        f << @registry.to_yaml
      end
    end
         
    def self.load(filename)
      file = File.open(filename)
      update_registry(filename,file.mtime)
      
      options = parse(file.read)
      options[:published_date] = registry[filename]
      options[:archive] = File.dirname(filename) =~ /.*\/(.*)\/?.*/ && $1
      options[:url] = "http://#{Blaag::DOMAIN}/archives/#{options[:archive]}/#{File.basename(filename)}.html"
      options[:file] = File.basename(filename)
      
      entry = Entry.new(options)
    end
    
    def self.parse(entry)
      entry =~ /=title(.*)=description(.*)=entry(.*)/m
      { :title => $1.strip, :description => $2.strip, :entry => $3.strip }
    end
    
    def initialize(options={})
      @title = options[:title]
      @description = options[:description]
      @entry = RedCloth.new(options[:entry]).to_html
      @archive = options[:archive]
      @published_date = options[:published_date]
      @url = options[:url]
      @file = options[:file]
    end
    
    attr_reader :title, :description, :entry, :archive, :published_date, :url,
                :file
    
    def to_html
      ProcessTemplate("entry")
    end
    
    def related
      link = URI.escape(url)
      "<a href='http://blogsearch.google.com/blogsearch?hl=en&q=link%3A#{link}&btnG=Search+Blogs'>" +
      "Responses</a>"
    end
        
  end
  
  def self.layout(&block)
    ProcessTemplate("layout",&block)
  end
  
end
 
def ProcessTemplate(file)
  ERB.new(File.read("#{Blaag::BASEDIR}/templates/#{file}.html.erb")).result(binding)
end