public
Description: Git based blog?
Homepage:
Clone URL: git://github.com/sandal/korma.git
korma / korma.rb
100755 238 lines (184 sloc) 5.86 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require 'rubygems'
require 'redcloth'
require "builder"
require "fileutils"
require "erb"
require 'digest/md5'
require "pathname"
require "time"
 
KORMA_DIR = File.expand_path(File.dirname(__FILE__))
 
module Korma
  module Blog
 
    include FileUtils
 
    class Entry
      def initialize(file, author="")
        entry_data = Blog.parse_entry(file.read)
 
        @filename = "#{file.basename}.html"
        @author = Blog.authors[author]
        @title = entry_data[:title]
        @description = entry_data[:description]
        @entry = entry_data[:entry]
        @published_date = entry_data[:timestamp]
        @url = "/#{@author.base_path}#{@filename}"
      end
 
      attr_reader :title, :description, :entry, :published_date, :url, :author, :filename
 
      private
 
    end
 
    class Author
 
      def initialize(account, name, email, guest)
        @account, @name, @email, @guest = account, name, email, guest
      end
     
      attr_reader :account, :name, :email, :guest
 
      def base_path
        "posts/#{account}/"
      end
 
      def index_uri
        "/#{base_path}index.html"
      end
 
      def bio_uri
        "/about/#{account}.html"
      end
 
      def feed_uri
        "/feed/#{account}.xml"
      end
 
      def gravatar(size=80)
        "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}?s=#{size}"
      end
 
    end
 
    extend self
    attr_accessor :repository, :www_dir, :title, :domain, :description
    attr_reader :authors
 
    def authors=(data)
      @authors = {}
 
      data.each do |k,v|
        @authors[k] = Author.new(k, v['name'], v['email'], v['guest'])
      end
    end
 
    def normalize_path(path)
      path.gsub(%r{/+},"/")
    end
    
    def parse_entry(entry)
      entry =~ /=title(.*)=timestamp(.*)=description(.*)=entry(.*)/m
      { :title => $1.strip, :description => $3.strip,
        :entry => $4.strip, :timestamp => Time.parse($2) }
    end
 
    def author_names
      authors.keys
    end
 
    def all_entries
      entries = []
      author_names.each do |a|
        entries.concat entries_for_author(a)
      end
      entries.sort! { |a,b| b.published_date <=> a.published_date }
    end
 
    def site_feed
      to_rss(all_entries)
    end
 
    def entries_for_author(author)
      tree = Pathname.glob "#{repository}/posts/#{author}/*"
      return [] unless tree
      tree.select { |e| e.file? }.map { |e| Entry.new(e, author) }
    end
 
    def feed(author)
      to_rss entries_for_author(author).sort { |a,b| b.published_date <=> a.published_date }
    end
 
    def author_index(author)
      @author = authors[author]
      @entries = entries_for_author(author).sort { |a,b| b.published_date <=> a.published_date }
      erb :author_index
    end
 
    def site_index
      @entries = Korma::Blog.all_entries
      erb :index
    end
 
    def bio(author)
      @author = Korma::Blog.authors[author]
      file = repository + "about/#{author}"
 
      layout { RedCloth.new(ERB.new(file.read).result(binding)).to_html }
    end
 
    def update_stylesheet
      file = repository + "styles.css"
 
      if file.exist?
        write "styles.css", file.read
      end
    end
 
    def layout
      file = repository + "layout.erb"
 
      if file.exist?
        ERB.new(file.read).result(binding)
      else
        yield
      end
    end
 
    def generate_static_files
      # fix relative path names
      self.repository = repository.realpath unless repository.absolute?
 
      mkdir_p www_dir
      cd www_dir
      write "feed.xml", site_feed
      
      write 'index.html', site_index
 
      mkdir_p "feed"
      mkdir_p "about"
 
      about = repository + "about/index"
 
      if about.exist?
        write "about/index.html", layout { RedCloth.new(about.read).to_html }
      end
 
      update_stylesheet
 
      author_names.each do |author|
        write "feed/#{author}.xml", feed(author)
        mkdir_p "posts/#{author}"
        write "posts/#{author}/index.html", author_index(author)
        entries_for_author(author).each do |e|
          @post = e
          @author = authors[author]
          @contents = RedCloth.new(e.entry).to_html
          write "posts/#{author}/#{e.filename}", erb(:post)
        end
        write "about/#{author}.html", bio(author)
      end
 
    end
 
    def write(file, contents)
      File.open(file, "w") { |f| f << contents }
    end
 
    def erb(file)
      file = repository + "views/#{file}.erb"
      
      if File.exist? file
        engine = ERB.new(file.read)
        layout { engine.result(binding) }
      else
        raise "File not found #{file}"
      end
    end
 
    def to_rss(entries)
      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.gsub( %r{</?[^>]+?>}, '' )
              xml.description RedCloth.new(entry.entry).to_html
              xml.author "#{entry.author.email} (#{entry.author.name})"
              xml.pubDate entry.published_date.rfc822
              xml.link "http://#{domain}#{entry.url}"
              xml.guid "http://#{domain}#{entry.url}"
            end
          end
        end
      end
    end
 
  end
end
 
Korma::Blog.repository = Pathname.new(ARGV[0])
config = YAML.load((Korma::Blog.repository + "korma_config.yml").read)
 
Korma::Blog.title = config['title']
Korma::Blog.domain = config['domain']
Korma::Blog.description = config['description']
Korma::Blog.authors = config['authors']
Korma::Blog.www_dir = ARGV[1] || "www"
 
Korma::Blog.generate_static_files