smallcage 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. data/.gitignore +1 -0
  2. data/.rubocop.yml +8 -0
  3. data/.travis.yml +1 -1
  4. data/Gemfile +16 -5
  5. data/Guardfile +4 -0
  6. data/History.rdoc +9 -5
  7. data/README.rdoc +3 -3
  8. data/Rakefile +1 -1
  9. data/bin/smc +4 -12
  10. data/lib/smallcage.rb +4 -1
  11. data/lib/smallcage/anonymous_loader.rb +32 -0
  12. data/lib/smallcage/application.rb +4 -5
  13. data/lib/smallcage/commands/auto.rb +16 -17
  14. data/lib/smallcage/commands/base.rb +4 -5
  15. data/lib/smallcage/commands/clean.rb +5 -5
  16. data/lib/smallcage/commands/export.rb +5 -5
  17. data/lib/smallcage/commands/import.rb +25 -27
  18. data/lib/smallcage/commands/manifest.rb +4 -4
  19. data/lib/smallcage/commands/server.rb +4 -4
  20. data/lib/smallcage/commands/update.rb +4 -5
  21. data/lib/smallcage/commands/uri.rb +3 -5
  22. data/lib/smallcage/document_path.rb +6 -9
  23. data/lib/smallcage/http_server.rb +9 -10
  24. data/lib/smallcage/loader.rb +19 -49
  25. data/lib/smallcage/misc.rb +1 -1
  26. data/lib/smallcage/renderer.rb +1 -1
  27. data/lib/smallcage/resources/auto.html +7 -7
  28. data/lib/smallcage/runner.rb +6 -6
  29. data/lib/smallcage/update_list.rb +2 -3
  30. data/lib/smallcage/version.rb +1 -1
  31. data/project/base/_smc/helpers/base_helper.rb +8 -9
  32. data/project/bluecloth/_smc/helpers/blue_cloth_helper.rb +2 -3
  33. data/project/cache/_smc/filters/cache_filter.rb +10 -12
  34. data/project/cache/_smc/rakelib/cache.rake +7 -8
  35. data/project/gdata/_smc/rakelib/gdata.rake +3 -5
  36. data/project/lang/_smc/helpers/lang_helper.rb +3 -3
  37. data/project/news/_smc/helpers/news_helper.rb +3 -4
  38. data/project/nkf/_smc/filters/nkf_filter.rb +6 -6
  39. data/project/nkf/_smc/helpers/nkf_helper.rb +8 -9
  40. data/project/rake/_smc/rakelib/svn.rake +5 -6
  41. data/project/redcloth/_smc/helpers/red_cloth_helper.rb +3 -3
  42. data/project/standard/_smc/helpers/menu_helper.rb +4 -5
  43. data/project/standard/_smc/templates/redirect.rhtml +1 -1
  44. data/spec/data/anonymous_loader/1/error1.rb +1 -0
  45. data/spec/data/anonymous_loader/1/error2.rb +1 -0
  46. data/spec/data/anonymous_loader/1/test1.rb +1 -0
  47. data/spec/data/anonymous_loader/1/test2.rb +1 -0
  48. data/spec/lib/smallcage/anonymous_loader_spec.rb +37 -0
  49. data/spec/lib/smallcage/application_spec.rb +271 -0
  50. data/spec/{smallcage → lib/smallcage}/commands/auto_spec.rb +0 -0
  51. data/spec/{export_spec.rb → lib/smallcage/commands/export_spec.rb} +8 -10
  52. data/spec/{import_spec.rb → lib/smallcage/commands/import_spec.rb} +5 -6
  53. data/spec/{manifest_spec.rb → lib/smallcage/commands/manifest_spec.rb} +4 -3
  54. data/spec/{smallcage → lib/smallcage}/commands/server_spec.rb +0 -0
  55. data/spec/{update_spec.rb → lib/smallcage/commands/update_spec.rb} +15 -19
  56. data/spec/{uri_spec.rb → lib/smallcage/commands/uri_spec.rb} +3 -4
  57. data/spec/{document_path_spec.rb → lib/smallcage/document_path_spec.rb} +19 -31
  58. data/spec/{loader_spec.rb → lib/smallcage/loader_spec.rb} +16 -18
  59. data/spec/lib/smallcage/misc_spec.rb +18 -0
  60. data/spec/{update_list_spec.rb → lib/smallcage/update_list_spec.rb} +18 -21
  61. data/spec/{smallcage_spec.rb → lib/smallcage_spec.rb} +1 -1
  62. data/spec/spec_helper.rb +5 -4
  63. metadata +85 -55
  64. checksums.yaml +0 -7
  65. data/Gemfile.lock +0 -32
  66. data/spec/misc_spec.rb +0 -25
  67. data/spec/smallcage/application_spec.rb +0 -271
