forked from adamwiggins/scanty
/
post.rb
123 lines (104 loc) · 2.64 KB
/
post.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
#require File.dirname(__FILE__) + '/../vendor/couchrest/couchrest'
require File.dirname(__FILE__) + '/../vendor/maruku/maruku'
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/syntax'
require 'syntax/convertors/html'
class Post < CouchRest::Model
use_database CouchRest.database!((Blog.url_base_database || '') + Blog.database_name)
key_accessor :title, :body, :slug, :tags, :not_public
view_by :created_at, :descending=>true
view_by :slug
view_by :not_public
view_by :created_at_and_public, :descending=>true,
:map =>
"function(doc) {
if(doc['couchrest-type'] == 'Post' && !doc['not_public']) {
emit(doc['created_at'],1);
}
}",
:reduce =>
"function(keys, values, rereduce) {
return sum(values);
}"
view_by :tags,
:map =>
"function(doc) {
if (doc['couchrest-type'] == 'Post' && doc['tags']) {
doc['tags'].forEach(function(tag){
emit(tag, 1);
});
}
}",
:reduce =>
"function(keys, values, rereduce) {
return sum(values);
}"
view_by :tags_and_public,
:map =>
"function(doc) {
if (doc['couchrest-type'] == 'Post' && doc['tags'] && !doc['not_public']) {
doc['tags'].forEach(function(tag){
emit(tag, 1);
});
}
}",
:reduce =>
"function(keys, values, rereduce) {
return sum(values);
}"
timestamps!
couchrest_type = 'Post'
def created_at
unless self['created_at'].nil?
self['created_at'] = Time.parse(self['created_at']) unless self['created_at'].respond_to?('year')
end
self['created_at']
end
def url
d = self.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(self['body'])
end
def summary
summary, more = split_content(self['body'])
summary
end
def summary_html
to_html(summary)
end
def more?
summary, more = split_content(self['body'])
more
end
def linked_tags
self['tags'].inject([]) do |accum, tag|
accum << "<a href=\"/past/tags/#{tag}\">#{tag}</a>"
end.join(" ")
end
def self.make_slug(title)
title.downcase.gsub(/ /, '_').gsub(/[^a-z0-9_]/, '').squeeze('_')
end
########
def to_html(markdown)
h = Maruku.new(markdown).to_html
h.gsub!(/<pre>/, "<pre class=\"prettyprint\">")
h
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