tdreyno-staticmatic 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/bin/staticmatic +2 -14
  2. data/lib/staticmatic/actionpack_support/remove_controller_caching.rb +7 -0
  3. data/lib/staticmatic/autoload.rb +16 -14
  4. data/lib/staticmatic/base.rb +97 -126
  5. data/lib/staticmatic/builder.rb +64 -86
  6. data/lib/staticmatic/config.rb +31 -37
  7. data/lib/staticmatic/previewer.rb +16 -28
  8. data/lib/staticmatic/template_handlers/haml.rb +6 -10
  9. data/lib/staticmatic/template_handlers/liquid.rb +3 -7
  10. data/lib/staticmatic/template_handlers/markdown.rb +3 -7
  11. data/lib/staticmatic/template_handlers/sass.rb +3 -7
  12. data/lib/staticmatic/template_handlers/textile.rb +3 -7
  13. data/lib/staticmatic.rb +16 -12
  14. data/lib/templates/script/builder +6 -0
  15. data/lib/templates/script/server +4 -0
  16. data/spec/action_view_helpers_spec.rb +12 -0
  17. data/spec/asset_helpers_spec.rb +22 -0
  18. data/spec/base_spec.rb +42 -0
  19. data/spec/builder_spec.rb +44 -0
  20. data/spec/config_spec.rb +31 -0
  21. data/spec/deprecation_spec.rb +18 -0
  22. data/spec/fixtures/sample/Rakefile +4 -0
  23. data/spec/fixtures/sample/build/haml_test.html +11 -0
  24. data/spec/fixtures/sample/build/hello_world.html +9 -0
  25. data/spec/fixtures/sample/build/index.html +9 -0
  26. data/spec/fixtures/sample/build/page_with_error.html +61 -0
  27. data/spec/fixtures/sample/build/services/index.html +9 -0
  28. data/spec/fixtures/sample/build/services/web_development.html +9 -0
  29. data/spec/fixtures/sample/build/stylesheets/site.css +3 -0
  30. data/spec/fixtures/sample/config.rb +3 -0
  31. data/spec/fixtures/sample/src/helpers/speech_helper.rb +5 -0
  32. data/spec/fixtures/sample/src/layouts/site.html.erb +9 -0
  33. data/spec/fixtures/sample/src/layouts/specified_layout.html.erb +3 -0
  34. data/spec/fixtures/sample/src/pages/_form.html.erb +1 -0
  35. data/spec/fixtures/sample/src/pages/haml_test.html.haml +3 -0
  36. data/spec/fixtures/sample/src/pages/hello_world.html.erb +1 -0
  37. data/spec/fixtures/sample/src/pages/index.html.erb +1 -0
  38. data/spec/fixtures/sample/src/pages/liquid_test.html.liquid +3 -0
  39. data/spec/fixtures/sample/src/pages/markdown_test.html.markdown +3 -0
  40. data/spec/fixtures/sample/src/pages/page_with_error.html.haml +3 -0
  41. data/spec/fixtures/sample/src/pages/services/index.html.erb +1 -0
  42. data/spec/fixtures/sample/src/pages/services/web_development.html.erb +1 -0
  43. data/spec/fixtures/sample/src/pages/specify_layout.html.erb +1 -0
  44. data/spec/fixtures/sample/src/pages/textile_test.html.textile +3 -0
  45. data/spec/fixtures/sample/src/stylesheets/site.css.sass +3 -0
  46. data/spec/layouts_spec.rb +29 -0
  47. data/spec/rendering_spec.rb +83 -0
  48. metadata +78 -22
  49. data/Rakefile +0 -12
  50. data/app_generators/staticmatic/staticmatic_generator.rb +0 -74
  51. data/app_generators/staticmatic/templates/Rakefile +0 -3
  52. data/lib/staticmatic/rescue.rb +0 -14
  53. data/lib/tasks/staticmatic.rb +0 -9
  54. data/staticmatic.gemspec +0 -52
  55. data/vendor/html-scanner/html/document.rb +0 -68
  56. data/vendor/html-scanner/html/node.rb +0 -530
  57. data/vendor/html-scanner/html/sanitizer.rb +0 -173
  58. data/vendor/html-scanner/html/selector.rb +0 -828
  59. data/vendor/html-scanner/html/tokenizer.rb +0 -105
  60. data/vendor/html-scanner/html/version.rb +0 -11
  61. /data/{app_generators/staticmatic → lib}/templates/src/helpers/site_helper.rb +0 -0
  62. /data/{app_generators/staticmatic → lib}/templates/src/layouts/site.html.haml +0 -0
  63. /data/{app_generators/staticmatic → lib}/templates/src/pages/index.html.haml +0 -0
  64. /data/{app_generators/staticmatic → lib}/templates/src/stylesheets/site.css.sass +0 -0