@@ -3,7 +3,7 @@ module SmallCage::Commands
3
3
  def self.execute(opts)
4
4
  self.new(opts).execute
5
5
  end
6
-
6
+
7
7
  def initialize(opts)
8
8
  @opts = opts
9
9
  if @opts[:from] == "default"
@@ -11,21 +11,21 @@ module SmallCage::Commands
11
11
  end
12
12
  @project_dir = Pathname.new(__FILE__) + "../../../../project"
13
13
  end
14
-
14
+
15
15
  def qp(str = "")
16
16
  print str unless @opts[:quiet]
17
17
  end
18
-
18
+
19
19
  def qps(str = "")
20
20
  puts str unless @opts[:quiet]
21
21
  end
22
-
22
+
23
23
  def execute
24
24
  @dest = Pathname.new(@opts[:to])
25
-
25
+
26
26
  Dir.mkdir(@dest) unless @dest.exist?
27
27
  return unless @dest.directory?
28
-
28
+
29
29
  from = @opts[:from].split(/,/)
30
30
  from.each do |f|
31
31
  qps
@@ -38,7 +38,7 @@ module SmallCage::Commands
38
38
  end
39
39
  end
40
40
  end
41
-
41
+
42
42
  def import(from)
43
43
  d = @project_dir + from
44
44
  return unless d.directory?
@@ -48,7 +48,7 @@ module SmallCage::Commands
48
48
  end
49
49
  import_entries
50
50
  end
51
-
51
+
52
52
  def import_external
53
53
  @entries = external_entries
54
54
  unless @opts[:quiet]
@@ -69,7 +69,7 @@ module SmallCage::Commands
69
69
  else
70
70
  qps "? /" + e.path
71
71
  end
72
-
72
+
73
73
  begin
74
74
  e.import
75
75
  rescue
@@ -77,7 +77,7 @@ module SmallCage::Commands
77
77
  qps "F /" + e.path
78
78
  end
79
79
  end
80
-
80
+
81
81
  unless failed.empty?
82
82
  qps "FAILED:"
83
83
  failed.each do |e|
@@ -101,16 +101,16 @@ module SmallCage::Commands
101
101
  return result
102
102
  end
103
103
  private :local_entries
104
-
104
+
105
105
  def external_entries(uri)
106
106
  if uri !~ %r{/$}
107
107
  uri += "/"
108
108
  end
109
109
  mfuri = uri + "Manifest.html"
110
-
110
+
111
111
  source = open(mfuri) {|io| io.read }
112
112
  result = []
113
-
113
+
114
114
  files = source.scan(%r{<li><a href="(./[^"]+)">(./[^<]+)</a></li>}) #"
115
115
  files.each do |f|
116
116
  raise "illegal path:#{f[0]},#{f[1]}" if f[0] != f[1]
@@ -122,14 +122,14 @@ module SmallCage::Commands
122
122
  e.to = @dest + path
123
123
  result << e
124
124
  end
125
-
125
+
126
126
  return result
127
127
  end
128
128
  private :external_entries
129
-
129
+
130
130
  def confirm_entries
131
131
  overwrite = []
132
-
132
+
133
133
  qps "Create:"
134
134
  @entries.each do |e|
135
135
  if e.overwrite?
@@ -139,7 +139,7 @@ module SmallCage::Commands
139
139
  end
140
140
  end
141
141
  qps
142
-
142
+
143
143
  unless overwrite.empty?
144
144
  qps "Overwrite:"
145
145
  overwrite.each do |e|
@@ -147,11 +147,11 @@ module SmallCage::Commands
147
147
  end
148
148
  qps
149
149
  end
150
-
150
+
151
151
  return y_or_n("Import these files?[Yn]: ", true)
152
152
  end
153
153
  private :confirm_entries
154
-
154
+
155
155
  def y_or_n(prompt, default)
156
156
  loop do
157
157
  print prompt
@@ -166,8 +166,7 @@ module SmallCage::Commands
166
166
  end
167
167
  end
168
168
  private :y_or_n
169
-
170
-
169
+
171
170
  class ImportEntry
172
171
  attr_accessor :path, :from, :to
173
172
 
@@ -178,19 +177,19 @@ module SmallCage::Commands
178
177
  copy_local
179
178
  end
180
179
  end
181
-
180
+
182
181
  def external?
