shinmun 0.2 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,135 +0,0 @@
1
- require 'rack'
2
-
3
- module Shinmun
4
-
5
- class RackAdapter
6
-
7
- def initialize(blog)
8
- @blog = blog
9
- @routing = []
10
-
11
- map /\.rss$/, FeedController
12
- map /^\/\d\d\d\d\/\d+/, PostController
13
- map /^\/categories/, CategoryController
14
- map /^\/comments/, CommentsController
15
- map //, PageController
16
- end
17
-
18
- def map(pattern, controller)
19
- @routing << [pattern, controller]
20
- end
21
-
22
- def call(env)
23
- request = Rack::Request.new(env)
24
- response = Rack::Response.new
25
-
26
- @blog.reload
27
-
28
- klass = find_controller(request.path_info)
29
-
30
- controller = klass.new(@blog, request, response)
31
- controller.handle_request
32
-
33
- response.status ||= 200
34
- response.finish
35
- end
36
-
37
- def find_controller(path)
38
- for pattern, klass in @routing
39
- return klass if pattern.match(path)
40
- end
41
- end
42
-
43
- end
44
-
45
- class Controller
46
- attr_reader :blog, :request, :response, :path, :extname
47
-
48
- def initialize(blog, request, response)
49
- @blog = blog
50
- @request = request
51
- @response = response
52
- @extname = File.extname(request.path_info)
53
- @path = request.path_info[1..-1].chomp(@extname)
54
- end
55
-
56
- def params
57
- request.params
58
- end
59
-
60
- def redirect_to(location)
61
- response.headers["Location"] = location
62
- response.status = 302
63
- end
64
-
65
- def handle_request
66
- action = request.request_method.downcase
67
- response.body = send(action) if self.class.public_instance_methods.include?(action)
68
- end
69
- end
70
-
71
- class PageController < Controller
72
- def get
73
- if path.empty?
74
- blog.render_index_page
75
- else
76
- page = blog.find_page(path) or raise "#{path} not found"
77
- blog.render_page(page)
78
- end
79
- end
80
- end
81
-
82
- class PostController < PageController
83
- def get
84
- if post = blog.find_post(path)
85
- blog.render_post(post)
86
- else
87
- year, month, = path.split('/')
88
- blog.render_month(year.to_i, month.to_i)
89
- end
90
- end
91
- end
92
-
93
- class FeedController < Controller
94
- def get
95
- path_list = path.split('/')
96
- case path_list[0]
97
- when 'categories'
98
- category = blog.find_category(path_list[1].chomp('.rss'))
99
- blog.render_category_feed(category)
100
- when 'index'
101
- blog.render_index_feed
102
- end
103
- end
104
- end
105
-
106
- class CategoryController < Controller
107
- def get
108
- category = blog.find_category(path.split('/')[1].chomp('.html'))
109
- blog.render_category(category)
110
- end
111
- end
112
-
113
- class CommentsController < Controller
114
-
115
- def post
116
- params['path'].include?('..') and raise 'invalid path'
117
-
118
- comment = Comment.new(:time => Time.now,
119
- :name => params['name'],
120
- :email => params['email'],
121
- :website => params['website'],
122
- :text => params['text'])
123
-
124
- if params['preview'] == 'true'
125
- blog.render_comments([comment])
126
- else
127
- Comment.write(params['path'], comment)
128
- blog.render_comments(Comment.read(params['path']))
129
- end
130
- end
131
-
132
- end
133
-
134
- end
135
-
@@ -1,39 +0,0 @@
1
- module Shinmun
2
-
3
- module Helpers
4
- end
5
-
6
- # This class renders an ERB template for a set of attributes, which
7
- # are accessible as instance variables.
8
- class Template
9
- include Helpers
10
-
11
- attr_reader :erb, :blog
12
-
13
- # Initialize this template with an ERB instance.
14
- def initialize(erb, file)
15
- @erb = erb
16
- @file = file
17
- end
18
-
19
- # Set instance variable for this template.
20
- def set_variables(vars)
21
- for name, value in vars
22
- instance_variable_set("@#{name}", value)
23
- end
24
- self
25
- end
26
-
27
- # Render this template.
28
- def render
29
- @erb.result(binding)
30
- rescue => e
31
- e.backtrace.each do |s|
32
- s.gsub!('(erb)', @file)
33
- end
34
- raise e
35
- end
36
-
37
- end
38
-
39
- end