joahking / scanty forked from adamwiggins/scanty

trying to turn scanty into a microblog

This URL has Read+Write access

scanty / lib / post.rb
100644 87 lines (72 sloc) 1.853 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
require 'RedCloth'
 
module Scanty
  class Post < Sequel::Model
    set_schema do
      primary_key :id
      text :title
      text :body
      text :slug
      text :tags
      timestamp :created_at
    end
 
    validates do
      presence_of :title
      presence_of :body
    end
 
    # this class method bypasses validations, so the condition
    def self.make_slug(title)
      title.downcase.gsub(/ /, '_').gsub(/[^a-z0-9_]/, '').squeeze('_') unless title.nil?
    end
 
    # returns sorted unique non nil non empty tags, see specs
    def self.tags
      map(:tags).compact. # nils out
        collect { |t| t.split(',') }.flatten.uniq. # unique tags in 1-dim array
        collect { |t| t.strip }.sort # around spaces out and sorting
    end
 
    def linked_tags
      tags.split(',').inject([]) do |accum, tag|
        tag.strip!
        accum << "<a href='/past/tags/#{tag}'>#{tag}</a>"
      end.join('&nbsp;') unless tags.nil?
    end
 
    def url
      d = created_at
      "/past/#{d.year}/#{d.month}/#{d.day}/#{slug}/"
    end
 
    def full_url
      Blog.url_base.gsub(/\/$/, '') + url
    end
 
    def body_html
      to_html(body)
    end
 
    def summary
      summary, more = split_content(body)
      summary
    end
 
    def summary_html
      to_html(summary)
    end
 
    def more?
      summary, more = split_content(body) unless body.nil?
      more
    end
 
    ########
    def to_html(textile)
      RedCloth.new(textile).to_html
    end
 
    def split_content(string)
      parts = string.gsub(/\r/, '').split("\n\n")
      show = []
      hide = []
      parts.each do |part|
        if show.join.length < 100
          show << part
        else
          hide << part
        end
      end
      [ to_html(show.join("\n\n")), hide.size > 0 ]
    end
  end
end
 
Scanty::Post.create_table unless Scanty::Post.table_exists?