-
Notifications
You must be signed in to change notification settings - Fork 12
/
plugin.rb
86 lines (68 loc) · 2.16 KB
/
plugin.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
# name: blog
# about: blog frontend for Discourse
# version: 0.1
# authors: Sam Saffron
# gem "multi_xml","0.5.5"
# gem "httparty", "0.12.0"
# TODO consider serel
# gem "serel", "1.2.0"
::BLOG_HOST = Rails.env.development? ? "dev.samsaffron.com" : "samsaffron.com"
::BLOG_DISCOURSE = Rails.env.development? ? "l.discourse" : "discuss.samsaffron.com"
module ::Blog
class Engine < ::Rails::Engine
engine_name "blog"
isolate_namespace Blog
end
end
Rails.configuration.assets.precompile += ['LAB.js', 'blog.css']
after_initialize do
# got to patch this class to allow more hostnames
class ::Middleware::EnforceHostname
def call(env)
hostname = env[Rack::Request::HTTP_X_FORWARDED_HOST].presence || env[Rack::HTTP_HOST]
env[Rack::Request::HTTP_X_FORWARDED_HOST] = nil
if hostname == ::BLOG_HOST
env[Rack::HTTP_HOST] = ::BLOG_HOST
else
env[Rack::HTTP_HOST] = ::BLOG_DISCOURSE
end
@app.call(env)
end
end
load File.expand_path("../app/jobs/blog_update_twitter.rb", __FILE__)
load File.expand_path("../app/jobs/blog_update_stackoverflow.rb", __FILE__)
require_dependency "plugin/filter"
Plugin::Filter.register(:after_post_cook) do |post, cooked|
if post.post_number == 1 && post.topic && post.topic.archetype == "regular"
split = cooked.split(/<hr\/?>/)
if split.length > 1
post.topic.custom_fields["summary"] = split[0]
post.topic.save unless post.topic.new_record?
cooked = split[1..-1].join("<hr>")
end
end
cooked
end
class BlogConstraint
def matches?(request)
request.host == BLOG_HOST
end
end
class ::Topic
before_save :blog_bake_summary
before_save :ensure_permalink
def ensure_permalink
unless custom_fields["permalink"]
custom_fields["permalink"] = (Time.now.strftime "/archive/%Y/%m/%d/") + self.slug
end
end
def blog_bake_summary
if summary = custom_fields["summary"]
custom_fields["cooked_summary"] = PrettyText.cook(summary)
end
end
end
Discourse::Application.routes.prepend do
mount ::Blog::Engine, at: "/", constraints: BlogConstraint.new
end
end