183
182
  from.to_s =~ %r{^https?://}
184
183
  end
185
-
184
+
186
185
  def exist?
187
186
  to.exist?
188
187
  end
189
-
188
+
190
189
  def overwrite?
191
190
  to.file?
192
191
  end
193
-
192
+
194
193
  def copy_local
195
194
  if from.directory?
196
195
  FileUtils.makedirs(to)
@@ -200,7 +199,7 @@ module SmallCage::Commands
200
199
  end
201
200
  end
202
201
  private :copy_local
203
-
202
+
204
203
  def copy_external
205
204
  if from =~ %r{/$}
206
205
  FileUtils.makedirs(to)
@@ -211,7 +210,6 @@ module SmallCage::Commands
211
210
  end
212
211
  end
213
212
  private :copy_external
214
-
215
213
  end
216
214
  end
217
215
  end
@@ -3,11 +3,11 @@ module SmallCage::Commands
3
3
  def self.execute(opts)
4
4
  self.new(opts).execute
5
5
  end
6
-
6
+
7
7
  def initialize(opts)
8
8
  @opts = opts
9
9
  end
10
-
10
+
11
11
  def execute
12
12
  entries = []
13
13
  root = Pathname.new(@opts[:path])
@@ -16,7 +16,7 @@ module SmallCage::Commands
16
16
  entries << f
17
17
  end
18
18
  end
19
-
19
+
20
20
  tmp = []
21
21
  entries.each do |f|
22
22
  path = root + f
@@ -28,7 +28,7 @@ module SmallCage::Commands
28
28
  tmp << f
29
29
  end
30
30
  entries = tmp
31
-
31
+
32
32
  template = File.dirname(__FILE__) + "/../resources/Manifest.erb"
33
33
  source = ERB.new(File.read(template), nil, "-").result(binding)
34
34
  open(root + "Manifest.html", "w") do |io|
@@ -3,13 +3,13 @@ module SmallCage::Commands
3
3
  def self.execute(opts)
4
4
  document_root = opts[:path]
5
5
  port = opts[:port]
6
-
6
+
7
7
  server = SmallCage::HTTPServer.new(document_root, port)
8
-
8
+
9
9
  sighandler = Proc.new {|signal| server.shutdown}
10
10
  SmallCage::Application.add_signal_handler(["INT", "TERM"], sighandler)
11
11
 
12
- server.start
12
+ server.start
13
13
  end
14
14
  end
15
- end
15
+ end
@@ -5,18 +5,18 @@ module SmallCage::Commands
5
5
  def self.execute(opts)
6
6
  self.new(opts).execute
7
7
  end
8
-
8
+
9
9
  def initialize(opts)
10
10
  @opts = opts
11
11
  end
12
-
12
+
13
13
  def execute
14
14
  start = Time.now
15
15
  target = Pathname.new(@opts[:path])
16
16
  unless target.exist?
17
17
  raise "target directory or file does not exist.: " + target.to_s
18
18
  end
19
-
19
+
20
20
  @loader = Loader.new(target)
21
21
  @renderer = Renderer.new(@loader)
22
22
  @list = UpdateList.create(@loader.root, target)
@@ -91,7 +91,7 @@ module SmallCage::Commands
91
91
  end
92
92
  end
93
93
  private :render_multi
94
-
94
+
95
95
  def after_rendering_filters(obj, result)
96
96
  filters = @loader.filters("after_rendering_filters")
97
97
  filters.each do |f|
@@ -107,6 +107,5 @@ module SmallCage::Commands
107
107
  end
108
108
  end
109
109
  private :output_result
110
-
111
110
  end
112
111
  end
@@ -4,17 +4,17 @@ module SmallCage::Commands
4
4
  def self.execute(opts)
5
5
  self.new(opts).execute
6
6
  end
7
-
7
+
8
8
  def initialize(opts)
9
9
  @opts = opts
10
10
  end
11
-
11
+
12
12
  def execute
13
13
  target = Pathname.new(@opts[:path])
14
14
  unless target.exist?
15
15
  raise "target directory or file does not exist.: " + target.to_s
16
16
  end
17
-
17
+
18
18
  @loader = SmallCage::Loader.new(target)
19
19
  @renderer = SmallCage::Renderer.new(@loader)
20
20
  print_uris
@@ -38,7 +38,6 @@ module SmallCage::Commands
38
38
  private :print_default_or_template_uris
39
39
 
40
40
  def print_uri_templates(obj, uris)
41
-
42
41
  uris = uris.map {|uri| uri.strip }
43
42
  base = obj['path'].parent
44
43
  uris.each_with_index do |uri, index|
@@ -51,6 +50,5 @@ module SmallCage::Commands
51
50
  end
52
51
  end
53
52
  private :print_uri_templates
54
-
55
53
  end
56
54
  end
@@ -1,8 +1,7 @@
1
1
  module SmallCage
2
2
  class DocumentPath
3
-
4
3
  attr_reader :root, :uri, :path
5
-
4
+
6
5
  def initialize(root, path)
7
6
  @root = Pathname.new(root).realpath
8
7
 
@@ -12,27 +11,27 @@ module SmallCage
12
11
  else
13
12
  @path = @path.cleanpath
14
13
  end
15
-
14
+
16
15
  if @path.to_s[0...@root.to_s.length] != @root.to_s
17
16
  raise "Illegal path: #{path.to_s}"
18
17
  end
19
-
18
+
20
19
  if @path == @root
21
20
  @uri = "/"
22
21
  else
23
22
  @uri = @path.to_s[@root.to_s.length .. -1]
24
23
  end
25
24
  end
26
-
25
+
27
26
  def smc?
28
27
  return @path.extname == ".smc"
29
28
  end
30
-
29
+
31
30
  def outfile
32
31
  return nil unless smc?
33
32
  return self.class.new(@root, @path.to_s[0 .. -5])
34
33
  end
35
-
34
+
36
35
  def outuri
37
36
  return nil unless smc?
38
37
  return uri[0 .. -5]
@@ -64,7 +63,5 @@ module SmallCage
64
63
 
65
64
  return obj
66
65
  end
67
-
68
-
69
66
  end
70
67
  end
@@ -2,7 +2,7 @@ require 'webrick'
2
2
 
3
3
  module SmallCage
4
4
  class HTTPServer
5
-
5
+
6
6
  def initialize(document_root, port)
7
7
  # logger = WEBrick::Log.new(nil, 1)
8
8
  @server = WEBrick::HTTPServer.new({
@@ -17,19 +17,19 @@ module SmallCage
17
17
  @server.mount("/_smc/update_uri", UpdateUriServlet)
18
18
  @server.mount("/_smc/auto", AutoServlet)
19
19
  end
20
-
20
+
21
21
  def start
22
22
  @server.start
23
23
  end
24
-
24
+
25
25
  def shutdown
26
26
  @server.shutdown
27
27
  end
28
-
28
+
29
29
  def updated_uri=(uri)
30
30
  UpdateUriServlet.uri = uri
31
31
  end
32
-
32
+
33
33
  def reload
34
34
  UpdateUriServlet.uri = ":reload"
35
35
  end
@@ -39,21 +39,20 @@ module SmallCage
39
39
  class UpdateUriServlet < WEBrick::HTTPServlet::AbstractServlet
40
40
  @@uri = "/index.html"
41
41
  @@update_time = ""
42
-
42
+
43
43
  def do_GET(req, res)
44
44
  res['content-type'] = "text/plain"
45
45
  res.body = @@uri + "\n" + @@update_time
46
46
  end
47
-
47
+
48
48
  def self.uri=(uri)
49
49
  @@uri = uri
50
50
  update_time
51
51
  end
52
-
52
+
53
53
  def self.update_time
54
54
  @@update_time = Time.now.to_s
55
55
  end
56
-
57
56
  end
58
57
 
59
58
  class AutoServlet < WEBrick::HTTPServlet::AbstractServlet
@@ -63,4 +62,4 @@ module SmallCage
63
62
  res.body = File.read(html)
64
63
  end
65
64
  end
66
- end
65
+ end
@@ -6,12 +6,12 @@ module SmallCage
6
6
  DIR_PROP_FILE = "_dir.smc"
7
7
  LOCAL_PROP_FILE = "_local.smc"
8
8
  MAX_DEPTH = 100
9
-
9
+
10
10
  attr_reader :root, :target, :erb_base
11
-
11
+
12
12
  def initialize(target)
13
13
  target = Pathname.new(target.to_s.strip.chomp('/'))
14
- target = real_target(target)
14
+ target = real_target(target)
15
15
 
16
16
  @target = target # absolute
17
17
  @root = self.class.find_root(target) # absolute
@@ -34,7 +34,7 @@ module SmallCage
34
34
  end
35
35
  raise "Root not found: #{path}"
36
36
  end
37
-
37
+
38
38
  def load(path)
39
39
  raise "Not found: #{path}" unless path.exist?
40
40
 
@@ -42,7 +42,7 @@ module SmallCage
42
42
 
43
43
  if path.file?
44
44
  return load_smc_file(docpath)
45
- else
45
+ else
46
46
  return load_dir_prop(docpath)
47
47
  end
48
48
  end
@@ -52,7 +52,7 @@ module SmallCage
52
52
 
53
53
  result = create_base_smc_object(docpath.outfile.path, docpath.path,
54
54
  docpath.outuri, docpath.uri)
55
-
55
+
56
56
  result["template"] = DEFAULT_TEMPLATE
57
57
  result["dirs"] = load_dirs(docpath.path)
58
58
 
@@ -82,7 +82,7 @@ module SmallCage
82
82
  return result
83
83
  end
84
84
  private :load_dir_prop
85
-
85
+
86
86
  def create_base_smc_object(path_out, path_smc, uri_out, uri_smc)
87
87
  result = {}
88
88
  result["arrays"] = []
@@ -139,7 +139,7 @@ module SmallCage
139
139
  return nil unless result.file?
140
140
  return result
141
141
  end
142
-
142
+
143
143
  def each_smc_obj
144
144
  each_smc_file do |path|
145
145
  next if path.directory?
@@ -149,7 +149,7 @@ module SmallCage
149
149
  yield obj
150
150
  end
151
151
  end
152
-
152
+
153
153
  def each_smc_file
154
154
  if @target.directory?
155
155
  path = Pathname.new(@target)
@@ -162,7 +162,7 @@ module SmallCage
162
162
  yield @target
163
163
  end
164
164
  end
165
-
165
+
166
166
  def each_not_smc_file
167
167
  if @target.directory?
168
168
  path = Pathname.new(@target)
@@ -181,10 +181,10 @@ module SmallCage
181
181
  yield DocumentPath.new(@root, @target)
182
182
  end
183
183
  end
184
-
184
+
185
185
  def real_target(target)
186
186
  return target.realpath if target.directory?
187
- return target.realpath if target.file? and target.to_s =~ /\.smc$/
187
+ return target.realpath if target.file? and target.to_s =~ /\.smc$/
188
188
 
189
189
  tmp = Pathname.new(target.to_s + ".smc")
190
190
  return tmp.realpath if tmp.file?
@@ -192,8 +192,7 @@ module SmallCage
192
192
  raise "Target not found: " + target.to_s
193
193
  end
194
194
  private :real_target
195
-
196
-
195
+
197
196
  def load_erb_base
198
197
  result = Class.new(ErbBase)
199
198
  class << result
@@ -205,41 +204,13 @@ module SmallCage
205
204
  end
206
205
  end
207
206
  end
208
-
209
- helpers = load_anonymous(@helpers_dir, %r{([^/]+_helper)\.rb$})
207
+
208
+ helpers = SmallCage::AnonymousLoader.load(@helpers_dir, %r{([^/]+_helper)\.rb\z})
210
209
  result.include_helpers(helpers[:module], helpers[:names])
211
210
 
212
211
  return result
213
212
  end
214
213
  private :load_erb_base
215
-
216
- def load_anonymous(dir, rex)
217
- module_names = []
218
- mod = Module.new
219
- result = { :module => mod, :names => module_names }
220
-
221
- return result unless File.exist?(dir)
222
-
223
- Dir.entries(dir).sort.each do |h|
224
- next unless h =~ rex
225
-
226
- # create anonymous module.
227
- module_name = $1.camelize
228
-
229
- src = File.read("#{dir}/#{h}")
230
- begin
231
- mod.module_eval(src, "#{dir}/#{h}")
232
- rescue => ex
233
- STDERR << ex.to_s # TODO show error
234
- load("#{dir}/#{h}", true) # try to know error line number.
235
- raise "Can't load #{dir}/#{h} / line# unknown"
236
- end
237
- module_names << module_name
238
- end
239
-
240
- return result
241
- end
242
- private :load_anonymous
243
214
 
244
215
  def filters(name)
245
216
  if @filters[name].nil?
@@ -247,14 +218,14 @@ module SmallCage
247
218
  end
248
219
  return @filters[name]
249
220
  end
250
-
221
+
251
222
  def load_filters
252
223
  result = {}
253
224
  return {} unless @filters_dir.directory?
254
-
255
- filter_modules = load_anonymous(@filters_dir, %r{([^/]+_filter)\.rb$})
225
+
226
+ filter_modules = SmallCage::AnonymousLoader.load(@filters_dir, %r{([^/]+_filter)\.rb\z})
256
227
  smc_module = filter_modules[:module].const_get("SmallCage")
257
-
228
+
258
229
  load_filters_config.each do |filter_type,filter_list|
259
230
  result[filter_type] = []
260
231
  filter_list.to_a.each do |fc|
@@ -273,6 +244,5 @@ module SmallCage
273
244
  return YAML.load(path.read()) || {}
274
245
  end
275
246
  private :load_filters_config
276
-
277
247
  end
278
248
  end