data/bin/staticmatic CHANGED
@@ -1,17 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'rubygems'
4
- require 'rubigen'
5
-
6
- if %w(-v --version).include? ARGV.first
7
- require 'staticmatic/version'
8
- puts "#{File.basename($0)} #{Staticmatic::VERSION::STRING}"
9
- exit(0)
10
- end
11
-
12
- require 'rubigen/scripts/generate'
13
- source = RubiGen::PathSource.new(:application,
14
- File.join(File.dirname(__FILE__), "../app_generators"))
15
- RubiGen::Base.reset_sources
16
- RubiGen::Base.append_sources source
17
- RubiGen::Scripts::Generate.new.run(ARGV, :generator => 'staticmatic')
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'staticmatic', 'generator')
5
+ StaticMatic::Generator.run_cli(Dir.pwd, 'staticmatic', 1, ["setup"].concat(ARGV))
@@ -0,0 +1,7 @@
1
+ module ActionController
2
+ class Base
3
+ def self.perform_caching
4
+ false
5
+ end
6
+ end
7
+ end
@@ -1,18 +1,20 @@
1
1
  module StaticMatic
2
- autoload :Builder, 'staticmatic/builder'
3
- autoload :Config, 'staticmatic/config'
4
- autoload :Deprecation, 'staticmatic/deprecation'
5
- autoload :Previewer, 'staticmatic/previewer'
6
- autoload :Rescue, 'staticmatic/rescue'
2
+ autoload :Base, File.join(File.dirname(__FILE__), 'base')
3
+ autoload :Config, File.join(File.dirname(__FILE__), 'config')
4
+ autoload :Generator, File.join(File.dirname(__FILE__), 'generator')
5
+ autoload :Deprecation, File.join(File.dirname(__FILE__), 'deprecation')
6
+ autoload :Previewer, File.join(File.dirname(__FILE__), 'previewer')
7
7
 
8
8
  module Helpers
9
- autoload :AssetTagHelper, 'staticmatic/helpers/asset_tag_helper'
10
- autoload :DeprecatedHelpers, 'staticmatic/helpers/deprecated_helpers'
11
- autoload :PageHelper, 'staticmatic/helpers/page_helper'
12
- autoload :UrlHelper, 'staticmatic/helpers/url_helper'
9
+ autoload :AssetTagHelper, File.join(File.dirname(__FILE__), 'helpers', 'asset_tag_helper')
10
+ autoload :PageHelper, File.join(File.dirname(__FILE__), 'helpers', 'page_helper')
11
+ autoload :UrlHelper, File.join(File.dirname(__FILE__), 'helpers', 'url_helper')
13
12
  end
14
- end
15
-
16
- require 'staticmatic/actionpack_support/mime'
17
- require 'staticmatic/actionpack_support/remove_partial_benchmark'
18
- require 'staticmatic/actionpack_support/remove_controller_caching'
13
+
14
+ module TemplateHandlers
15
+ end
16
+
17
+ module Generator
18
+ autoload :BuilderGenerator, File.join(File.dirname(__FILE__), 'builder')
19
+ end
20
+ end
@@ -1,160 +1,131 @@
1
1
  module StaticMatic
2
2
  class Base
3
- include StaticMatic::Rescue
4
3
  include StaticMatic::Deprecation
5
-
6
- attr_accessor :logger
7
- attr_accessor :root_dir
8
- attr_accessor :build_dir
9
- attr_accessor :src_dir
10
- attr_accessor :template
11
- attr_accessor :request
12
-
4
+
5
+ attr_accessor :logger, :root_dir, :src_dir, :build_dir
6
+
13
7
  def initialize(root_dir)
14
- @root_dir = root_dir
15
- @src_dir = "#{@root_dir}/src"
16
- @build_dir = "#{@root_dir}/build"
17
- load_helpers
18
- initialize_config
19
- initialize_logger
20
- initialize_template
21
- end
8
+ @root_dir = root_dir
9
+ @src_dir = File.join(@root_dir, "src")
10
+ @build_dir = File.join(@root_dir, "build")
22
11
 
