/
post.rb
80 lines (67 loc) · 1.44 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
require File.dirname(__FILE__) + '/../vendor/maruku/maruku'
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/syntax'
require 'syntax/convertors/html'
class Post < Sequel::Model
unless table_exists?
set_schema do
primary_key :id
text :title
text :body
text :slug
text :tags
timestamp :created_at
end
create_table
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 ||= body.match(/(.{200}.*?\n)/m)
@summary || body
end
def summary_html
to_html(summary.to_s)
end
def more?
@more ||= body.match(/.{200}.*?\n(.*)/m)
@more
end
def linked_tags
tags.split.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(/<code>([^<]+)<\/code>/m) do
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
highlighted = convertor.convert($1)
"<code>#{highlighted}</code>"
end
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