staticmatic2 2.0.2 → 2.1.0
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/Gemfile +6 -3
- data/Gemfile.lock +10 -6
- data/{README.markdown → README.md} +24 -5
- data/Rakefile +4 -4
- data/VERSION.yml +2 -2
- data/bin/staticmatic +6 -4
- data/lib/staticmatic/ambiguous_template_error.rb +19 -0
- data/lib/staticmatic/base.rb +77 -52
- data/lib/staticmatic/configuration.rb +32 -9
- data/lib/staticmatic/helpers/assets_helper.rb +6 -5
- data/lib/staticmatic/helpers/current_path_helper.rb +4 -4
- data/lib/staticmatic/helpers/render_helper.rb +1 -12
- data/lib/staticmatic/helpers/tag_helper.rb +1 -1
- data/lib/staticmatic/mixins/build.rb +25 -61
- data/lib/staticmatic/mixins/render.rb +61 -103
- data/lib/staticmatic/mixins/rescue.rb +1 -1
- data/lib/staticmatic/server.rb +10 -29
- data/lib/staticmatic/templates/rescues/default.haml +1 -1
- data/lib/staticmatic.rb +2 -5
- data/spec/base_spec.rb +2 -2
- data/spec/compass_integration_spec.rb +3 -2
- data/spec/helpers/asset_helper_spec.rb +1 -3
- data/spec/helpers/custom_helper_spec.rb +1 -2
- data/spec/render_spec.rb +4 -4
- data/spec/rescue_spec.rb +3 -3
- data/spec/sandbox/test_site/site/sub_folder/another_sub_folder/index.html +1 -0
- data/spec/sandbox/test_site/site/sub_folder/another_sub_folder/index.html.html +1 -0
- data/spec/sandbox/test_site/site/sub_folder/index.html +1 -0
- data/spec/sandbox/test_site/src/{helpers → _helpers}/application_helper.rb +0 -0
- data/spec/sandbox/test_site/src/{layouts → _layouts}/alternate_layout.haml +0 -0
- data/spec/sandbox/test_site/src/{layouts → _layouts}/default.haml +0 -0
- data/spec/sandbox/test_site/src/{layouts → _layouts}/projects.haml +0 -0
- data/spec/sandbox/test_site/src/{partials → _partials}/menu.haml +0 -0
- data/spec/sandbox/test_site/src/{partials → _partials}/partial_with_error.haml +0 -0
- data/spec/sandbox/test_site/src/{pages/hello_world.erb → hello_world.erb} +0 -0
- data/spec/sandbox/test_site/src/{pages/index.haml → index.haml} +0 -0
- data/spec/sandbox/test_site/src/{pages/layout_test.haml → layout_test.haml} +0 -0
- data/spec/sandbox/test_site/src/{pages/page_one.haml → page_one.haml} +0 -0
- data/spec/sandbox/test_site/src/{pages/page_two.haml → page_two.haml} +0 -0
- data/spec/sandbox/test_site/src/{pages/page_with_error.haml → page_with_error.haml} +0 -0
- data/spec/sandbox/test_site/src/{pages/page_with_partial_error.haml → page_with_partial_error.haml} +0 -0
- data/spec/server_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/template_error_spec.rb +3 -3
- data/staticmatic2.gemspec +43 -54
- metadata +72 -62
- data/spec/setup_spec.rb +0 -22
@@ -8,145 +8,103 @@ module StaticMatic::RenderMixin
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
def source_for_layout
|
12
|
-
if layout_exists?(determine_layout)
|
13
|
-
File.read(full_layout_path(determine_layout))
|
14
|
-
else
|
15
|
-
raise StaticMatic::Error.new("", full_layout_path(determine_layout), "Layout not found")
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
11
|
# Generate html from source file:
|
20
|
-
#
|
21
|
-
def
|
22
|
-
|
23
|
-
|
12
|
+
# render_template("index.haml")
|
13
|
+
def render_template(file_path)
|
14
|
+
@current_file_stack.push(file_path)
|
24
15
|
begin
|
25
|
-
|
26
|
-
html = generate_html_from_template_source(File.read(full_file_path))
|
16
|
+
tilt_template(file_path)
|
27
17
|
rescue StaticMatic::TemplateError => e
|
28
18
|
raise e # re-raise inline errors
|
29
19
|
rescue Exception => e
|
30
|
-
raise StaticMatic::TemplateError.new(
|
20
|
+
raise StaticMatic::TemplateError.new(file_path, e)
|
21
|
+
ensure
|
22
|
+
@current_file_stack.pop
|
31
23
|
end
|
32
|
-
|
33
|
-
html
|
34
24
|
end
|
35
25
|
|
36
|
-
def
|
37
|
-
@
|
38
|
-
@current_file_stack.unshift(File.join(source_dir, "#{source}.haml"))
|
26
|
+
def render_template_with_layout(file_path)
|
27
|
+
@current_file_stack.push(file_path)
|
39
28
|
begin
|
40
|
-
|
41
|
-
|
29
|
+
rendered_file_content = render_template(file_path)
|
30
|
+
tilt_template_with_layout(fetch_layout_path) { rendered_file_content }
|
42
31
|
rescue Exception => e
|
43
32
|
render_rescue_from_error(e)
|
44
33
|
ensure
|
45
|
-
|
46
|
-
@current_page = nil
|
47
|
-
@current_file_stack.shift
|
34
|
+
@current_file_stack.pop
|
48
35
|
end
|
49
36
|
end
|
50
37
|
|
51
38
|
def generate_partial(name, options = {})
|
52
|
-
partial_dir, partial_name =
|
53
|
-
partial_dir, partial_name = File.split(name) if name.index('/')
|
54
|
-
partial_type = partial_name[/(\.haml|\.html)$/]
|
55
|
-
|
39
|
+
partial_dir, partial_name, partial_ext = expand_path name
|
56
40
|
partial_name = "_#{partial_name}"
|
57
|
-
partial_name += ".haml" unless partial_type
|
58
41
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
42
|
+
context = File.join File.dirname(self.current_file), partial_dir
|
43
|
+
partial_path = determine_template_path(partial_name, partial_ext, context)
|
44
|
+
|
45
|
+
unless partial_path && File.exists?(partial_path)
|
46
|
+
# partial not found in the current file's directory, so try the _partials folder
|
47
|
+
context = File.join @src_dir, '_partials', partial_dir
|
63
48
|
partial_name.sub! /^_/, ''
|
64
|
-
partial_path =
|
49
|
+
partial_path = determine_template_path(partial_name, partial_ext, context)
|
65
50
|
end
|
66
51
|
|
67
|
-
if File.exists?(partial_path)
|
68
|
-
|
69
|
-
@current_file_stack.unshift(partial_rel_path)
|
70
|
-
begin
|
71
|
-
if partial_type == '.html'
|
72
|
-
File.read(partial_path)
|
73
|
-
else
|
74
|
-
generate_html_from_template_source(File.read(partial_path), options)
|
75
|
-
end
|
76
|
-
rescue Exception => e
|
77
|
-
raise StaticMatic::TemplateError.new(partial_path, e)
|
78
|
-
ensure
|
79
|
-
@current_file_stack.shift
|
80
|
-
end
|
52
|
+
if partial_path && File.exists?(partial_path)
|
53
|
+
return render_template partial_path
|
81
54
|
else
|
82
55
|
raise StaticMatic::Error.new("", name, "Partial not found")
|
83
56
|
end
|
84
57
|
end
|
85
58
|
|
86
|
-
def
|
87
|
-
|
59
|
+
def fetch_layout_path(dir = nil)
|
60
|
+
layout_path = File.join(@src_dir, "_layouts")
|
61
|
+
declared_layout_name = @scope.instance_variable_get("@layout")
|
88
62
|
|
89
|
-
if
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
sass_options[:syntax] = :scss
|
95
|
-
end
|
96
|
-
|
97
|
-
stylesheet = Sass::Engine.new(File.read(full_file_path), sass_options)
|
98
|
-
stylesheet.to_css
|
99
|
-
rescue Exception => e
|
100
|
-
render_rescue_from_error(StaticMatic::TemplateError.new(full_file_path, e))
|
63
|
+
if declared_layout_name
|
64
|
+
path = determine_template_path declared_layout_name, '', layout_path
|
65
|
+
unless path
|
66
|
+
error_path = File.join(layout_path, declared_layout_name)
|
67
|
+
raise StaticMatic::Error.new("", error_path, "Layout not found")
|
101
68
|
end
|
102
|
-
else
|
103
|
-
raise StaticMatic::Error.new("", source, "Stylesheet not found")
|
104
69
|
end
|
70
|
+
|
71
|
+
if dir
|
72
|
+
dir_layout_name = dir.split("/")[1]
|
73
|
+
path ||= determine_template_path dir_layout_name, '', layout_path
|
74
|
+
end
|
75
|
+
path ||= determine_template_path @default_layout_name, '', layout_path
|
76
|
+
|
77
|
+
unless path
|
78
|
+
error_path = File.join(layout_path, @default_layout_name)
|
79
|
+
raise StaticMatic::Error.new("", error_path, "No default layout could be found")
|
80
|
+
end
|
81
|
+
|
82
|
+
return path
|
105
83
|
end
|
106
84
|
|
107
|
-
|
108
|
-
full_file_path = File.join(@src_dir, source_dir, "#{source}.coffee")
|
85
|
+
private
|
109
86
|
|
110
|
-
|
111
|
-
|
87
|
+
# TODO: more code reuse. needs some ruby &block and yield sorcery.
|
88
|
+
def tilt_template(file_path)
|
89
|
+
options = get_engine_options(file_path)
|
90
|
+
Tilt.new(file_path, options).render(@scope)
|
112
91
|
end
|
113
92
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
#
|
118
|
-
# Pass a block containing a string to yield within in the passed source:
|
119
|
-
#
|
120
|
-
# generate_html_from_template_source("content:\n= yield") { "blah" } -> "content: blah"
|
121
|
-
#
|
122
|
-
def generate_html_from_template_source(source, options = {})
|
123
|
-
html = Haml::Engine.new(source, self.configuration.haml_options.merge(options))
|
124
|
-
locals = options[:locals] || {}
|
125
|
-
html.render(@scope, locals) { yield }
|
93
|
+
def tilt_template_with_layout(file_path)
|
94
|
+
options = get_engine_options(file_path)
|
95
|
+
Tilt.new(file_path, options).render(@scope) { yield }
|
126
96
|
end
|
127
97
|
|
128
|
-
def
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
layout_name = @scope.instance_variable_get("@layout")
|
133
|
-
elsif dir
|
134
|
-
dirs = dir.split("/")
|
135
|
-
dir_layout_name = dirs[1]
|
136
|
-
|
137
|
-
if layout_exists?(dir_layout_name)
|
138
|
-
layout_name = dir_layout_name
|
139
|
-
end
|
140
|
-
end
|
98
|
+
def get_engine_options(file_path)
|
99
|
+
ext = File.extname(file_path).sub(/^\./, '')
|
100
|
+
options = configuration.engine_options[ext] || {}
|
101
|
+
preview_options = configuration.preview_engine_options[ext] || {}
|
141
102
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
def source_template_from_path(path)
|
148
|
-
file_dir, file_name = File.split(path)
|
149
|
-
file_name.chomp!(File.extname(file_name))
|
150
|
-
[ file_dir, file_name ]
|
103
|
+
if @mode == :preview
|
104
|
+
options.merge preview_options
|
105
|
+
else
|
106
|
+
options
|
107
|
+
end
|
151
108
|
end
|
109
|
+
|
152
110
|
end
|
data/lib/staticmatic/server.rb
CHANGED
@@ -3,36 +3,37 @@ module StaticMatic
|
|
3
3
|
def initialize(staticmatic, default = nil)
|
4
4
|
@files = default || Rack::File.new(staticmatic.src_dir)
|
5
5
|
@staticmatic = staticmatic
|
6
|
-
|
7
|
-
|
8
6
|
end
|
9
7
|
|
10
8
|
def call(env)
|
11
9
|
@staticmatic.load_helpers
|
12
10
|
path_info = env["PATH_INFO"]
|
13
11
|
|
14
|
-
file_dir, file_name, file_ext = expand_path(path_info)
|
12
|
+
file_dir, file_name, file_ext = @staticmatic.expand_path(path_info)
|
15
13
|
|
16
14
|
file_dir = CGI::unescape(file_dir)
|
17
15
|
file_name = CGI::unescape(file_name)
|
18
16
|
|
19
17
|
unless file_ext && ["html", "css", "js"].include?(file_ext) &&
|
20
|
-
|
21
|
-
|
18
|
+
File.basename(file_name) !~ /^\_/ &&
|
19
|
+
(template_path = @staticmatic.determine_template_path file_name, file_ext, file_dir)
|
22
20
|
return @files.call(env)
|
23
21
|
end
|
24
22
|
|
25
23
|
res = Rack::Response.new
|
26
|
-
res.header["Content-Type"] = "text/#{file_ext}"
|
27
24
|
|
28
25
|
begin
|
26
|
+
@staticmatic.clear_template_variables!
|
27
|
+
|
29
28
|
if file_ext == "css"
|
30
|
-
res.
|
29
|
+
res.header["Content-Type"] = "text/css"
|
30
|
+
res.write @staticmatic.render_template(template_path)
|
31
31
|
elsif file_ext == "js"
|
32
32
|
res.header["Content-Type"] = "text/javascript"
|
33
|
-
res.write @staticmatic.
|
33
|
+
res.write @staticmatic.render_template(template_path)
|
34
34
|
else
|
35
|
-
res.
|
35
|
+
res.header["Content-Type"] = "text/html"
|
36
|
+
res.write @staticmatic.render_template_with_layout(template_path)
|
36
37
|
end
|
37
38
|
rescue StaticMatic::Error => e
|
38
39
|
res.write e.message
|
@@ -62,25 +63,5 @@ module StaticMatic
|
|
62
63
|
Rack::Handler::WEBrick.run(app, :Port => port, :Host => host)
|
63
64
|
end
|
64
65
|
|
65
|
-
private
|
66
|
-
|
67
|
-
def expand_path(path_info)
|
68
|
-
dirname, basename = File.split(path_info)
|
69
|
-
|
70
|
-
extname = File.extname(path_info).sub(/^\./, '')
|
71
|
-
filename = basename.chomp(".#{extname}")
|
72
|
-
|
73
|
-
if extname.empty?
|
74
|
-
dir = File.join(dirname, filename)
|
75
|
-
is_dir = path_info[-1, 1] == '/'
|
76
|
-
if is_dir
|
77
|
-
dirname = dir
|
78
|
-
filename = 'index'
|
79
|
-
end
|
80
|
-
extname = 'html'
|
81
|
-
end
|
82
|
-
|
83
|
-
[ dirname, filename, extname ]
|
84
|
-
end
|
85
66
|
end
|
86
67
|
end
|
data/lib/staticmatic.rb
CHANGED
@@ -3,6 +3,7 @@ require 'compass'
|
|
3
3
|
require 'haml'
|
4
4
|
require 'sass'
|
5
5
|
require 'sass/plugin'
|
6
|
+
require 'tilt'
|
6
7
|
require 'rack'
|
7
8
|
require 'rack/handler/webrick'
|
8
9
|
require 'cgi'
|
@@ -10,16 +11,12 @@ require 'fileutils'
|
|
10
11
|
|
11
12
|
|
12
13
|
module StaticMatic
|
13
|
-
VERSION = "0.11.0"
|
14
14
|
end
|
15
15
|
|
16
16
|
["render", "build", "setup", "server", "helpers", "rescue"].each do |mixin|
|
17
17
|
require File.join(File.dirname(__FILE__), "staticmatic", "mixins", mixin)
|
18
18
|
end
|
19
19
|
|
20
|
-
["base", "configuration", "error", "server", "helpers", "template_error", "compass"].each do |lib|
|
20
|
+
["base", "configuration", "error", "server", "helpers", "template_error", "ambiguous_template_error", "compass"].each do |lib|
|
21
21
|
require File.join(File.dirname(__FILE__), "staticmatic", lib)
|
22
22
|
end
|
23
|
-
|
24
|
-
Haml::Helpers.class_eval("include StaticMatic::Helpers")
|
25
|
-
|
data/spec/base_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe "Compass integration" do
|
4
4
|
context "with the default staticmatic configuration" do
|
@@ -7,6 +7,7 @@ describe "Compass integration" do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should configure compass" do
|
10
|
+
pending "Compass config not quite right yet"
|
10
11
|
Compass.configuration.project_path.should == TEST_SITE_PATH
|
11
12
|
Compass.configuration.sass_dir.should == File.join("src", "stylesheets")
|
12
13
|
Compass.configuration.css_dir.should == File.join("site", "stylesheets")
|
@@ -24,4 +25,4 @@ describe "Compass integration" do
|
|
24
25
|
Compass.configuration.http_path.should == "http://a.test.host"
|
25
26
|
end
|
26
27
|
end
|
27
|
-
end
|
28
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
require
|
2
|
+
require "spec_helper"
|
3
3
|
|
4
4
|
describe "Helpers:" do
|
5
5
|
include StaticMatic::Helpers::AssetsHelper
|
@@ -7,7 +7,6 @@ describe "Helpers:" do
|
|
7
7
|
include StaticMatic::Helpers::TagHelper
|
8
8
|
before do
|
9
9
|
setup_staticmatic
|
10
|
-
@staticmatic.instance_variable_set("@current_page", "")
|
11
10
|
end
|
12
11
|
|
13
12
|
context "When using the stylesheet helper" do
|
@@ -32,7 +31,6 @@ describe "Helpers:" do
|
|
32
31
|
|
33
32
|
context "When using the stylesheet helper from a sub page" do
|
34
33
|
before do
|
35
|
-
@staticmatic.instance_variable_set("@current_page", "/sub/index.html")
|
36
34
|
@links = stylesheets
|
37
35
|
end
|
38
36
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
require
|
2
|
+
require "spec_helper"
|
3
3
|
|
4
4
|
describe "Helpers:" do
|
5
5
|
include StaticMatic::Helpers::AssetsHelper
|
@@ -7,7 +7,6 @@ describe "Helpers:" do
|
|
7
7
|
include StaticMatic::Helpers::TagHelper
|
8
8
|
before do
|
9
9
|
setup_staticmatic
|
10
|
-
@staticmatic.instance_variable_set("@current_page", "")
|
11
10
|
end
|
12
11
|
|
13
12
|
it "should include custom helper" do
|
data/spec/render_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe "StaticMatic::Render" do
|
4
4
|
before do
|
@@ -17,7 +17,7 @@ describe "StaticMatic::Render" do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it "generate css" do
|
20
|
-
content = @staticmatic.generate_css("application")
|
20
|
+
content = @staticmatic.generate_css("stylesheets/application")
|
21
21
|
end
|
22
22
|
|
23
23
|
it "find source filename from path" do
|
@@ -30,7 +30,7 @@ describe "StaticMatic::Render" do
|
|
30
30
|
|
31
31
|
context "handling scss" do
|
32
32
|
it "should generate css from scss" do
|
33
|
-
@staticmatic.generate_css("sassy").should match(/color\: \#3bbfce\;/)
|
33
|
+
@staticmatic.generate_css("stylesheets/sassy").should match(/color\: \#3bbfce\;/)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
@@ -41,4 +41,4 @@ describe "StaticMatic::Render" do
|
|
41
41
|
output.should match(/The variable is/)
|
42
42
|
output.should_not match(/hello/)
|
43
43
|
end
|
44
|
-
end
|
44
|
+
end
|
data/spec/rescue_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe "StaticMatic::Rescue" do
|
4
4
|
before do
|
@@ -11,7 +11,7 @@ describe "StaticMatic::Rescue" do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "catch sass template errors" do
|
14
|
-
output = @staticmatic.generate_css("css_with_error")
|
14
|
+
output = @staticmatic.generate_css("stylesheets/css_with_error")
|
15
15
|
output.should match(/StaticMatic::TemplateError/)
|
16
16
|
end
|
17
17
|
|
@@ -33,4 +33,4 @@ describe "StaticMatic::Rescue" do
|
|
33
33
|
output.should match(/Exception/)
|
34
34
|
output.should match(/This is an exception/)
|
35
35
|
end
|
36
|
-
end
|
36
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Sub sub folder test
|
@@ -0,0 +1 @@
|
|
1
|
+
Sub sub folder test
|
@@ -0,0 +1 @@
|
|
1
|
+
An index of a sub folder
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/spec/sandbox/test_site/src/{pages/page_with_partial_error.haml → page_with_partial_error.haml}
RENAMED
File without changes
|
data/spec/server_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/template_error_spec.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe "StaticMatic::Template" do
|
4
4
|
before do
|
5
5
|
setup_staticmatic
|
6
6
|
|
7
|
-
template_file = File.join(TEST_SITE_PATH, "src", "
|
7
|
+
template_file = File.join(TEST_SITE_PATH, "src", "page_with_error.haml")
|
8
8
|
|
9
9
|
begin
|
10
10
|
@staticmatic.generate_html_from_template_source(File.read(template_file))
|
@@ -20,4 +20,4 @@ describe "StaticMatic::Template" do
|
|
20
20
|
it "extract line number from backtrace" do
|
21
21
|
@template_error.line_number.should == "3"
|
22
22
|
end
|
23
|
-
end
|
23
|
+
end
|