23
- def initialize_config
12
+ # Setup logger
13
+ @logger = Logger.new(STDERR)
14
+ @logger.level = Logger::INFO
15
+
16
+ # User config
24
17
  StaticMatic::Config.setup
25
18
  config_file = File.join(@root_dir, "config.rb")
26
19
  require config_file if File.exists? config_file
27
-
28
- Haml::Template.options = StaticMatic::Config[:haml_options] if defined?(Haml::Template)
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, '')
29
35
  end
30
36
 
31
- def initialize_logger
32
- @logger = Logger.new($stderr)
33
- @logger.level = Logger::INFO
34
- @logger
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
35
48
  end
36
49
 
37
- def initialize_template
38
- @template = ActionView::Base.new([], {}, self)
39
- @template.template_format = :html
40
- @template.finder.view_paths = [@src_dir]
41
- @template.instance_variable_set("@staticmatic", self)
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('/') + '/' : ''
42
55
  end
56
+
57
+ def render(filename)
58
+ load_helpers
59
+ output = do_render(filename)
43
60
 
44
- def render(template, options = {})
45
- @template.template_format = determine_format_for(template)
46
- template = strip_extension(template)
47
-
48
- begin
49
- @template.render_file(full_template_path(template), true)
50
- rescue Exception => e
51
- rescue_from_error(e)
61
+ return output if determine_format_for(filename) == :css
62
+ layout = view.instance_variable_get("@layout")
63
+ layout = "site" if layout.nil?
64
+ return output unless layout
65
+
66
+ view.instance_variable_set("@content_for_layout", output)
67
+ view.instance_variable_set("@layout", nil) # Clean @layout variable for next request
68
+ do_render("layouts/#{layout}")
69
+ rescue Exception => e
70
+ rescue_from_error(e)
71
+ end
72
+
73
+ def view
74
+ @view ||= begin
75
+ # Initialize View
76
+ view = ActionView::Base.new([], {}, self)
77
+ view.template_format = :html
78
+ view.finder.view_paths = [ @src_dir ]
79
+ view.instance_variable_set("@staticmatic", self)
80
+ view
52
81
  end
53
82
  end
54
-
55
- # Render the given template within the current layout
56
- def render_with_layout(template)
57
- @template.instance_variable_set("@relative_path_to_root", "#{calculate_relative_path_to_root(full_template_path(template))}")
58
- content_for_layout = render(template)
59
-
60
- @template.instance_variable_set("@current_page", template)
61
- @template.instance_variable_set("@content_for_layout", content_for_layout)
62
-
63
- layout = @template.instance_variable_get("@layout")
64
-
65
- # Clean @layout variable for next request
66
- @template.instance_variable_set("@layout", nil)
67
-
68
- layout ||= "site"
69
-
70
- render("layouts/#{layout}")
83
+
84
+ def do_render(filename)
85
+ filename = full_template_path(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)
71
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")
72
94
 
95
+ view.instance_variable_set("@exception", exception)
96
+ view.render_file(error_template_path, false)
97
+ end
98
+
73
99
  # Load all helpers from src/helpers/
74
100
  def load_helpers
75
- Dir["#{@src_dir}/helpers/**/*_helper.rb"].each do |helper|
76
- load helper
101
+ Dir[File.join(@src_dir, 'helpers', '**', '*_helper.rb')].each do |helper|
102
+ load(helper)
77
103
  module_name = File.basename(helper, '.rb').gsub(/(^|\_)./) { |c| c.upcase }.gsub(/\_/, '')
78
104
  ActionView::Base.class_eval("include #{module_name}")
79
105
  end
80
106
  end
107
+
108
+ # Full path to a template, relative to src/
109
+ def full_template_path(template)
110
+ template = template.gsub("#{@src_dir}/", '')
111
+ directory = template_directory_for(template)
112
+ template = File.join(directory, template) unless directory.empty?
113
+
114
+ path = File.join(File.expand_path(src_dir), template)
115
+ template = File.join(template, "index") if File.directory? path
81
116
 
117
+ template
118
+ end
119
+
82
120
  # Return the source directory for a given template
83
- #
84
121
  # Default is pages/
85
- #
86
122
  def template_directory_for(template)
