smallcage 0.0.8 → 0.0.9
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.
- data/History.txt +10 -0
- data/Manifest.txt +23 -7
- data/bin/smc +6 -126
- data/lib/smallcage.rb +4 -8
- data/lib/smallcage/application.rb +159 -0
- data/lib/smallcage/commands/auto.rb +133 -27
- data/lib/smallcage/commands/base.rb +19 -0
- data/lib/smallcage/commands/clean.rb +10 -2
- data/lib/smallcage/commands/export.rb +41 -0
- data/lib/smallcage/commands/import.rb +12 -7
- data/lib/smallcage/commands/manifest.rb +1 -1
- data/lib/smallcage/commands/server.rb +5 -14
- data/lib/smallcage/commands/update.rb +92 -11
- data/lib/smallcage/document_path.rb +46 -0
- data/lib/smallcage/http_server.rb +66 -0
- data/lib/smallcage/loader.rb +37 -26
- data/lib/smallcage/{commands → resources}/Manifest.erb +0 -0
- data/lib/smallcage/resources/auto.html +119 -0
- data/lib/smallcage/runner.rb +15 -0
- data/lib/smallcage/version.rb +1 -1
- data/project/{default → base}/_smc/helpers/base_helper.rb +0 -0
- data/project/{default → base}/_smc/helpers/site_helper.rb +0 -0
- data/project/{default → base}/_smc/templates/default.rhtml +0 -0
- data/project/{default → base}/_smc/templates/footer.rhtml +0 -0
- data/project/{default → base}/_smc/templates/header.rhtml +0 -0
- data/project/lang/_smc/helpers/lang_helper.rb +19 -0
- data/project/lang/_smc/templates/other_lang.rhtml +6 -0
- data/project/redcloth/_smc/helpers/red_cloth_helper.rb +14 -0
- data/project/redcloth/_smc/templates/markdown.rhtml +5 -0
- data/project/redcloth/_smc/templates/textile.rhtml +5 -0
- data/project/standard/_smc/helpers/menu_helper.rb +2 -2
- data/project/standard/about/index.html.smc +1 -1
- data/project/standard/index.html.smc +1 -1
- data/project/standard/sample1/index.html.smc +1 -1
- data/project/standard/sample1/index2.html.smc +1 -1
- data/project/standard/sample2/index.html.smc +1 -1
- data/spec/data/htdocs1/a/b/c/index.html.smc +6 -1
- data/{log/debug.log → spec/data/htdocs2/_smc/templates/dummy.rhtml} +0 -0
- data/spec/data/htdocs2/a/b/c/test.html +1 -0
- data/spec/data/htdocs2/a/b/test.html +1 -0
- data/spec/data/htdocs2/a/test.html.smc +2 -0
- data/spec/document_path_spec.rb +42 -0
- data/spec/export_spec.rb +45 -0
- data/spec/loader_spec.rb +20 -6
- data/spec/manifest_spec.rb +18 -11
- data/spec/smallcage_spec.rb +17 -11
- metadata +25 -9
@@ -16,8 +16,16 @@ module SmallCage::Commands
|
|
16
16
|
|
17
17
|
loader = SmallCage::Loader.new(target)
|
18
18
|
loader.each_smc_obj do |obj|
|
19
|
-
obj["path"].
|
20
|
-
|
19
|
+
if obj["path"].exist?
|
20
|
+
obj["path"].delete
|
21
|
+
puts "D " + obj["uri"] unless @opts[:quiet]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
tmpdir = loader.root + "./_smc/tmp"
|
26
|
+
if tmpdir.exist?
|
27
|
+
FileUtils.rm_r(tmpdir)
|
28
|
+
puts "D /_smc/tmp" unless @opts[:quiet]
|
21
29
|
end
|
22
30
|
end
|
23
31
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module SmallCage::Commands
|
2
|
+
class Export < SmallCage::Commands::Base
|
3
|
+
def execute
|
4
|
+
target = Pathname.new(@opts[:path])
|
5
|
+
unless target.exist?
|
6
|
+
raise target.to_s + " does not exist."
|
7
|
+
end
|
8
|
+
|
9
|
+
loader = SmallCage::Loader.new(target)
|
10
|
+
root = loader.root
|
11
|
+
|
12
|
+
if @opts[:out].nil?
|
13
|
+
out = root + ("./_smc/tmp/export/" + Time.now.strftime("%Y%m%d%H%M%S"))
|
14
|
+
else
|
15
|
+
out = Pathname.new(@opts[:out])
|
16
|
+
end
|
17
|
+
if out.exist?
|
18
|
+
raise out.to_s + " already exist."
|
19
|
+
end
|
20
|
+
FileUtils.makedirs(out)
|
21
|
+
out = out.realpath
|
22
|
+
|
23
|
+
# TODO create empty directories
|
24
|
+
loader.each_not_smc_file do |docpath|
|
25
|
+
dir = Pathname.new(docpath.uri).parent
|
26
|
+
outdir = out + ("." + dir.to_s)
|
27
|
+
FileUtils.makedirs(outdir)
|
28
|
+
FileUtils.cp(docpath.path, outdir)
|
29
|
+
puts "A " + docpath.uri unless quiet?
|
30
|
+
end
|
31
|
+
|
32
|
+
unless quiet?
|
33
|
+
puts ""
|
34
|
+
puts "All contents exported to:"
|
35
|
+
puts " #{out.to_s}"
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -6,6 +6,9 @@ module SmallCage::Commands
|
|
6
6
|
|
7
7
|
def initialize(opts)
|
8
8
|
@opts = opts
|
9
|
+
if @opts[:from] == "default"
|
10
|
+
@opts[:from] = "base,standard"
|
11
|
+
end
|
9
12
|
@project_dir = Pathname.new(__FILE__) + "../../../../project"
|
10
13
|
end
|
11
14
|
|
@@ -58,18 +61,20 @@ module SmallCage::Commands
|
|
58
61
|
failed = []
|
59
62
|
@entries.each do |e|
|
60
63
|
if e.overwrite?
|
61
|
-
|
64
|
+
qps "M /" + e.path
|
62
65
|
elsif ! e.exist?
|
63
|
-
|
66
|
+
qps "A /" + e.path
|
67
|
+
elsif e.to.directory?
|
68
|
+
# nothing
|
64
69
|
else
|
65
|
-
|
70
|
+
qps "? /" + e.path
|
66
71
|
end
|
67
|
-
|
72
|
+
|
68
73
|
begin
|
69
74
|
e.import
|
70
75
|
rescue
|
71
76
|
failed << e
|
72
|
-
qps "F " + e.path
|
77
|
+
qps "F /" + e.path
|
73
78
|
end
|
74
79
|
end
|
75
80
|
|
@@ -130,7 +135,7 @@ module SmallCage::Commands
|
|
130
135
|
if e.overwrite?
|
131
136
|
overwrite << e
|
132
137
|
elsif ! e.exist?
|
133
|
-
qps " " + e.path
|
138
|
+
qps " /" + e.path
|
134
139
|
end
|
135
140
|
end
|
136
141
|
qps
|
@@ -138,7 +143,7 @@ module SmallCage::Commands
|
|
138
143
|
unless overwrite.empty?
|
139
144
|
qps "Overwrite:"
|
140
145
|
overwrite.each do |e|
|
141
|
-
qps " " + e.path
|
146
|
+
qps " /" + e.path
|
142
147
|
end
|
143
148
|
qps
|
144
149
|
end
|
@@ -29,7 +29,7 @@ module SmallCage::Commands
|
|
29
29
|
end
|
30
30
|
entries = tmp
|
31
31
|
|
32
|
-
template = File.dirname(__FILE__) + "/Manifest.erb"
|
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|
|
35
35
|
io << source
|
@@ -1,24 +1,15 @@
|
|
1
|
-
require 'webrick'
|
2
|
-
|
3
1
|
module SmallCage::Commands
|
4
2
|
class Server
|
5
3
|
def self.execute(opts)
|
6
4
|
document_root = opts[:path]
|
7
5
|
port = opts[:port]
|
8
|
-
|
9
|
-
server = WEBrick::HTTPServer.new({
|
10
|
-
:DocumentRoot => document_root,
|
11
|
-
:Port => port
|
12
|
-
})
|
13
|
-
|
14
|
-
WEBrick::HTTPServlet::FileHandler.remove_handler("cgi")
|
15
|
-
WEBrick::HTTPServlet::FileHandler.remove_handler("rhtml")
|
16
6
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
7
|
+
server = SmallCage::HTTPServer.new(document_root, port)
|
8
|
+
|
9
|
+
sighandler = Proc.new {|signal| server.shutdown}
|
10
|
+
SmallCage::Application.add_signal_handler(["INT", "TERM"], sighandler)
|
21
11
|
|
12
|
+
server.start
|
22
13
|
end
|
23
14
|
end
|
24
15
|
end
|
@@ -14,22 +14,103 @@ module SmallCage::Commands
|
|
14
14
|
raise "target directory or file does not exist.: " + target.to_s
|
15
15
|
end
|
16
16
|
|
17
|
-
loader = SmallCage::Loader.new(target)
|
18
|
-
renderer = SmallCage::Renderer.new(loader)
|
17
|
+
@loader = SmallCage::Loader.new(target)
|
18
|
+
@renderer = SmallCage::Renderer.new(@loader)
|
19
|
+
|
20
|
+
urilist = render_smc_files
|
21
|
+
if list_file.exist?
|
22
|
+
urilist = delete_expired_files(urilist)
|
23
|
+
end
|
24
|
+
save_list(urilist)
|
25
|
+
end
|
26
|
+
|
27
|
+
def render_smc_files
|
28
|
+
urilist = []
|
29
|
+
@loader.each_smc_obj do |obj|
|
30
|
+
urilist << obj["uri"].smc
|
31
|
+
mark = obj["path"].exist? ? "U " : "A "
|
32
|
+
render_smc_obj(obj)
|
33
|
+
puts mark + obj["uri"] unless @opts[:quiet]
|
34
|
+
end
|
35
|
+
return urilist
|
36
|
+
end
|
37
|
+
private :render_smc_files
|
38
|
+
|
39
|
+
def render_smc_obj(obj)
|
40
|
+
result = @renderer.render(obj["template"], obj)
|
41
|
+
result = after_rendering_filters(obj, result)
|
42
|
+
output_result(obj, result)
|
43
|
+
end
|
44
|
+
private :render_smc_obj
|
45
|
+
|
46
|
+
def delete_expired_files(urilist)
|
47
|
+
old_urilist = load_list
|
48
|
+
root = @loader.root
|
49
|
+
target_uri = SmallCage::DocumentPath.new(root, @loader.target).uri
|
50
|
+
|
51
|
+
if @loader.target.file?
|
52
|
+
old_urilist << target_uri unless old_urilist.include?(target_uri)
|
53
|
+
return old_urilist
|
54
|
+
end
|
19
55
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
56
|
+
target_uris = []
|
57
|
+
not_target_uris = []
|
58
|
+
old_urilist.each do |uri|
|
59
|
+
if uri.index(target_uri) == 0
|
60
|
+
target_uris << uri
|
61
|
+
else
|
62
|
+
not_target_uris << uri
|
26
63
|
end
|
64
|
+
end
|
65
|
+
|
66
|
+
deletelist = target_uris - urilist
|
27
67
|
|
28
|
-
|
29
|
-
|
68
|
+
deletelist.each do |uri|
|
69
|
+
delfile = SmallCage::DocumentPath.new(root, root + ("." + uri)).outfile
|
70
|
+
next unless delfile.path.file?
|
71
|
+
|
72
|
+
File.delete(delfile.path)
|
73
|
+
puts "D #{delfile.uri}" unless @opts[:quiet]
|
74
|
+
end
|
75
|
+
|
76
|
+
return (not_target_uris + urilist).sort
|
77
|
+
end
|
78
|
+
private :delete_expired_files
|
79
|
+
|
80
|
+
def after_rendering_filters(obj, result)
|
81
|
+
filters = @loader.filters("after_rendering_filters")
|
82
|
+
filters.each do |f|
|
83
|
+
result = f.after_rendering_filter(obj, result)
|
84
|
+
end
|
85
|
+
return result
|
86
|
+
end
|
87
|
+
private :after_rendering_filters
|
88
|
+
|
89
|
+
def save_list(urilist)
|
90
|
+
f = list_file
|
91
|
+
FileUtils.makedirs(f.parent)
|
92
|
+
open(f, "w") do |io|
|
93
|
+
io << "version: " + SmallCage::VERSION::STRING + "\n"
|
94
|
+
urilist.each do |u|
|
95
|
+
io << u + "\n"
|
96
|
+
end
|
30
97
|
end
|
31
98
|
end
|
99
|
+
private :save_list
|
32
100
|
|
101
|
+
def load_list
|
102
|
+
txt = File.read(list_file)
|
103
|
+
result = txt.split(/\n/)
|
104
|
+
result.shift
|
105
|
+
return result
|
106
|
+
end
|
107
|
+
private :load_list
|
108
|
+
|
109
|
+
def list_file
|
110
|
+
@loader.root + "./_smc/tmp/list.txt"
|
111
|
+
end
|
112
|
+
private :list_file
|
113
|
+
|
33
114
|
def output_result(obj, str)
|
34
115
|
open(obj["path"], "w") do |io|
|
35
116
|
io << str
|
@@ -37,4 +118,4 @@ module SmallCage::Commands
|
|
37
118
|
end
|
38
119
|
private :output_result
|
39
120
|
end
|
40
|
-
end
|
121
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module SmallCage
|
2
|
+
class DocumentPath
|
3
|
+
|
4
|
+
attr_reader :root, :uri, :path
|
5
|
+
|
6
|
+
def initialize(root, path)
|
7
|
+
@root = Pathname.new(root).realpath;
|
8
|
+
|
9
|
+
@path = Pathname.new(path)
|
10
|
+
if @path.exist?
|
11
|
+
@path = @path.realpath
|
12
|
+
else
|
13
|
+
@path = @path.cleanpath
|
14
|
+
end
|
15
|
+
|
16
|
+
if @path.to_s[0...@root.to_s.length] != @root.to_s
|
17
|
+
raise "Illegal path: #{path.to_s}"
|
18
|
+
end
|
19
|
+
|
20
|
+
if @path == @root
|
21
|
+
@uri = "/"
|
22
|
+
else
|
23
|
+
@uri = @path.to_s[@root.to_s.length .. -1]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def smc?
|
28
|
+
return @path.extname == ".smc"
|
29
|
+
end
|
30
|
+
|
31
|
+
def outfile
|
32
|
+
return nil unless smc?
|
33
|
+
return self.class.new(@root, @path.to_s[0 .. -5])
|
34
|
+
end
|
35
|
+
|
36
|
+
def outuri
|
37
|
+
return nil unless smc?
|
38
|
+
return uri[0 .. -5]
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.to_uri(root, path)
|
42
|
+
return self.new(root,path).uri
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'webrick'
|
2
|
+
|
3
|
+
module SmallCage
|
4
|
+
class HTTPServer
|
5
|
+
|
6
|
+
def initialize(document_root, port)
|
7
|
+
# logger = WEBrick::Log.new(nil, 1)
|
8
|
+
@server = WEBrick::HTTPServer.new({
|
9
|
+
:DocumentRoot => document_root,
|
10
|
+
:Port => port,
|
11
|
+
:AccessLog => [[File.open("/dev/null", "w+"), ""]]
|
12
|
+
})
|
13
|
+
|
14
|
+
WEBrick::HTTPServlet::FileHandler.remove_handler("cgi")
|
15
|
+
WEBrick::HTTPServlet::FileHandler.remove_handler("rhtml")
|
16
|
+
|
17
|
+
@server.mount("/_smc/update_uri", UpdateUriServlet)
|
18
|
+
@server.mount("/_smc/auto", AutoServlet)
|
19
|
+
end
|
20
|
+
|
21
|
+
def start
|
22
|
+
@server.start
|
23
|
+
end
|
24
|
+
|
25
|
+
def shutdown
|
26
|
+
@server.shutdown
|
27
|
+
end
|
28
|
+
|
29
|
+
def updated_uri=(uri)
|
30
|
+
UpdateUriServlet.uri = uri
|
31
|
+
end
|
32
|
+
|
33
|
+
def reload
|
34
|
+
UpdateUriServlet.uri = ":reload"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
class UpdateUriServlet < WEBrick::HTTPServlet::AbstractServlet
|
40
|
+
@@uri = "/index.html"
|
41
|
+
@@update_time = ""
|
42
|
+
|
43
|
+
def do_GET(req, res)
|
44
|
+
res['content-type'] = "text/plain"
|
45
|
+
res.body = @@uri + "\n" + @@update_time
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.uri=(uri)
|
49
|
+
@@uri = uri
|
50
|
+
update_time
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.update_time
|
54
|
+
@@update_time = Time.now.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
class AutoServlet < WEBrick::HTTPServlet::AbstractServlet
|
60
|
+
def do_GET(req, res)
|
61
|
+
res['content-type'] = "text/html"
|
62
|
+
html = File.dirname(__FILE__) + "/resources/auto.html"
|
63
|
+
res.body = File.read(html)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/smallcage/loader.rb
CHANGED
@@ -11,7 +11,7 @@ module SmallCage
|
|
11
11
|
target = real_target(target)
|
12
12
|
|
13
13
|
@target = target # absolute
|
14
|
-
@root =
|
14
|
+
@root = self.class.find_root(target) # absolute
|
15
15
|
@templates_dir = @root + "_smc/templates"
|
16
16
|
@helpers_dir = @root + "_smc/helpers"
|
17
17
|
@filters_dir = @root + "_smc/filters"
|
@@ -49,32 +49,34 @@ module SmallCage
|
|
49
49
|
unless path.exist?
|
50
50
|
raise "Not found: " + path.to_s
|
51
51
|
end
|
52
|
-
|
53
|
-
|
54
|
-
raise "Illegal path: " + path.to_s + " , " + @root.to_s
|
55
|
-
end
|
52
|
+
|
53
|
+
docpath = SmallCage::DocumentPath.new(@root, path)
|
56
54
|
|
57
55
|
result = {}
|
58
56
|
if path.file?
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
57
|
+
unless docpath.smc?
|
58
|
+
raise "Path is not smc file: " + docpath.to_s
|
59
|
+
end
|
60
|
+
|
61
|
+
path_smc = docpath.path
|
62
|
+
path_out = docpath.outfile.path
|
63
|
+
uri_smc = docpath.uri
|
64
|
+
uri_out = docpath.outuri
|
65
|
+
source_path = path_smc
|
64
66
|
|
65
67
|
result["dirs"] = load_dirs(path)
|
66
68
|
result["template"] = DEFAULT_TEMPLATE
|
67
|
-
else
|
69
|
+
else # directory
|
68
70
|
path_smc = nil
|
69
71
|
path_out = path
|
70
|
-
uri_smc
|
71
|
-
uri_out
|
72
|
+
uri_smc = nil
|
73
|
+
uri_out = docpath.uri
|
72
74
|
uri_out += "/" unless uri_out =~ %r{/$}
|
73
75
|
source_path = path + DIR_PROP_FILE
|
74
|
-
|
76
|
+
|
75
77
|
if source_path.file?
|
76
78
|
path_smc = source_path
|
77
|
-
uri_smc = to_uri(source_path)
|
79
|
+
uri_smc = SmallCage::DocumentPath.to_uri(@root, source_path)
|
78
80
|
end
|
79
81
|
end
|
80
82
|
|
@@ -86,6 +88,7 @@ module SmallCage
|
|
86
88
|
result["arrays"] = []
|
87
89
|
result["strings"] = []
|
88
90
|
|
91
|
+
# target is directory and _dir.smc is not exist.
|
89
92
|
return result unless source_path.exist?
|
90
93
|
|
91
94
|
source = source_path.read
|
@@ -145,7 +148,25 @@ module SmallCage
|
|
145
148
|
yield @target
|
146
149
|
end
|
147
150
|
end
|
148
|
-
|
151
|
+
|
152
|
+
def each_not_smc_file
|
153
|
+
if @target.directory?
|
154
|
+
p = Pathname.new(@target)
|
155
|
+
Dir.chdir(@target) do
|
156
|
+
Dir.glob("**/*") do |f|
|
157
|
+
f = p + f
|
158
|
+
next if f.directory?
|
159
|
+
next if f.to_s =~ %r{/_smc/}
|
160
|
+
next if f.to_s =~ %r{\.smc$}
|
161
|
+
yield SmallCage::DocumentPath.new(@root, p + f)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
else
|
165
|
+
return if @target.to_s =~ %r{/_smc/}
|
166
|
+
return if @target.to_s =~ %r{\.smc$}
|
167
|
+
yield SmallCage::DocumentPath.new(@root, @target)
|
168
|
+
end
|
169
|
+
end
|
149
170
|
|
150
171
|
def real_target(target)
|
151
172
|
return target.realpath if target.directory?
|
@@ -213,16 +234,6 @@ module SmallCage
|
|
213
234
|
end
|
214
235
|
private :load_filters_config
|
215
236
|
|
216
|
-
def strip_ext(path)
|
217
|
-
path.to_s[0..-5]
|
218
|
-
end
|
219
|
-
private :strip_ext
|
220
|
-
|
221
|
-
def to_uri(path)
|
222
|
-
path.realpath.to_s[@root.to_s.length .. -1]
|
223
|
-
end
|
224
|
-
private :to_uri
|
225
|
-
|
226
237
|
def add_smc_method(obj, value)
|
227
238
|
obj.instance_eval do
|
228
239
|
@__smallcage ||= {}
|