forked from semanticart/hoboken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.rb
32 lines (27 loc) · 859 Bytes
/
article.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
class Article
REP = '<+<<*>>+>'
include DataMapper::Resource
property :id, Serial
property :body, Text
property :title, String
property :slug, String
property :created_at, DateTime
property :updated_at, DateTime
has_tags_on :tags
is :versioned, :on => :updated_at
def auto_link
keeps = []
# replace everything currently within brackets with our constant
# and save the results
altered = body.gsub(/\[+.*?\]+/) do |match|
# save what we're replacing so we can put it back later
keeps << match
REP
end
# auto-link any articles
altered.gsub!(Regexp.new("(#{Article.all.map{|x| x.slug}.join("|")})"), "[[\\1]]")
# put our original already-bracketed items back
altered.gsub!(REP){|match| keeps.shift}
altered
end
end