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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
%w(camping/session coderay coderay/for_redcloth feed).each { |lib| require lib }

# fix unicode urls
$KCODE = 'u'

Camping.goes :Blokk

# TODO: safe Textile for comments
# TODO: fix Textile (produces improper HTML at times, < for example)
# TODO: document
# DONE: make this HTML 5 compatible
# DONE: exclude numerical tags from the post tag list
# DONE: RSS only lists posts with Index tag
# DONE: Show admin links all the time, hide them with CSS
# DONE: Redirect to requested page after login
# DONE: Login link removed, logout redirects to Index
# DONE: Fixed comment posting
module Blokk; VERSION = "1.0"; PAGE_URL = 'http://murfy.de'
  
  def service(*a)
    # don't set any cookies unless necessary
    def @cookies.[]=(k,v); end unless @env['PATH_INFO'] == self / '/login'
    super(*a)
  end
  
  module Models
    
    Base.establish_connection :adapter => 'sqlite3', :database => File.dirname(__FILE__) + '/../murfy.db'
    
    class CreateTheBasics < V 1.0
      def self.up
        create_table :blokk_admins, :force => true do |table|
          table.string :name, :password
        end
        print "Password for #{user = ENV['USER']}? "
        Admin.create :name => user, :password => $stdin.gets.chomp
        
        create_table :blokk_posts, :force => true do |table|
          table.string :title, :tags, :nickname
          table.text :body
          table.timestamps
        end
        
        create_table :blokk_comments, :force => true do |table|
          table.integer :post_id
          table.string :username
          table.text :body
          table.datetime :created_at
        end
      end
    end if false
    
    class Admin < Base; end
    
    class Post < Base
      has_many :comments, :order => 'created_at ASC'
      validates_presence_of :title, :nickname
      validates_uniqueness_of :nickname
      def self.find_by_id_or_nickname id
        find :first, :conditions => ['id = ? OR nickname = ?', id, id]
      end
    end
    
    class Comment < Base
      validates_presence_of :username
      validates_length_of :body, :within => 1..3000
      validates_inclusion_of :bot, :in => %w(K)
      validates_associated :post
      belongs_to :post
      attr_accessor :bot
    end
    
  end
  
  def self.create
    # Camping::Models::Session.create_schema
    # Models.create_schema :assume => (Blokk::Models::Post.table_exists? ? 1.0 : 0.0)
  end
  
  module Helpers
    
    # login system
    def current_user
      @current_user ||= Models::Admin.find(@cookies.admin_id) unless @cookies.admin_id.blank?
    end
    alias logged_in? current_user
    
    def login name, password = nil
      @current_user = Models::Admin.find_by_name_and_password name, password
      @cookies.admin_id = @current_user ? @current_user.id : nil
    end
    
    # menu bar
    def menu target = nil
      if target
        args = target.is_a?(Symbol) ? [] : [target]
        for role, submenu in menu[target].sort_by { |k, v| [:visitor, :admin].index k }
          ul.menu.send(role) do
            submenu.each do |x|
              li { x[/\A\w+\z/] ? a(x, :href => '#') : x }
              # li { x[/\A\w+\z/] ? a(x, :href => R(Controllers.const_get(x), *args)) : x }
            end
          end unless submenu.empty?
        end
      else
        @menu ||= Hash.new { |h, k| h[k] = { :visitor => [], :admin => [] } }
      end
    end
    
    # shortcut for error-aware labels
    def label_for name, record = nil, attr = name, options = {}
      errors = record && !record.body.blank? && !record.valid? && record.errors[attr]
      label name.to_s, { :for => name }, options.merge(errors ? { :class => :error } : {})
    end
    
    # find all tags
    def tags
      Models::Post.all(:select => 'DISTINCT tags').map(&:tags).join(' ').strip.split(/\s+/).uniq
    end
    
    def slogan
      Models::Post.find_by_nickname('slogan').body rescue ''
    end
    
  end
  
  # beautiful HTML 5
  class Mab
    def initialize(assigns = {}, helpers = nil, &block)
      super(assigns.merge(:indent => 2), helpers, &block)
    end
    def html5
      self.tagset = Markaby::XHTMLStrict
      declare! :DOCTYPE, :html
      tag!(:html) { yield }
      self
    end
  end
  
  TAG_PATTERN = ["tags LIKE '% TAG %", 'TAG %', '% TAG', "TAG'"].join("' OR tags LIKE '")
  
  module Controllers
    
    class Index < R '/', '/index', '/tag/()([-\w]*)', '/all()()', '/(rss)', '/(rss)/([-\w]+)'
      def get format = 'html', tag = 'Index'
        @posts = Post.all :conditions => TAG_PATTERN.gsub('TAG', tag || ''), :order => 'created_at DESC'
        if format == 'rss'
          @comments = Comment.all :order => 'created_at DESC', :include => :post if tag == 'comments'
          rss = ::RSS.feed :title => "(almost) murphy.de#{' - comments' if @comments}",
            :about => "#{PAGE_URL}/rss", :link => PAGE_URL,
            :description => 'Kornelius Kalnbach @ Berlin, Germany',
            :image => ["#{PAGE_URL}/raven.png", '(almost) murphy.de'] do |feed|
            for item in @comments || @posts.select { |p| p.tags[/\bIndex\b/] }
              post = @comments ? item.post : item
              feed.item :title => post.title, :link => "http:#{URL()}/read/#{post.nickname}",
                :date => item.created_at, :author => (item.username if @comments),
                :description => @comments ? item.body : RedCloth.new(item.body.sub(/^---+/, '')).to_html
            end
          end
          r 200, rss.to_s, 'Content-Type' => 'application/rss+xml; charset=UTF-8'
        else
          @tag = tag
          render :index
        end
      end
    end
    
    module Poster
      def post id = nil
        return unless logged_in?
        @post = id.blank? ? Post.new : Post.find(id)
        @post.update_attributes :title => input.title, :body => input.body,
          :tags => input.tags, :nickname => input.nickname
        return redirect Read, @post.nickname if @post.valid?
        render @post.new_record? ? :new : :edit
      end
    end
    
    class New < R '/new', '/new/([-\w]*)'
      def get tag = nil
        @post = Post.new :tags => "Index #{tag}".strip if logged_in?
        render :new
      end
      include Poster
    end
    
    class Edit < R '/edit/([-\w]+)', '/edit'
      def get id
        @post = Post.find_by_id_or_nickname id if logged_in?
        render :edit
      end
      include Poster
    end
    
    class Delete < R '/delete/([-\w]+)', '/delete'
      def get id
        @post = Post.find_by_id_or_nickname id if logged_in?
        render :delete
      end
      def post
        (@post = Post.find(input['id'])).destroy if logged_in?
        redirect Index
      end
    end
    
    class Shoot < R '/shoot/(\d+)'
      def get comment_id
        return unless logged_in?
        (comment = Comment.find(comment_id)).destroy
        redirect self / "/read/#{comment.post.nickname}?#comments"
      end
    end
    
    class Login < R '/login', '/logout'
      def post
        login input.name, input.password
        logged_in? && @env["HTTP_REFERER"][/\A#{PAGE_URL}/] ? redirect($') : get
      end
      def get
        logged_in? ? (login(nil); redirect(Index)) : render(:login)
      end
    end
    
    Logout = Login
    
    # Lowest precedence to allow urls like /<nickname>
    class Read < R '/read/([-\w]+)', '/([-\w]+)'
      def get id  # read article
        @post = Post.find_by_id_or_nickname id
        @comment = Comment.new :username => input.name
        render :view if @post
      end
      def post id  # post comment
        @comment = Comment.create :post_id => id, :bot => input.bot,
          :username => (name = input.name), :body => (comment = input.comment)
        redirect self / "/read/#{Post.find(id).nickname}?name=#{C.escape name}#comments"
      end
    end
     
  end
  
  module Views
    
    def layout
      html5 do
        head do
          title "(almost) murphy.de#{" - #{@post.title}" if @post}"
          link :rel => 'stylesheet', :type => 'text/css', :href => self / '/main.css'
          # link :rel => 'alternate stylesheet', :type => 'text/css', :title => 'Christmas Style', :href => self / '/main-xmas.css'
          link :rel => 'alternate', :type => 'application/rss+xml', :href => "/rss#{"/#@tag" if @tag != 'Index'}"
          link :rel => 'alternate', :type => 'application/rss+xml', :href => "/rss/comments"
          link :rel => 'shortcut icon', :type => 'image/x-icon', :href => '/murfy32.png'
        end
        body do
          div.header! do
            h1 { a(:href => R(Index), :accesskey => 'I') { sup('(almost)') + span('murphy.de') } +
              sup { a '@murphy_karasu', :href => 'https://twitter.com/murphy_karasu', :title => 'me on Twitter' } }
            menu[:top][:visitor] = tags.sort.map { |t| a t, :href => self / "/tag/#{t}" }
            menu[:top][:admin] << a('New', :href => self / "/new#{"/#@tag" if @tag != 'Index'}")
            menu[:top][:admin] << 'Logout' if logged_in?
            div.bar! { menu :top }
          end
          div.slogan { RedCloth.new(slogan).to_html }
          div.content { self << yield }
          div.footer! do
            a "blokk #{VERSION}", :href => 'http://murfy.de/read/blokk'
            a.C! 'C', :href => 'http://camping.rubyforge.org', :title => 'powered by Camping'
          end
        end
      end
    end
    
    def index
      p 'No posts yet / Hier steht noch nix.' if @posts.empty?
      @posts.each { |post| _post post }
    end
    
    def login
      logged_in? ? yield : _login_form
    end
    
    def new
      login { _post_form @post, R(New) }
    end
    
    def edit
      login { _post_form @post, R(Edit) + "/#{@post.id}" }
    end
    
    def delete
      login do
        p { text 'Really delete %s?' % a(@post.title, :href => R(Read, @post.id)) }
        form :action => R(Delete), :method => :post do
          input :type => :hidden, :name => :id, :value => @post.id
          input :type => :submit, :value => 'Delete'
        end
      end
    end
    
    def view
      _post @post, true
      
      # comments
      h2 'Say something! / Sag was!', :id => 'comments'
      for c in @post.comments
        div.comment do
          pre.body c.body
          timestamp = c.created_at.strftime('%H:%M on %A, %Y-%m-%d')
          div.username "#{c.username} @ #{timestamp}"
          a 'Delete', :href => R(Shoot, c), :onclick => "return confirm('Sure?');" if logged_in?
        end
      end
      
      form :action => R(Read, @post), :method => :post do
        div do
          label_for :name, @comment, :username, :accesskey => 'C'
          input.name! :name => :name, :value => @comment.username, :size => 41, :type => :text
        end
        div do
          label_for :comment, @comment, :body
          textarea.comment! '', :name => :comment, :cols => 60, :rows => 10
          input.bot! :type => :hidden, :name => :bot, :value => 'spambot'
          p 'No markup, just plain monospace text. / Kein Markup, nur Normschrift-Klartext.'
          input :type => :submit, :value => 'Post! / Abschicken!',
            :onclick => "getElementById('bot').value='K'"
        end
        # Later: Textile
        # a 'use Textile', :href => 'http://whytheluckystiff.net/ruby/redcloth', :target => '0' if false
      end
    end
    
    # Partials
    def _post post, full = false, id = post.nickname || post.id
      div.post do
        div.title { a post.title, :href => URL() + "/read/#{id.gsub(/ä|ö|ü/, '')}" }
        div.subtitle do
          text "#{post.created_at.strftime('%a %Y-%m-%d')} "
          text '(%s)' % post.tags.scan(/[a-z][-\w]+/i).map { |t|
            a t[0,2], :title => t, :href => R(Index, t)
          }.join
        end
        excerpt, body = *post.body.split(/^---+/, 2)
        div.body { text RedCloth.new("#{excerpt}#{body if full}").to_html }
        div.more { a('Read... / Lesen...', :href => URL() + "/read/#{id.gsub(/ä|ö|ü/, '')}") } if body && !full
        cs = post.comments.size
        menu[id][:visitor] << a("#{cs} comment#{'s' unless cs == 1}", :href => URL() + "/read/#{id.gsub(/ä|ö|ü/, '')}")
        menu[id][:admin] << a('Edit', :href => URL() + "/edit/#{id.gsub(/ä|ö|ü/, '')}", :accesskey => ('E' if full))
        menu[id][:admin] << a('Delete', :href => URL() + "/delete/#{id.gsub(/ä|ö|ü/, '')}")
        menu id
      end
    end
    
    def _login_form
      form :action => R(Login), :method => :post do
        div do
          label_for :name
          input :name => :name, :type => :text
        end
        div do
          label_for :password
          input :name => :password, :type => :password
          input :type => :submit, :name => :login, :value => 'Login'
        end
      end
    end
    
    def _post_form post, action
      form :method => :post, :action => action do
        div do
          label_for :title, post
          input.title! :name => :title, :type => :text, :size => 81, :value => post.title
        end
        div do
          label_for :body, post
          textarea.body! post.body, :name => :body, :cols => 80, :rows => 30, :accesskey => 'B'
        end
        div do
          label_for :nickname, post
          input.nickname! :name => :nickname, :type => :text, :size => 81, :value => post.nickname
          label_for :tags, post, :tags, :accesskey => 'T'
          input.tags! :name => :tags, :type => :text, :size => 81, :value => post.tags
          input :type => :hidden, :name => :id, :value => post.id
          input :type => :submit, :value => 'Save', :accesskey => 'S'
        end
      end
    end
    
  end
  
end