staticmatic2 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/Gemfile +6 -3
  2. data/Gemfile.lock +10 -6
  3. data/{README.markdown → README.md} +24 -5
  4. data/Rakefile +4 -4
  5. data/VERSION.yml +2 -2
  6. data/bin/staticmatic +6 -4
  7. data/lib/staticmatic/ambiguous_template_error.rb +19 -0
  8. data/lib/staticmatic/base.rb +77 -52
  9. data/lib/staticmatic/configuration.rb +32 -9
  10. data/lib/staticmatic/helpers/assets_helper.rb +6 -5
  11. data/lib/staticmatic/helpers/current_path_helper.rb +4 -4
  12. data/lib/staticmatic/helpers/render_helper.rb +1 -12
  13. data/lib/staticmatic/helpers/tag_helper.rb +1 -1
  14. data/lib/staticmatic/mixins/build.rb +25 -61
  15. data/lib/staticmatic/mixins/render.rb +61 -103
  16. data/lib/staticmatic/mixins/rescue.rb +1 -1
  17. data/lib/staticmatic/server.rb +10 -29
  18. data/lib/staticmatic/templates/rescues/default.haml +1 -1
  19. data/lib/staticmatic.rb +2 -5
  20. data/spec/base_spec.rb +2 -2
  21. data/spec/compass_integration_spec.rb +3 -2
  22. data/spec/helpers/asset_helper_spec.rb +1 -3
  23. data/spec/helpers/custom_helper_spec.rb +1 -2
  24. data/spec/render_spec.rb +4 -4
  25. data/spec/rescue_spec.rb +3 -3
  26. data/spec/sandbox/test_site/site/sub_folder/another_sub_folder/index.html +1 -0
  27. data/spec/sandbox/test_site/site/sub_folder/another_sub_folder/index.html.html +1 -0
  28. data/spec/sandbox/test_site/site/sub_folder/index.html +1 -0
  29. data/spec/sandbox/test_site/src/{helpers → _helpers}/application_helper.rb +0 -0
  30. data/spec/sandbox/test_site/src/{layouts → _layouts}/alternate_layout.haml +0 -0
  31. data/spec/sandbox/test_site/src/{layouts → _layouts}/default.haml +0 -0
  32. data/spec/sandbox/test_site/src/{layouts → _layouts}/projects.haml +0 -0
  33. data/spec/sandbox/test_site/src/{partials → _partials}/menu.haml +0 -0
  34. data/spec/sandbox/test_site/src/{partials → _partials}/partial_with_error.haml +0 -0
  35. data/spec/sandbox/test_site/src/{pages/hello_world.erb → hello_world.erb} +0 -0
  36. data/spec/sandbox/test_site/src/{pages/index.haml → index.haml} +0 -0
  37. data/spec/sandbox/test_site/src/{pages/layout_test.haml → layout_test.haml} +0 -0
  38. data/spec/sandbox/test_site/src/{pages/page_one.haml → page_one.haml} +0 -0
  39. data/spec/sandbox/test_site/src/{pages/page_two.haml → page_two.haml} +0 -0
  40. data/spec/sandbox/test_site/src/{pages/page_with_error.haml → page_with_error.haml} +0 -0
  41. data/spec/sandbox/test_site/src/{pages/page_with_partial_error.haml → page_with_partial_error.haml} +0 -0
  42. data/spec/server_spec.rb +1 -1
  43. data/spec/spec_helper.rb +1 -1
  44. data/spec/template_error_spec.rb +3 -3
  45. data/staticmatic2.gemspec +43 -54
  46. metadata +72 -62
  47. 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