87
- template_directory = "pages"
88
-
89
- %w(stylesheets layouts).each do |directory|
90
- template_directory = "" if template.match(/^(\/)?#{directory}/)
91
- end
92
-
93
- template_directory
94
- end
95
-
96
- # Return the format for a given template path
97
- #
98
- # For example: application.css.sass -> :css
99
- #
100
- def determine_format_for(template)
101
- ext_matches = template.match /\.([a-z0-9]+)/
102
-
103
- # For templates that have only handler extensions, default for backwards compatibility
104
- if ext_matches
105
- unless template.match(/\.([a-z0-9]+)\./)
106
- case ext_matches[1]
107
- when "sass"
108
- extension = :css
109
- when "haml"
110
- extension = :html
111
- end
112
- end
113
-
114
- extension = ext_matches[1].to_sym unless extension
115
- else
116
- extension = :html
117
- end
118
-
119
- extension
123
+ template.match(/^(\/)?(layouts|stylesheets|pages)/) ? "" : "pages"
120
124
  end
121
-
125
+
122
126
  # Remove the extension from a given template path
123
127
  def strip_extension(template)
124
- template.gsub(File.extname(template), '')
125
- end
126
-
127
- # Checks to see if a template exists within a given path
128
- #
129
- # Current only used by the previewer as ActionView handles the actual checking
130
- def can_render?(template)
131
- @template.template_format = determine_format_for(template)
132
- template = strip_extension(template)
133
- @template.finder.class.reload!
134
- @template.finder.file_exists?(full_template_path(template))
135
- end
136
-
137
- # Adds 'index' to a given template path if the path is a directory
138
- # Will render as path/index.html
139
- #
140
- def add_index_if_needed(template)
141
- File.directory?(File.join(File.expand_path(@src_dir), template)) ?
142
- File.join(template, "index") :
143
- template
128
+ template.split('.').first
144
129
  end
145
-
146
- # Full path to a template, relative to src/
147
- def full_template_path(template)
148
- add_index_if_needed(File.join(template_directory_for(template), template))
149
- end
150
-
151
- def calculate_relative_path_to_root(template)
152
- if template.match(/^((\.\.?)?\/|\#|.+?\:)/) == nil
153
- current_page_depth = template.split('/').length - 2;
154
- (current_page_depth > 0) ? ([ '..' ] * current_page_depth).join('/') + '/' : ''
155
- else
156
- ''
157
- end
158
- end
159
130
  end
160
131
  end
@@ -1,102 +1,80 @@
1
+ require 'templater'
2
+
1
3
  module StaticMatic
2
- class Builder
3
- attr_accessor :staticmatic
4
-
5
- def initialize(staticmatic)
6
- @staticmatic = staticmatic
7
- determine_last_build
8
- build_pages
9
- log_version
10
- end
11
-
12
- def determine_last_build
13
- versions_file = @staticmatic.root_dir + "/builds"
14
- @last_build = File.read(versions_file).split(/\n/)[0] if File.exists?(versions_file)
15
- end
16
-
17
- def log_version
18
- return unless StaticMatic::Config[:use_build_tracking]
19
- timestamp = Time.now.strftime("%Y%m%d%H%M%S")
20
- versions_file = @staticmatic.root_dir + "/builds"
4
+ module Generator
5
+ extend Templater::Manifold
6
+ desc "Multiple ways to build a staticmatic site"
7
+
8
+ class BuilderGenerator < Templater::Generator
9
+ # Setup StaticMatic site
10
+ def self.site; @@site ||= ::StaticMatic::Base.new(Dir.pwd); end
11
+ def site; self.class.site; end
12
+
13
+ # Define source and desintation
14
+ def self.source_root; site.src_dir; end
15
+ def destination_root; site.build_dir; end
21
16
 
22
- if File.exists?(versions_file)
23
- current_versions = File.read(versions_file)
24
-
25
- File.open(versions_file, "w") do |file|
26
- file.puts timestamp
27
- file.puts current_versions
28
- end
29
- else
30
- File.open(versions_file, "w") do |file|
31
- file.puts timestamp
17
+ # Override the base template to handle haml/sass/layouts
18
+ option :format, :default => :html, :desc => "Which format to export as (html, rails, dotnet)"
19
+ 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)
32
34
  end
33
35
  end
34
- end
35
-
36
- def build_pages
37
- %w(pages stylesheets).each do |template_path|
38
- Dir["#{@staticmatic.src_dir}/#{template_path}/**/*"].each do |path|
39
- if File.directory? path
40
- unless File.exists? build_path_for(path)
41
- @staticmatic.logger.info("Creating: #{build_path_for(path)}")
42
- FileUtils.mkdir(build_path_for(path))
36
+
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 =~ /^_/
43
49
  end
50
+
51
+ template :rendered_file, relative_path, destination_path
44
52
  else
45
- format = @staticmatic.determine_format_for(path).to_s
46
- base_template_name = base_template_name_for(path)
47
-
48
- @staticmatic.template.template_format = format
49
- build_file_path = "#{build_path_for(path)}"
50
-
51
- if !StaticMatic::Config[:use_build_tracking] || should_overwrite?(path, build_file_path)
52
- output = (format == "html") ?
53
- @staticmatic.render_with_layout(base_template_name) :
54
- @staticmatic.render(base_template_name)
55
-
56
- output_prefix = "#{template_path}/" if template_path != "pages"
57
- save_built_file(build_file_path, output)
58
- end
53
+ file :static_file, relative_path
59
54
  end
60
55
  end
61
56
  end
62
57
  end
63
58
 
64
- def should_overwrite?(template_file, build_file)
65
- return true unless File.exists?(build_file)
66
- file_changed?(template_file)
67
- end
68
-
69
- def file_changed?(src_file)
70
- template_modification_time = File.stat(src_file).mtime.strftime("%Y%m%d%H%M%S")
71
- template_modification_time.to_i > @last_build.to_i
72
- end
73
-
74
- # Strip off src file path and extension
75
- def base_template_name_for(path)
76
- path.gsub("#{@staticmatic.root_dir}/", "").
77
- gsub(/^src\//, '').
78
- gsub(/^pages\//, '').
79
- gsub(/\.[a-z]+$/, '')
80
- end
81
-
82
- # Return an output filename
83
- def build_path_for(path)
84
- File.join(@staticmatic.build_dir, base_template_name_for(path))
85
- end
86
-
87
- # Save contents to the specified file with the given extension to the build directory
88
- def save_built_file(path, contents)
89
- @staticmatic.logger.info("Generating #{path}")
90
- File.open(path, 'w+') do |f|
91
- f << contents
59
+ class HTMLTemplate < Templater::Actions::Template
60
+ def render
61
+ generator.site.render(source)
92
62
  end
93
63
  end
94
64
 
95
- class << self
96
- def build(staticmatic)
97
- staticmatic = StaticMatic::Base.new(staticmatic) if staticmatic.is_a? String
98
- new(staticmatic)
99
- end
100
- end
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
+ add :build, BuilderGenerator
101
79
  end
102
80
  end
@@ -1,47 +1,41 @@
1
- module StaticMatic
2
- class Config
3
- class << self
4
- def defaults
5
- @defaults ||= {
6
- :host => "0.0.0.0",
7
- :port => "3000",
8
- :sass_options => {},
9
- :haml_options => {},
10
- :use_build_tracking => true
11
- }
12
- end
1
+ class StaticMatic::Config
2
+ class << self
3
+ def defaults
4
+ @defaults ||= {
5
+ :host => "0.0.0.0",
6
+ :port => "3000",
7
+ :sass_options => {},
8
+ :haml_options => {},
9
+ :use_build_tracking => true
10
+ }
11
+ end
13
12
 
14
- def setup(settings = {})
15
- @configuration = defaults.merge(settings)
16
- end
17
-
18
- def use
19
- yield @configuration
20
- end
13
+ def setup(settings = {})
14
+ @configuration = defaults.merge(settings)
15
+ end
21
16
 
22
- def key?(key)
23
- @configuration.key?(key)
24
- end
17
+ def key?(key)
18
+ @configuration.key?(key)
19
+ end
25
20
 
26
- def [](key)
27
- (@configuration||={})[key]
28
- end
21
+ def [](key)
22
+ (@configuration||={})[key]
23
+ end
29
24
 
30
- def []=(key,val)
31
- @configuration[key] = val
32
- end
25
+ def []=(key,val)
26
+ @configuration[key] = val
27
+ end
33
28
 
34
- def delete(key)
35
- @configuration.delete(key)
36
- end
29
+ def delete(key)
30
+ @configuration.delete(key)
31
+ end
37
32
 
38
- def fetch(key, default)
39
- @configuration.fetch(key, default)
40
- end
33
+ def fetch(key, default)
34
+ @configuration.fetch(key, default)
35
+ end
41
36
 
42
- def to_hash
43
- @configuration
44
- end
37
+ def to_hash
38
+ @configuration
45
39
  end
46
40
  end
47
41
  end
@@ -2,37 +2,27 @@ require 'mongrel'
2
2
 
3
3
  module StaticMatic
4
4
  class Previewer < Mongrel::HttpHandler
5
- @@file_only_methods = %w(GET HEAD)
6
-
7
5
  def initialize(staticmatic)
8
- @files = Mongrel::DirHandler.new(staticmatic.build_dir, false)
6
+ @files = Mongrel::DirHandler.new(staticmatic.src_dir, false)
9
7
  @staticmatic = staticmatic
10
8
  end
11
9
 
12
10
  def process(request, response)
13
- path_info = request.params[Mongrel::Const::PATH_INFO].chomp("/")
14
- get_or_head = @@file_only_methods.include? request.params[Mongrel::Const::REQUEST_METHOD]
11
+ path_info = request.params[Mongrel::Const::PATH_INFO].chomp("/")
12
+ get_or_head = %w(GET HEAD).include? request.params[Mongrel::Const::REQUEST_METHOD]
15
13
 
16
- if get_or_head and @files.can_serve(path_info)
17
- @files.process(request, response) # try to serve static file from site dir
18
- elsif @staticmatic.can_render? path_info
19
- response.start(200) do |head, out|
20
- file_ext = File.extname(path_info).gsub(/^\./, '')
21
- file_ext = "html" if file_ext.blank?
22
-
23
- file_name = path_info.chomp(".#{file_ext}")
24
- file_name = CGI::unescape(file_name)
25
- file_name.gsub!(/^\//, '')
26
-
27
- head["Content-Type"] = "text/#{file_ext}"
28
- output = ""
14
+ if StaticMatic::Base.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.chomp(".#{file_ext}")).gsub!(/^\//, '')
29
18
 
30
- @staticmatic.load_helpers
31
- output = (file_ext == "css") ?
32
- @staticmatic.render(path_info) :
33
- @staticmatic.render_with_layout(file_name)
34
- out.write output
35
- end
19
+ response.start(200) do |head, out|
20
+ output = Template.new(file_name).render
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
36
26
  end
37
27
  end
38
28
 
@@ -40,8 +30,7 @@ module StaticMatic
40
30
  # Starts the StaticMatic preview server
41
31
  def start(staticmatic)
42
32
  staticmatic = StaticMatic::Base.new(staticmatic) if staticmatic.is_a? String
43
-
44
- config = Mongrel::Configurator.new :host => StaticMatic::Config[:host] do
33
+ Mongrel::Configurator.new :host => StaticMatic::Config[:host] do
45
34
  puts "Running Preview of #{staticmatic.root_dir} on port #{StaticMatic::Config[:port]}"
46
35
  listener :port => StaticMatic::Config[:port] do
47
36
  uri "/", :handler => Previewer.new(staticmatic)
@@ -49,8 +38,7 @@ module StaticMatic
49
38
  end
50
39
  trap("INT") { stop }
51
40
  run
52
- end
53
- config.join
41
+ end.join
54
42
  end
55
43
  end
56
44
  end
@@ -1,18 +1,14 @@
1
1
  require 'haml'
2
2
  require 'haml/engine'
3
3
 
4
- module StaticMatic
5
- module TemplateHandlers
6
- class Haml < ActionView::TemplateHandler
7
- include ActionView::TemplateHandlers::Compilable
4
+ class StaticMatic::TemplateHandlers::Haml < ActionView::TemplateHandler
5
+ include ActionView::TemplateHandlers::Compilable
8
6
 
9
- def compile(template, local_assigns = {})
10
- options = StaticMatic::Config[:haml_options].dup
11
- options[:filename] = template.filename
7
+ def compile(template, local_assigns = {})
8
+ options = StaticMatic::Config[:haml_options].dup
9
+ options[:filename] = template.filename
12
10
 
13
- ::Haml::Engine.new(template.source, options).send(:precompiled_with_ambles, [])
14
- end
15
- end
11
+ ::Haml::Engine.new(template.source, options).send(:precompiled_with_ambles, [])
16
12
  end
17
13
  end
18
14