tdreyno-staticmatic 2.1.4 → 2.9.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.
Files changed (45) hide show
  1. data/bin/sm-build +4 -0
  2. data/bin/sm-init +4 -0
  3. data/bin/sm-server +4 -0
  4. data/lib/staticmatic.rb +1 -0
  5. data/lib/staticmatic/autoload.rb +1 -4
  6. data/lib/staticmatic/base.rb +39 -93
  7. data/lib/staticmatic/builder.rb +59 -53
  8. data/lib/staticmatic/builder/dotnet.rb +44 -0
  9. data/lib/staticmatic/builder/html.rb +23 -0
  10. data/lib/staticmatic/generator.rb +3 -3
  11. data/lib/staticmatic/previewer.rb +23 -14
  12. data/lib/staticmatic/template.rb +97 -0
  13. data/lib/templates/{src/helpers → helpers}/site_helper.rb +0 -0
  14. data/lib/templates/{src/layouts → layouts}/site.html.haml +0 -0
  15. data/lib/templates/{src/pages → pages}/index.html.haml +0 -0
  16. data/lib/templates/{src/stylesheets → stylesheets}/site.css.sass +0 -0
  17. data/spec/action_view_helpers_spec.rb +1 -1
  18. data/spec/asset_helpers_spec.rb +9 -7
  19. data/spec/base_spec.rb +22 -10
  20. data/spec/builder_spec.rb +29 -33
  21. data/spec/fixtures/sample/{src/helpers → helpers}/speech_helper.rb +0 -0
  22. data/spec/fixtures/sample/{src/layouts → layouts}/site.html.erb +0 -0
  23. data/spec/fixtures/sample/{src/layouts → layouts}/specified_layout.html.erb +0 -0
  24. data/spec/fixtures/sample/{src/pages → pages}/_form.html.erb +0 -0
  25. data/spec/fixtures/sample/{src/pages → pages}/haml_test.html.haml +0 -0
  26. data/spec/fixtures/sample/{src/pages → pages}/hello_world.html.erb +0 -0
  27. data/spec/fixtures/sample/{src/pages → pages}/index.html.erb +0 -0
  28. data/spec/fixtures/sample/{src/pages → pages}/liquid_test.html.liquid +0 -0
  29. data/spec/fixtures/sample/{src/pages → pages}/markdown_test.html.markdown +0 -0
  30. data/spec/fixtures/sample/{src/pages → pages}/page_with_error.html.haml +0 -0
  31. data/spec/fixtures/sample/pages/partial_test.html.erb +1 -0
  32. data/spec/fixtures/sample/{src/pages → pages}/services/index.html.erb +0 -0
  33. data/spec/fixtures/sample/{src/pages → pages}/services/web_development.html.erb +0 -0
  34. data/spec/fixtures/sample/{src/pages → pages}/specify_layout.html.erb +0 -0
  35. data/spec/fixtures/sample/pages/static.html +1 -0
  36. data/spec/fixtures/sample/{src/pages → pages}/textile_test.html.textile +0 -0
  37. data/spec/fixtures/sample/{src/stylesheets → stylesheets}/site.css.sass +0 -0
  38. data/spec/layouts_spec.rb +1 -2
  39. data/spec/rendering_spec.rb +4 -25
  40. metadata +66 -52
  41. data/bin/staticmatic +0 -5
  42. data/lib/templates/script/builder +0 -5
  43. data/lib/templates/script/server +0 -4
  44. data/spec/fixtures/sample/Rakefile +0 -4
  45. data/spec/fixtures/sample/config.rb +0 -3
data/bin/sm-build ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'staticmatic')
4
+ StaticMatic::Builder.run_cli(Dir.pwd, 'sm-build', 1, %w(build --force).concat(ARGV))
data/bin/sm-init ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'staticmatic')
4
+ StaticMatic::Generator.run_cli(Dir.pwd, 'sm-init', 1, %w(setup).concat(ARGV))
data/bin/sm-server ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'staticmatic')
4
+ StaticMatic::Previewer.start(Dir.pwd)
data/lib/staticmatic.rb CHANGED
@@ -5,6 +5,7 @@ $LOAD_PATH.unshift File.dirname(__FILE__) unless
5
5
  require 'rubygems'
6
6
  require 'staticmatic/autoload'
7
7
 
8
+ gem 'actionpack', '2.1.2'
8
9
  require 'active_support'
9
10
  $LOAD_PATH.unshift Gem.required_location('actionpack', 'action_controller/vendor/html-scanner')
10
11
  require 'action_view'
@@ -1,5 +1,6 @@
1
1
  module StaticMatic
2
2
  autoload :Base, File.join(File.dirname(__FILE__), 'base')
3
+ autoload :Template, File.join(File.dirname(__FILE__), 'template')
3
4
  autoload :Config, File.join(File.dirname(__FILE__), 'config')
4
5
  autoload :Builder, File.join(File.dirname(__FILE__), 'builder')
5
6
  autoload :Generator, File.join(File.dirname(__FILE__), 'generator')
@@ -14,8 +15,4 @@ module StaticMatic
14
15
 
15
16
  module TemplateHandlers
16
17
  end
17
-
18
- module Generator
19
- autoload :BuilderGenerator, File.join(File.dirname(__FILE__), 'builder')
20
- end
21
18
  end
@@ -2,12 +2,12 @@ module StaticMatic
2
2
  class Base
3
3
  include StaticMatic::Deprecation
4
4
 
5
- attr_accessor :logger, :root_dir, :src_dir, :build_dir
5
+ attr_accessor :logger, :root_dir, :build_dir, :extentions, :guard
6
6
 
7
7
  def initialize(root_dir)
8
8
  @root_dir = root_dir
9
- @src_dir = File.join(@root_dir, "src")
10
9
  @build_dir = File.join(@root_dir, "build")
10
+ @guard = Mutex.new
11
11
 
12
12
  # Setup logger
13
13
  @logger = Logger.new(STDERR)