- # generate_html("index")
21
- def generate_html(source_file, source_dir = '')
22
- full_file_path = File.join(@src_dir, source_dir, "#{source_file}.haml")
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(full_file_path, e)
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 generate_html_with_layout(source, source_dir = '')
37
- @current_page = File.join(source_dir, "#{source}.html")
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
- template_content = generate_html(source, source_dir)
41
- generate_html_from_template_source(source_for_layout) { template_content }
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
- clear_template_variables!
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 = '', 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
- partial_path = File.join(@src_dir, File.dirname(self.current_file), partial_dir, partial_name)
60
- unless File.exists?(partial_path)
61
- # couldn't find it in the pages subdirectory tree, so try the partials folder
62
- partial_dir = File.join '_partials', partial_dir
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 = File.join(@src_dir, partial_dir, partial_name)
49
+ partial_path = determine_template_path(partial_name, partial_ext, context)
65
50
  end
66
51
 
67
- if File.exists?(partial_path)
68
- partial_rel_path = "/#{partial_dir}/#{partial_name}".gsub(/\/+/, '/')
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 generate_css(source, source_dir = '')
87
- full_file_path = Dir[File.join(@src_dir, source_dir, "#{source}.{sass,scss}")].first
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 full_file_path && File.exist?(full_file_path)
90
- begin
91
- sass_options = { :load_paths => [ @src_dir ] }.merge(self.configuration.sass_options)
92
-
93
- if File.extname(full_file_path) == ".scss"
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
- def generate_js(source, source_dir = '')
108
- full_file_path = File.join(@src_dir, source_dir, "#{source}.coffee")
85
+ private
109
86
 
110
- coffee_options = "#{self.configuration.coffee_options} -p"
111
- javascript = `coffee #{coffee_options} #{full_file_path}`
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
- # Generates html from the passed source string
115
- #
116
- # generate_html_from_template_source("%h1 Welcome to My Site") -> "<h1>Welcome to My Site</h1>"
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 determine_layout(dir = '')
129
- layout_name ||= @default_layout
130
-
131
- if @scope.instance_variable_get("@layout")
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
- layout_name
143
- end
144
-
145
- # Returns a raw template name from a source file path:
146
- # source_template_from_path("/path/to/site/src/stylesheets/application.sass") -> "application"
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
@@ -7,6 +7,6 @@ module StaticMatic::RescueMixin
7
7
 
8
8
  @scope.instance_variable_set("@exception", exception)
9
9
 
10
- generate_html_from_template_source(File.read(error_template_path))
10
+ render_template(error_template_path)
11
11
  end
12
12
  end
@@ -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
- @staticmatic.template_exists?(file_name, file_ext, file_dir) &&
21
- File.basename(file_name) !~ /^\_/
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.write @staticmatic.generate_css(file_name, file_dir)
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.generate_js(file_name, file_dir)
33
+ res.write @staticmatic.render_template(template_path)
34
34
  else
35
- res.write @staticmatic.generate_html_with_layout(file_name, file_dir)
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
@@ -1,6 +1,6 @@
1
1
  %h1= @exception.class.name
2
2
 
3
- %p= @exception.message
3
+ %p= @exception.message.gsub /\n/, '<br />'
4
4
 
5
5
  %h2 Stack Trace
6
6
 
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 File.dirname(__FILE__) + "/spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe "StaticMatic::Base" do
4
4
  before do
@@ -10,4 +10,4 @@ describe "StaticMatic::Base" do
10
10
  @staticmatic.configuration.preview_server_port.should == 3000
11
11
  end
12
12
 
13
- end
13
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + "/spec_helper"
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 File.dirname(__FILE__) + "/../spec_helper"
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 File.dirname(__FILE__) + "/../spec_helper"
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 File.dirname(__FILE__) + "/spec_helper"
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 File.dirname(__FILE__) + "/spec_helper"
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
+ An index of a sub folder
data/spec/server_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + "/spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe "StaticMatic::Server" do
4
4
  before do
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'stringio'
3
3
  require 'spec'
4
4
 
5
- require 'lib/staticmatic'
5
+ require 'staticmatic'
6
6
 
7
7
  TEST_SITE_PATH = File.expand_path(File.join(File.dirname(__FILE__), "sandbox", "test_site"))
8
8
 
@@ -1,10 +1,10 @@
1
- require File.dirname(__FILE__) + "/spec_helper"
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", "pages", "page_with_error.haml")
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