@@ -17,115 +17,61 @@ module StaticMatic
17
17
  StaticMatic::Config.setup
18
18
  config_file = File.join(@root_dir, "config.rb")
19
19
  require config_file if File.exists? config_file
20
- end
21
-
22
- # Checks to see if a template exists within a given path
23
- #
24
- # Current only used by the previewer as ActionView handles the actual checking
25
- def can_render?(filename)
26
- relative_path = full_template_path(filename)
27
- view.template_format = determine_format_for(relative_path)
28
- view.finder.class.reload!
29
- view.finder.file_exists?(strip_extension(relative_path))
30
- end
31
-
32
- def rendered_ext(filename)
33
- regex = Regexp.new('\.(' + ActionView::Template.template_handler_extensions.join('|') + ')')
34
- filename.gsub(regex, '')
20
+
21
+ @extentions = ActionView::Template.template_handler_extensions.map { |ext| ext.to_s }
35
22
  end
36
23
 
37
- # Return the format for a given template path
38
- #
39
- # For example: application.css.sass -> :css
40
- def determine_format_for(filename)
41
- extension = File.extname(filename)
42
-
43
- # For templates that have only handler extensions, default for backwards compatibility
44
- extension = ".html" if extension.empty? || extension == ".haml"
45
- extension = ".css" if extension == ".sass"
46
-
47
- extension[1,extension.length-1].to_sym
48
- end
49
-
50
- def calculate_relative_path_to_root(template)
51
- return '' if template.match(/^((\.\.?)?\/|\#|.+?\:)/)
52
-
53
- current_page_depth = template.split('/').length - 2;
54
- (current_page_depth > 0) ? ([ '..' ] * current_page_depth).join('/') + '/' : ''
24
+ def helpers
25
+ Dir[File.join(@root_dir, 'helpers', '**', '*_helper.rb')]
55
26
  end
56
-
57
- def render(filename)
58
- load_helpers
59
- filename = full_template_path(filename)
60
- output = do_render(filename)
61
-
62
- return output if determine_format_for(filename) == :css
63
- layout = view.instance_variable_get("@layout")
64
- layout = "site" if layout.nil?
65
- return output unless layout
66
27
 
67
- view.instance_variable_set("@content_for_layout", output)
68
- view.instance_variable_set("@layout", nil) # Clean @layout variable for next request
69
- do_render("layouts/#{layout}")
70
- rescue Exception => e
71
- rescue_from_error(e)
72
- end
73
-
74
- def view
75
- @view ||= begin
76
- # Initialize View
77
- view = ActionView::Base.new([], {}, self)
78
- view.template_format = :html
79
- view.finder.view_paths = [ @src_dir ]
80
- view.instance_variable_set("@staticmatic", self)
81
- view
28
+ def layouts
29
+ @layouts ||= Dir[File.join(@root_dir, "layouts", "*")].select do |path|
30
+ !File.directory?(path)
31
+ end.map do |path|
32
+ File.basename(path)
82
33
  end
83
34
  end
84
-
85
- def do_render(filename)
86
- view.instance_variable_set("@current_page", filename)
87
- view.template_format = determine_format_for(filename)
88
- view.render_file(strip_extension(filename), true)
89
- end
90
-
91
- def rescue_from_error(exception)
92
- rescue_template = (exception == ActionView::TemplateError) ? "template_error" : "default_error"
93
- error_template_path = File.expand_path(File.dirname(__FILE__) + "/templates/rescues/#{rescue_template}.html.erb")
94
35
 
95
- view.instance_variable_set("@exception", exception)
96
- view.render_file(error_template_path, false)
36
+ def template(filename)
37
+ filename = full_template_path(filename)
38
+ @templates ||= {}
39
+ @templates[filename] ||= Template.new(filename, self)
97
40
  end
98
-
99
- # Load all helpers from src/helpers/
100
- def load_helpers
101
- Dir[File.join(@src_dir, 'helpers', '**', '*_helper.rb')].each do |helper|
102
- load(helper)
103
- module_name = File.basename(helper, '.rb').gsub(/(^|\_)./) { |c| c.upcase }.gsub(/\_/, '')
104
- ActionView::Base.class_eval("include #{module_name}")
41
+
42
+ def pages
43
+ @pages ||= Dir[File.join(@root_dir, "pages", "**/*")].select do |path|
44
+ !File.directory?(path) && File.basename(path) !~ /^_/
45
+ end.map do |path|
46
+ path.gsub("#{@root_dir}/", "")
47
+ end.partition do |relative_path|
48
+ can_render?(relative_path)
105
49
  end
106
50
  end
51
+
52
+ # Aliases
53
+ def dynamic_pages; pages.first; end
54
+ def static_pages; pages.last; end
55
+
56
+ # Delegate to Template class
57
+ def render(filename); template(filename).render; end
58
+ def rendered_ext(filename); template(filename).rendered_ext; end
59
+ def can_render?(filename)
60
+ return false if filename =~ /images\/|javascripts\//
61
+ template(filename).can_render?
62
+ end
107
63
 
64
+ protected
108
65
  # Full path to a template, relative to src/
109
66
  def full_template_path(template)
110
- template = template.gsub("#{@src_dir}/", '')
111
- directory = template_directory_for(template)
67
+ template = template.gsub("#{@root_dir}/", '')
68
+ directory = template.match(/^(\/)?(layouts|stylesheets|pages)/) ? "" : "pages"
112
69
  template = File.join(directory, template) unless directory.empty?
113
70
 
114
- path = File.join(File.expand_path(src_dir), template)
71
+ path = File.join(File.expand_path(@root_dir), template)
115
72
  template = File.join(template, "index") if File.directory? path
116
73
 
117
74
  template
118
75
  end
119
-
120
- # Return the source directory for a given template
121
- # Default is pages/
122
- def template_directory_for(template)
123
- template.match(/^(\/)?(layouts|stylesheets|pages)/) ? "" : "pages"
124
- end
125
-
126
- # Remove the extension from a given template path
127
- def strip_extension(template)
128
- template.split('.').first
129
- end
130
76
  end
131
77
  end
@@ -1,80 +1,86 @@
1
1
  require 'templater'
2
+ require 'zip/zip'
3
+ require 'find'
4
+ require 'fileutils'
2
5
 
3
6
  module StaticMatic
4
7
  module Builder
5
8
  extend Templater::Manifold
6
9
  desc "Multiple ways to build a staticmatic site"
7
10
 
8
- class BuilderGenerator < Templater::Generator
11
+ class BaseGenerator < Templater::Generator
9
12
  # Setup StaticMatic site
10
13
  def self.site; @@site ||= ::StaticMatic::Base.new(Dir.pwd); end
11
14
  def site; self.class.site; end
12
15
 
13
16
  # Define source and desintation
14
- def self.source_root; site.src_dir; end
17
+ def self.source_root; site.root_dir; end
15
18
  def destination_root; site.build_dir; end
16
19
 
17
- # Override the base template to handle haml/sass/layouts
18
- option :format, :default => :html, :desc => "Which format to export as (html, rails, dotnet)"
20
+ # Override template to ask staticmatic for the correct extension to output
21
+ # NOTE: could probably be better
19
22
  def self.template(name, *args, &block)
20
- # template = case format
21
- # when :rails
22
- # RailsTemplate
23
- # when :dotnet
24
- # DotNetTemplate
25
- # else
26
- # HTMLTemplate
27
- # end
28
-
29
- options = args.last.is_a?(Hash) ? args.pop : {}
30
- source, destination = args
31
- destination ||= source
32
- templates << Templater::ActionDescription.new(name, options) do |generator|
33
- HTMLTemplate.new(generator, name, source, destination, options)
23
+ ext = File.extname(args.first)
24
+ ext = ext[1..ext.length]
25
+
26
+ if (args[0] === args[1]) && (site.extentions.include? ext)
27
+ args[1] = site.rendered_ext(args[0])
34
28
  end
29
+
30
+ super(name, *args, &block)
35
31
  end
36
32
 
37
- %w(images javascripts stylesheets pages).each do |dir|
38
- base_path = File.join(source_root, dir)
39
- Dir[File.join(base_path, "**", "*")].each do |path|
40
- next if File.directory?(path)
41
- relative_path = path.gsub("#{source_root}/", "")
42
-
43
- if !%w(images javascripts).include?(dir) && site.can_render?(relative_path)
44
- destination_path = site.rendered_ext(relative_path)
45
-
46
- if dir == 'pages'
47
- destination_path.gsub!("pages/", "")
48
- next if destination_path =~ /^_/
49
- end
50
-
51
- template :rendered_file, relative_path, destination_path
52
- else
53
- file :static_file, relative_path
54
- end
55
- end
33
+ def render(action)
34
+ site.render(action.source)
56
35
  end
57
36
  end
58
37
 
59
- class HTMLTemplate < Templater::Actions::Template
60
- def render
61
- generator.site.render(source)
38
+ require File.join(File.dirname(__FILE__), 'builder', 'html')
39
+ require File.join(File.dirname(__FILE__), 'builder', 'dotnet')
40
+
41
+ class BuilderGenerator < Templater::Generator
42
+ # Setup StaticMatic site
43
+ def self.site; @@site ||= ::StaticMatic::Base.new(Dir.pwd); end
44
+ def site; self.class.site; end
45
+
46
+ option :format, :default => :html, :desc => "Which format to export as (html, dotnet)"
47
+ option :package, :as => :boolean, :desc => "Build a Zip file"
48
+
49
+ invoke :build_html, :format => :html
50
+ invoke :build_dotnet, :format => :dotnet
51
+
52
+ def after_generation
53
+ return unless package
54
+
55
+ output_file = File.join(site.build_dir, "package.zip")
56
+ FileUtils.rm(output_file) if File.exists?(output_file)
57
+
58
+ Zip::ZipFile.open(output_file, Zip::ZipFile::CREATE) do |zipfile|
59
+ Find.find(site.build_dir) do |path|
60
+ Find.prune if File.basename(path)[0] == ?.
61
+ dest = /build\/(\w.*)/.match(path)
62
+ zipfile.add(dest[1], path) if dest
63
+ end
64
+ end
65
+
66
+ status_flag = "[PACKAGE]".rjust(12)
67
+ say "<%= color('#{status_flag}', :green) %> " + File.basename(output_file)
62
68
  end
63
69
  end
64
70
 
65
- # class RailsTemplate < Templater::Actions::Template
66
- # def render
67
- # # No render, just copy it
68
- # end
69
- # end
70
- #
71
- # class DotNetTemplate < Templater::Actions::Template
72
- # def render
73
- # # Wrap with MasterPage tags
74
- # @staticmatic.render(file_name) # no layout
75
- # end
76
- # end
77
-
78
71
  add :build, BuilderGenerator
79
72
  end
73
+ end
74
+
75
+ # Monkey-patch to use a dynamic renderer, not just ERb
76
+ class Templater::Actions::Template
77
+ # Re-route back to the generator
78
+ def render
79
+ if generator.respond_to? :render
80
+ generator.render(self)
81
+ else
82
+ context = generator.instance_eval 'binding'
83
+ ERB.new(::File.read(source), nil, '-').result(context)
84
+ end
85
+ end
80
86
  end
@@ -0,0 +1,44 @@
1
+ module StaticMatic
2
+ module Builder
3
+ class DotNetGenerator < BaseGenerator
4
+ glob! "images", [] # No templates
5
+ glob! "javascripts", [] # No templates
6
+ glob! "stylesheets", site.extentions # Registered templates
7
+
8
+ # Run the dynamic pages through actionpack
9
+ site.dynamic_pages.each do |relative_path|
10
+ output_name = site.rendered_ext(relative_path).gsub("pages/", "")
11
+ template output_name.gsub('.', '_').to_sym, relative_path, output_name
12
+ end
13
+
14
+ # Build master pages
15
+ site.layouts.each do |layout_name|
16
+ file_name = (site.layouts.length <= 1) ? "MasterPage" : layout_name.split('.')[0].capitalize
17
+ template :layout, File.join("layouts", layout_name), "#{file_name}.master"
18
+ end
19
+
20
+ # Copy static pages
21
+ site.static_pages.each do |relative_path|
22
+ output_name = relative_path.gsub("pages/", "")
23
+ file output_name.gsub('.', '_').to_sym, relative_path, output_name
24
+ end
25
+
26
+ def render(action)
27
+ if action.name == :layout
28
+ output = site.render_layout(action.source, '<asp:ContentPlaceHolder id="content" runat="server" />')
29
+ file_name = File.basename(action.destination).gsub(".master", '')
30
+ prefix = %Q(<%@ Master Language="C#" AutoEventWireup="true" CodeFile="#{file_name}.master.cs" Inherits="#{file_name}" %>)
31
+ output = prefix + "\n" + output
32
+ output.gsub!("<head", '<head runat="server"')
33
+ else
34
+ # replace content_for with <asp:whatevers
35
+ site.render(action.source)
36
+ #prefix = %Q(<%@ Master Language="C#" AutoEventWireup="true" CodeFile="#{file_name}.master.cs" Inherits="#{file_name}" %>)
37
+ #output = prefix + "\n" + output
38
+ end
39
+ end
40
+ end
41
+
42
+ add :build_dotnet, DotNetGenerator
43
+ end
44
+ end
@@ -0,0 +1,23 @@
1
+ module StaticMatic
2
+ module Builder
3
+ class HTMLGenerator < BaseGenerator
4
+ glob! "images", [] # No templates
5
+ glob! "javascripts", [] # No templates
6
+ glob! "stylesheets", site.extentions # Registered templates
7
+
8
+ # Run the dynamic pages through actionpack
9
+ site.dynamic_pages.each do |relative_path|
10
+ output_name = site.rendered_ext(relative_path).gsub("pages/", "")
11
+ template output_name.gsub('.', '_').to_sym, relative_path, output_name
12
+ end
13
+
14
+ # Copy static pages
15
+ site.static_pages.each do |relative_path|
16
+ output_name = relative_path.gsub("pages/", "")
17
+ file output_name.gsub('.', '_').to_sym, relative_path, output_name
18
+ end
19
+ end
20
+
21
+ add :build_html, HTMLGenerator
22
+ end
23
+ end
@@ -18,9 +18,9 @@ module StaticMatic
18
18
  end
19
19
 
20
20
  glob!
21
- empty_directory :images, File.join("src", "images")
22
- empty_directory :javascripts, File.join("src", "javascripts")
23
- empty_directory :build, "build"
21
+ empty_directory :build, "build"
22
+ empty_directory :javascripts, "javascripts"
23
+ empty_directory :images, "images"
24
24
  end
25
25
 
26
26
  add :setup, StaticmaticGenerator
@@ -3,27 +3,36 @@ require 'mongrel'
3
3
  module StaticMatic
4
4
  class Previewer < Mongrel::HttpHandler
5
5
  def initialize(staticmatic)
6
- @files = Mongrel::DirHandler.new(staticmatic.src_dir, false)
6
+ @files = Mongrel::DirHandler.new(staticmatic.root_dir, false)
7
+ @guard = Mutex.new
7
8
  @staticmatic = staticmatic
8
9
  end
9
10
 
10
11
  def process(request, response)
12
+ return if response.socket.closed?
13
+
11
14
  path_info = request.params[Mongrel::Const::PATH_INFO].chomp("/")
12
15
  get_or_head = %w(GET HEAD).include? request.params[Mongrel::Const::REQUEST_METHOD]
13
16
 
14
- if @staticmatic.can_render? path_info
15
- file_ext = File.extname(path_info).gsub(/^\./, '')
16
- file_ext = "html" if file_ext.blank?
17
- file_name = CGI::unescape(path_info).gsub!(/^\//, '') || ''
18
-
19
- response.start(200) do |head, out|
20
- output = @staticmatic.render(file_name)
21
- head[Mongrel::Const::CONTENT_TYPE] = Mongrel::DirHandler::MIME_TYPES[".#{file_ext}"] || "application/octet-stream"
22
- out.write(output || "")
23
- end
24
- elsif get_or_head and @files.can_serve(path_info)
25
- @files.process(request, response) # try to serve static file from site dir
26
- end
17
+ # Reload(and therefore can_render?) is not thread-safe... so lame
18
+ @guard.synchronize {
19
+ if @staticmatic.can_render? path_info
20
+ file_ext = File.extname(path_info).gsub(/^\./, '')
21
+ file_ext = "html" if file_ext.blank?
22
+ file_name = CGI::unescape(path_info).gsub!(/^\//, '') || ''
23
+ begin
24
+ response.start(200) do |head, out|
25
+ output = @staticmatic.render(file_name)
26
+ head[Mongrel::Const::CONTENT_TYPE] = Mongrel::DirHandler::MIME_TYPES[".#{file_ext}"] || "application/octet-stream"
27
+ out.write(output || "")
28
+ end
29
+ rescue Errno::EPIPE
30
+ response.socket.close
31
+ end
32
+ elsif get_or_head and @files.can_serve(path_info)
33
+ @files.process(request, response) # try to serve static file from site dir
34
+ end
35
+ }
27
36
  end
28
37
 
29
38
  class << self
@@ -0,0 +1,97 @@
1
+ module StaticMatic
2
+ class Template
3
+ attr_accessor :view, :path
4
+
5
+ def initialize(filename, site)
6
+ @path = filename
7
+ @site = site
8
+
9
+ @view = ActionView::Base.new([], {}, self)
10
+ @view.template_format = determine_format_for(@path)
11
+ @view.finder.view_paths = [ @site.root_dir ]
12
+ @view.instance_variable_set("@staticmatic", @site)
13
+ end
14
+
15
+ def can_render?
16
+ @view.finder.class.reload!
17
+ @view.finder.file_exists?(strip_extension(@path))
18
+ end
19
+
20
+ def render
21
+ load_helpers
22
+
23
+ @view.instance_variable_set("@current_page", File.basename(@path))
24
+ output = @view.render_file(strip_extension(@path), true)
25
+ return output if layout === false
26
+
27
+ render_with_layout(layout, output)
28
+ rescue Exception => e
29
+ rescue_from_error(e)
30
+ end
31
+
32
+ def rendered_ext
33
+ @path.gsub(Regexp.new('\.(' + @site.extentions.join('|') + ')'), '')
34
+ end
35
+
36
+ protected
37
+ def render_with_layout(layout, content)
38
+ #layout = full_template_path(layout).gsub("pages/", "layouts/")
39
+ @view.instance_variable_set("@content_for_layout", content)
40
+ output = @view.render_file("layouts/#{layout}", true)
41
+
42
+ # little bit of cleaning for content_for blocks which concat by default (wtf?)
43
+ @view.instance_variables.grep(/content_for_|layout/).select do |name|
44
+ name != "content_for_layout"
45
+ end.each do |name|
46
+ @view.instance_variable_set(name, nil)
47
+ end
48
+
49
+ output
50
+ end
51
+
52
+ def layout
53
+ return false unless @path.include? "pages"
54
+
55
+ layout = @view.instance_variable_get("@layout")
56
+ layout = "site" if layout.nil? # @layout = false is valid
57
+ layout
58
+ end
59
+
60
+ def rescue_from_error(exception)
61
+ rescue_template = (exception == ActionView::TemplateError) ? "template_error" : "default_error"
62
+ error_template_path = File.expand_path(File.dirname(__FILE__) + "/templates/rescues/#{rescue_template}.html.erb")
63
+
64
+ @view.instance_variable_set("@exception", exception)
65
+ @view.render_file(error_template_path, false)
66
+ end
67
+
68
+ # Return the format for a given template path
69
+ #
70
+ # For example: application.css.sass -> :css
71
+ def determine_format_for(filename)
72
+ if File.extname(filename).empty? || filename =~ /(\.html|\.haml)/
73
+ extension = ".html"
74
+ elsif filename =~ /(\.css|\.sass)/
75
+ extension = ".css"
76
+ else
77
+ extension = File.extname(filename)
78
+ end
79
+
80
+ extension[1,extension.length-1].to_sym
81
+ end
82
+
83
+ # Load all helpers from src/helpers/
84
+ def load_helpers
85
+ @site.helpers.each do |helper|
86
+ load(helper)
87
+ module_name = File.basename(helper, '.rb').gsub(/(^|\_)./) { |c| c.upcase }.gsub(/\_/, '')
88
+ ActionView::Base.class_eval("include #{module_name}")
89
+ end
90
+ end
91
+
92
+ # Remove the extension from a given template path
93
+ def strip_extension(template)
94
+ template.split('.').first
95
+ end
96
+ end
97
+ end
File without changes
@@ -7,6 +7,6 @@ describe "ActionView Helper Integration" do
7
7
  end
8
8
 
9
9
  it "should render partial" do
10
- @staticmatic.view.render(:partial => "pages/form").should include("This is a form")
10
+ @staticmatic.render("partial_test").should include("This is a form")
11
11
  end
12
12
  end
@@ -7,16 +7,18 @@ describe "Asset Helpers" do
7
7
 
8
8
  include StaticMatic::Helpers::AssetTagHelper
9
9
  before :all do
10
- @sample_site_path = File.dirname(__FILE__) + "/fixtures/sample"
11
- @staticmatic = StaticMatic::Base.new(@sample_site_path)
12
- end
13
-
14
- it "should render partial" do
15
- @staticmatic.view.render(:partial => "pages/form").should include("This is a form")
10
+ @staticmatic = StaticMatic::Base.new(File.dirname(__FILE__) + "/fixtures/sample")
16
11
  end
17
12
 
18
13
  it "should generate stylesheet link" do
19
- @relative_path_to_root = @staticmatic.calculate_relative_path_to_root('pages/services/web_development/costs')
14
+ @relative_path_to_root = calculate_relative_path_to_root('pages/services/web_development/costs')
20
15
  stylesheet_link_tag("site").should include("../../stylesheets/site.css")
21
16
  end
17
+
18
+ def calculate_relative_path_to_root(template)
19
+ return '' if template.match(/^((\.\.?)?\/|\#|.+?\:)/)
20
+
21
+ current_page_depth = template.split('/').length - 2;
22
+ (current_page_depth > 0) ? ([ '..' ] * current_page_depth).join('/') + '/' : ''
23
+ end
22
24
  end
data/spec/base_spec.rb CHANGED
@@ -6,6 +6,23 @@ describe StaticMatic::Base do
6
6
  @staticmatic = StaticMatic::Base.new(@sample_site_path)
7
7
  end
8
8
 
9
+ it "should list static pages" do
10
+ @staticmatic.static_pages.should include("pages/static.html")
11
+ end
12
+
13
+ # Depending on system gems, markdown/textile files would be listed here too
14
+ it "should list dynamic pages" do
15
+ ["pages/haml_test.html.haml", "pages/hello_world.html.erb", "pages/index.html.erb",
16
+ "pages/no_layout.html.erb", "pages/page_with_error.html.haml", "pages/services/index.html.erb",
17
+ "pages/services/web_development.html.erb", "pages/specify_layout.html.erb"].each do |path|
18
+ @staticmatic.dynamic_pages.should include(path)
19
+ end
20
+ end
21
+
22
+ it "should list layouts" do
23
+ @staticmatic.layouts.should == %w(site.html.erb specified_layout.html.erb)
24
+ end
25
+
9
26
  it "should catch any haml template errors" do
10
27
  output = @staticmatic.render("page_with_error")
11
28
  output.should include("Illegal nesting")
@@ -17,13 +34,13 @@ describe StaticMatic::Base do
17
34
  end
18
35
 
19
36
  it "should load custom helpers" do
20
- @staticmatic.view.respond_to?(:say).should be_true
37
+ @staticmatic.template("haml_test").view.respond_to?(:say).should be_true
21
38
  end
22
39
 
23
40
  it "should determine template directory for file" do
24
- @staticmatic.full_template_path("stylesheets/site.css").should_not include("pages/")
25
- @staticmatic.full_template_path("hello_world.html").should include("pages/")
26
- @staticmatic.full_template_path("haml_test").should include("pages/")
41
+ @staticmatic.template("stylesheets/site.css").path.should_not include("pages/")
42
+ @staticmatic.template("hello_world.html").path.should include("pages/")
43
+ @staticmatic.template("haml_test").path.should include("pages/")
27
44
  end
28
45
 
29
46
  it "should know if we can render a template or not" do
@@ -32,11 +49,6 @@ describe StaticMatic::Base do
32
49
  end
33
50
 
34
51
  it "should add index if needed" do
35
- @staticmatic.full_template_path("services").should == "pages/services/index"
36
- end
37
-
38
- it "should know the relative path to the base dir" do
39
- @staticmatic.calculate_relative_path_to_root("pages/services/web_development/costs").should == "../../"
40
- @staticmatic.calculate_relative_path_to_root("pages/services/web_development").should == "../"
52
+ @staticmatic.template("services").path.should == "pages/services/index"
41
53
  end
42
54
  end
data/spec/builder_spec.rb CHANGED
@@ -1,33 +1,29 @@
1
- # require File.join(File.dirname(__FILE__), "..", "lib", "staticmatic")
2
- # require 'templater'
3
- #
4
- # describe StaticMatic::Builder do
5
- # before :each do
6
- # @root_dir = File.dirname(__FILE__) + "/fixtures/builder-test"
7
- #
8
- # orig_stdout = STDOUT.dup
9
- # STDOUT.reopen('/dev/null') # redirect stdout to /dev/null
10
- # StaticMatic::Builder.run_cli(File.dirname(@root_dir), 'staticmatic', 1, ["setup", File.basename(@root_dir)])
11
- # STDOUT.reopen(orig_stdout) # restore stdout
12
- # end
13
- #
14
- # def build_site
15
- # @staticmatic = StaticMatic::Base.new(@root_dir)
16
- # @staticmatic.logger = mock("logger")
17
- # @staticmatic.logger.should_receive(:info).at_least(:once)
18
- # StaticMatic::Builder.build(@staticmatic)
19
- # end
20
- #
21
- # after :each do
22
- # FileUtils.rm_rf(@root_dir)
23
- # end
24
- #
25
- # it "should not build partial files" do
26
- # File.open("#{@root_dir}/src/pages/_partial.html.erb", "w") do |f|
27
- # f.puts "Test"
28
- # end
29
- #
30
- # build_site
31
- # File.exists?("#{@root_dir}/build/_partial.html").should be_false
32
- # end
33
- # end
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "staticmatic")
2
+
3
+ describe StaticMatic::Builder do
4
+ before :each do
5
+ @root_dir = File.expand_path(File.join(File.dirname(__FILE__), "fixtures", "builder-test"))
6
+ @init_cmd = File.expand_path(File.join(File.dirname(__FILE__), "..", "bin", "sm-init"))
7
+ @build_cmd = File.expand_path(File.join(File.dirname(__FILE__), "..", "bin", "sm-build"))
8
+
9
+ `cd #{File.dirname(@root_dir)} && #{@init_cmd} #{File.basename(@root_dir)}`
10
+ end
11
+
12
+ after :each do
13
+ FileUtils.rm_rf(@root_dir)
14
+ end
15
+
16
+ it "should build normal files" do
17
+ `cd #{@root_dir} && #{@build_cmd}`
18
+ File.exists?("#{@root_dir}/build/index.html").should_not be_false
19
+ end
20
+
21
+ it "should not build partial files" do
22
+ File.open("#{@root_dir}/pages/_partial.html.erb", "w") do |f|
23
+ f.puts "Test"
24
+ end
25
+
26
+ `cd #{@root_dir} && #{@build_cmd}`
27
+ File.exists?("#{@root_dir}/build/_partial.html").should be_false
28
+ end
29
+ end
@@ -0,0 +1 @@
1
+ <%= render(:partial => "pages/form") %>
@@ -0,0 +1 @@
1
+ Static, no code!
data/spec/layouts_spec.rb CHANGED
@@ -18,8 +18,7 @@ describe StaticMatic::Base do
18
18
  end
19
19
 
20
20
  it "should clean layout variable for next request" do
21
- output = @staticmatic.render("specify_layout")
22
- @staticmatic.view.instance_variable_get("@layout").should be_nil
21
+ @staticmatic.template("specify_layout").view.instance_variable_get("@layout").should be_nil
23
22
  end
24
23
 
25
24
  it "should allow disabling the layout" do
@@ -25,27 +25,6 @@ describe StaticMatic::Base do
25
25
  output = @staticmatic.render("haml_test")
26
26
  output.should include("<strong>Hello from haml</strong>")
27
27
  end
28
-
29
- if ActionView::Template.template_handler_extensions.include? :markdown
30
- it "should render markdown template" do
31
- output = @staticmatic.render("markdown_test")
32
- output.should match(/<strong>Hello from markdown<\/strong>/)
33
- end
34
- end
35
-
36
- if ActionView::Template.template_handler_extensions.include? :textile
37
- it "should render textile template" do
38
- output = @staticmatic.render("textile_test")
39
- output.should match(/<strong>Hello from textile<\/strong>/)
40
- end
41
- end
42
-
43
- if ActionView::Template.template_handler_extensions.include? :liquid
44
- it "should render liquid template" do
45
- output = @staticmatic.render("liquid_test")
46
- output.should match(/<strong>Hello from liquid<\/strong>/)
47
- end
48
- end
49
28
 
50
29
  it "should register a css renderer" do
51
30
  output = @staticmatic.render("stylesheets/site.css")
@@ -54,16 +33,16 @@ describe StaticMatic::Base do
54
33
  end
55
34
 
56
35
  it "should setup up css type correctly" do
57
- @staticmatic.determine_format_for("stylesheets/site.css").should == :css
36
+ @staticmatic.template("stylesheets/site.css").view.template_format.should == :css
58
37
  end
59
38
 
60
39
  it "should determine format for file with extension" do
61
- @staticmatic.determine_format_for("stylesheets/site.css.sass").should == :css
40
+ @staticmatic.template("stylesheets/site.css.sass").view.template_format.should == :css
62
41
  end
63
42
 
64
43
  it "should have sensible defaults for haml & sass" do
65
- @staticmatic.determine_format_for("stylesheets/site.sass").should == :css
66
- @staticmatic.determine_format_for("pages/test.haml").should == :html
44
+ @staticmatic.template("stylesheets/site.sass").view.template_format.should == :css
45
+ @staticmatic.template("pages/test.haml").view.template_format.should == :html
67
46
  end
68
47
 
69
48
  it "should render index template from sub-directory" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tdreyno-staticmatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.4
4
+ version: 2.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Bartholomew
@@ -10,20 +10,20 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-09-05 00:00:00 -07:00
14
- default_executable: staticmatic
13
+ date: 2008-11-05 00:00:00 -08:00
14
+ default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
- name: templater
17
+ name: mongrel
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.3.3
23
+ version: "0"
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
- name: mongrel
26
+ name: rubyzip
27
27
  version_requirement:
28
28
  version_requirements: !ruby/object:Gem::Requirement
29
29
  requirements:
@@ -31,6 +31,15 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: "0"
33
33
  version:
34
+ - !ruby/object:Gem::Dependency
35
+ name: templater
36
+ version_requirement:
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 0.3.5
42
+ version:
34
43
  - !ruby/object:Gem::Dependency
35
44
  name: haml
36
45
  version_requirement:
@@ -45,38 +54,45 @@ dependencies:
45
54
  version_requirement:
46
55
  version_requirements: !ruby/object:Gem::Requirement
47
56
  requirements:
48
- - - ">="
57
+ - - "="
49
58
  - !ruby/object:Gem::Version
50
- version: 2.1.0
59
+ version: 2.1.2
51
60
  version:
52
61
  - !ruby/object:Gem::Dependency
53
62
  name: activesupport
54
63
  version_requirement:
55
64
  version_requirements: !ruby/object:Gem::Requirement
56
65
  requirements:
57
- - - ">="
66
+ - - "="
58
67
  - !ruby/object:Gem::Version
59
- version: 2.1.0
68
+ version: 2.1.2
60
69
  version:
61
70
  description:
62
71
  email: tdreyno@gmail.com
63
72
  executables:
64
- - staticmatic
73
+ - sm-init
74
+ - sm-build
75
+ - sm-server
65
76
  extensions: []
66
77
 
67
78
  extra_rdoc_files: []
68
79
 
69
80
  files:
70
81
  - LICENSE
71
- - bin/staticmatic
82
+ - bin/sm-init
83
+ - bin/sm-build
84
+ - bin/sm-server
72
85
  - lib/staticmatic.rb
73
86
  - lib/staticmatic/autoload.rb
74
87
  - lib/staticmatic/base.rb
75
88
  - lib/staticmatic/builder.rb
89
+ - lib/staticmatic/builder/html.rb
90
+ - lib/staticmatic/builder/dotnet.rb
76
91
  - lib/staticmatic/config.rb
77
92
  - lib/staticmatic/deprecation.rb
78
93
  - lib/staticmatic/generator.rb
79
94
  - lib/staticmatic/previewer.rb
95
+ - lib/staticmatic/template.rb
80
96
  - lib/staticmatic/actionpack_support/mime.rb
81
97
  - lib/staticmatic/actionpack_support/remove_partial_benchmark.rb
82
98
  - lib/staticmatic/actionpack_support/remove_controller_caching.rb
@@ -90,12 +106,10 @@ files:
90
106
  - lib/staticmatic/template_handlers/textile.rb
91
107
  - lib/staticmatic/templates/rescues/default_error.html.erb
92
108
  - lib/staticmatic/templates/rescues/template_error.html.erb
93
- - lib/templates/script/server
94
- - lib/templates/script/builder
95
- - lib/templates/src/helpers/site_helper.rb
96
- - lib/templates/src/layouts/site.html.haml
97
- - lib/templates/src/pages/index.html.haml
98
- - lib/templates/src/stylesheets/site.css.sass
109
+ - lib/templates/helpers/site_helper.rb
110
+ - lib/templates/layouts/site.html.haml
111
+ - lib/templates/pages/index.html.haml
112
+ - lib/templates/stylesheets/site.css.sass
99
113
  - spec/action_view_helpers_spec.rb
100
114
  - spec/asset_helpers_spec.rb
101
115
  - spec/base_spec.rb
@@ -104,8 +118,6 @@ files:
104
118
  - spec/deprecation_spec.rb
105
119
  - spec/layouts_spec.rb
106
120
  - spec/rendering_spec.rb
107
- - spec/fixtures/sample/Rakefile
108
- - spec/fixtures/sample/config.rb
109
121
  - spec/fixtures/sample/build/haml_test.html
110
122
  - spec/fixtures/sample/build/hello_world.html
111
123
  - spec/fixtures/sample/build/index.html
@@ -113,21 +125,23 @@ files:
113
125
  - spec/fixtures/sample/build/services/index.html
114
126
  - spec/fixtures/sample/build/services/web_development.html
115
127
  - spec/fixtures/sample/build/stylesheets/site.css
116
- - spec/fixtures/sample/src/helpers/speech_helper.rb
117
- - spec/fixtures/sample/src/layouts/site.html.erb
118
- - spec/fixtures/sample/src/layouts/specified_layout.html.erb
119
- - spec/fixtures/sample/src/pages/_form.html.erb
120
- - spec/fixtures/sample/src/pages/haml_test.html.haml
121
- - spec/fixtures/sample/src/pages/hello_world.html.erb
122
- - spec/fixtures/sample/src/pages/index.html.erb
123
- - spec/fixtures/sample/src/pages/liquid_test.html.liquid
124
- - spec/fixtures/sample/src/pages/markdown_test.html.markdown
125
- - spec/fixtures/sample/src/pages/page_with_error.html.haml
126
- - spec/fixtures/sample/src/pages/specify_layout.html.erb
127
- - spec/fixtures/sample/src/pages/textile_test.html.textile
128
- - spec/fixtures/sample/src/pages/services/index.html.erb
129
- - spec/fixtures/sample/src/pages/services/web_development.html.erb
130
- - spec/fixtures/sample/src/stylesheets/site.css.sass
128
+ - spec/fixtures/sample/helpers/speech_helper.rb
129
+ - spec/fixtures/sample/layouts/site.html.erb
130
+ - spec/fixtures/sample/layouts/specified_layout.html.erb
131
+ - spec/fixtures/sample/pages/_form.html.erb
132
+ - spec/fixtures/sample/pages/haml_test.html.haml
133
+ - spec/fixtures/sample/pages/hello_world.html.erb
134
+ - spec/fixtures/sample/pages/index.html.erb
135
+ - spec/fixtures/sample/pages/partial_test.html.erb
136
+ - spec/fixtures/sample/pages/liquid_test.html.liquid
137
+ - spec/fixtures/sample/pages/markdown_test.html.markdown
138
+ - spec/fixtures/sample/pages/page_with_error.html.haml
139
+ - spec/fixtures/sample/pages/specify_layout.html.erb
140
+ - spec/fixtures/sample/pages/static.html
141
+ - spec/fixtures/sample/pages/textile_test.html.textile
142
+ - spec/fixtures/sample/pages/services/index.html.erb
143
+ - spec/fixtures/sample/pages/services/web_development.html.erb
144
+ - spec/fixtures/sample/stylesheets/site.css.sass
131
145
  has_rdoc: false
132
146
  homepage: http://github.com/tdreyno/staticmatic
133
147
  post_install_message:
@@ -163,8 +177,6 @@ test_files:
163
177
  - spec/deprecation_spec.rb
164
178
  - spec/layouts_spec.rb
165
179
  - spec/rendering_spec.rb
166
- - spec/fixtures/sample/Rakefile
167
- - spec/fixtures/sample/config.rb
168
180
  - spec/fixtures/sample/build/haml_test.html
169
181
  - spec/fixtures/sample/build/hello_world.html
170
182
  - spec/fixtures/sample/build/index.html
@@ -172,18 +184,20 @@ test_files:
172
184
  - spec/fixtures/sample/build/services/index.html
173
185
  - spec/fixtures/sample/build/services/web_development.html
174
186
  - spec/fixtures/sample/build/stylesheets/site.css
175
- - spec/fixtures/sample/src/helpers/speech_helper.rb
176
- - spec/fixtures/sample/src/layouts/site.html.erb
177
- - spec/fixtures/sample/src/layouts/specified_layout.html.erb
178
- - spec/fixtures/sample/src/pages/_form.html.erb
179
- - spec/fixtures/sample/src/pages/haml_test.html.haml
180
- - spec/fixtures/sample/src/pages/hello_world.html.erb
181
- - spec/fixtures/sample/src/pages/index.html.erb
182
- - spec/fixtures/sample/src/pages/liquid_test.html.liquid
183
- - spec/fixtures/sample/src/pages/markdown_test.html.markdown
184
- - spec/fixtures/sample/src/pages/page_with_error.html.haml
185
- - spec/fixtures/sample/src/pages/specify_layout.html.erb
186
- - spec/fixtures/sample/src/pages/textile_test.html.textile
187
- - spec/fixtures/sample/src/pages/services/index.html.erb
188
- - spec/fixtures/sample/src/pages/services/web_development.html.erb
189
- - spec/fixtures/sample/src/stylesheets/site.css.sass
187
+ - spec/fixtures/sample/helpers/speech_helper.rb
188
+ - spec/fixtures/sample/layouts/site.html.erb
189
+ - spec/fixtures/sample/layouts/specified_layout.html.erb
190
+ - spec/fixtures/sample/pages/_form.html.erb
191
+ - spec/fixtures/sample/pages/haml_test.html.haml
192
+ - spec/fixtures/sample/pages/hello_world.html.erb
193
+ - spec/fixtures/sample/pages/index.html.erb
194
+ - spec/fixtures/sample/pages/partial_test.html.erb
195
+ - spec/fixtures/sample/pages/liquid_test.html.liquid
196
+ - spec/fixtures/sample/pages/markdown_test.html.markdown
197
+ - spec/fixtures/sample/pages/page_with_error.html.haml
198
+ - spec/fixtures/sample/pages/specify_layout.html.erb
199
+ - spec/fixtures/sample/pages/static.html
200
+ - spec/fixtures/sample/pages/textile_test.html.textile
201
+ - spec/fixtures/sample/pages/services/index.html.erb
202
+ - spec/fixtures/sample/pages/services/web_development.html.erb
203
+ - spec/fixtures/sample/stylesheets/site.css.sass
data/bin/staticmatic DELETED
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require File.join(File.dirname(__FILE__), '..', 'lib', 'staticmatic')
5
- StaticMatic::Generator.run_cli(Dir.pwd, 'staticmatic', 1, ["setup"].concat(ARGV))
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require 'staticmatic'
5
- StaticMatic::Builder.run_cli(Dir.pwd, 'builder', 1, ["build", "--force"].concat(ARGV))
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'staticmatic'
4
- StaticMatic::Previewer.start(Dir.pwd)
@@ -1,4 +0,0 @@
1
- require 'rake'
2
-
3
- # Hard-code path to staticmatic for testing
4
- require '../../../lib/tasks/staticmatic'
@@ -1,3 +0,0 @@
1
- StaticMatic::Config.setup(
2
- :custom_option => false
3
- )