staticmatic 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/staticmatic CHANGED
@@ -1,19 +1,13 @@
1
- #!/usr/local/bin/ruby
1
+ #!/usr/bin/env ruby
2
2
  require File.dirname(__FILE__) + '/../lib/staticmatic'
3
3
 
4
4
  command = ARGV[0]
5
5
  directory = ARGV[1]
6
6
 
7
7
  if !command || !directory
8
- puts 'Usage: staticmatic <build|setup> <directory>'
8
+ puts 'Usage: staticmatic <build|setup|preview> <directory>'
9
9
  exit
10
10
  end
11
11
 
12
- staticmatic = StaticMatic.new(directory)
13
-
14
- case command
15
- when 'build'
16
- staticmatic.build
17
- when 'setup'
18
- staticmatic.setup
19
- end
12
+ staticmatic = StaticMatic::Base.new(directory)
13
+ staticmatic.run(command)
@@ -2,8 +2,10 @@
2
2
  %html
3
3
  %head
4
4
  %title Welcome to my site
5
+ = stylesheets
5
6
  %body
6
7
  #menu
7
8
  %ul
8
- %li Test
9
+ %li=link "Home", "/"
10
+ %li=link "Contact"
9
11
  = yield
@@ -1,5 +1,3 @@
1
- %h1 This is a test - with a change
1
+ %h1 Welcome to StaticMatic
2
2
 
3
- = link img('image_path.jpg'), 'index'
4
-
5
- = img 'test'
3
+ %p Managing static site with Haml & Sass
@@ -1,2 +1,4 @@
1
1
  body
2
- :color = #ff0000
2
+ :font
3
+ :family verdana
4
+ :size 10pt
@@ -0,0 +1,158 @@
1
+ module StaticMatic
2
+ class Base
3
+ VERSION = '0.2.0'
4
+
5
+ # Directories generated for a new site setup
6
+ @@base_dirs = %w{
7
+ site/
8
+ site/stylesheets
9
+ site/images
10
+ site/javascripts
11
+ src/
12
+ src/pages/
13
+ src/layouts
14
+ src/stylesheets
15
+ }
16
+
17
+ # Templates for setup and their location
18
+ @@templates = {
19
+ 'application.haml' => 'layouts',
20
+ 'application.sass' => 'stylesheets',
21
+ 'index.haml' => 'pages'
22
+ }
23
+
24
+ def initialize(base_dir)
25
+ @base_dir = base_dir
26
+ @src_dir = "#{@base_dir}/src"
27
+ @site_dir = "#{@base_dir}/site"
28
+ @templates_dir = File.dirname(__FILE__) + '/templates'
29
+ end
30
+
31
+ def run(command)
32
+ valid_commands = ['build', 'setup', 'preview']
33
+
34
+ if valid_commands.include?(command)
35
+ send(command)
36
+ else
37
+ puts "#{command} is not a valid StaticMatic command"
38
+ end
39
+ end
40
+
41
+ def build
42
+ build_css
43
+ build_html
44
+ end
45
+
46
+ def setup
47
+ if !File.exists?(@base_dir)
48
+ Dir.mkdir(@base_dir)
49
+ end
50
+
51
+ @@base_dirs.each do |directory|
52
+ directory = "#{@base_dir}/#{directory}"
53
+ if !File.exists?(directory)
54
+ Dir.mkdir(directory)
55
+ puts "created #{directory}"
56
+ end
57
+ end
58
+
59
+ @@templates.each do |template, destination|
60
+ copy_file("#{@templates_dir}/#{template}", "#{@src_dir}/#{destination}")
61
+ end
62
+
63
+ puts "Done"
64
+ end
65
+
66
+ def preview
67
+ puts "StaticMatic Preview Server Starting..."
68
+ StaticMatic::Server.start(@base_dir)
69
+ end
70
+
71
+ def copy_file(from, to)
72
+ FileUtils.cp(from, to)
73
+ end
74
+
75
+ def save_page(filename, content)
76
+ filename = "#{filename}"
77
+ generate_site_file(filename, 'html', content)
78
+ end
79
+
80
+ def save_stylesheet(filename, content)
81
+ filename = "stylesheets/#{filename}"
82
+ generate_site_file(filename, 'css', content)
83
+ end
84
+
85
+ def generate_site_file(filename, extension, content)
86
+ path = File.join(@site_dir,"#{filename}.#{extension}")
87
+ File.open(path, 'w+') do |f|
88
+ f << content
89
+ end
90
+
91
+ puts "created #{path}"
92
+ end
93
+
94
+ # Keeps track of the layout template to use when generating the site
95
+ #
96
+ def layout=(name)
97
+ if name != @layout_name
98
+ @layout_name = name
99
+ end
100
+ end
101
+
102
+ def layout
103
+ if !@layout_name
104
+ @layout_name = "application"
105
+ end
106
+
107
+ File.read("#{@src_dir}/layouts/#{@layout_name}.haml")
108
+ end
109
+
110
+ # Generate html from source file:
111
+ # generate_html("index.haml")
112
+ def generate_html(source_file)
113
+ page = Haml::Engine.new(File.read("#{@src_dir}/pages/#{source_file}.haml"), :locals => {:base_dir => @base_dir})
114
+ page.render
115
+ end
116
+
117
+ def generate_html_with_layout(source)
118
+ Haml::Engine.new(layout, :locals => {:base_dir => @base_dir}).to_html { generate_html(source) }
119
+ end
120
+
121
+ def generate_css(source)
122
+ stylesheet = Sass::Engine.new(File.read("#{@src_dir}/stylesheets/#{source}.sass"))
123
+ stylesheet.to_css
124
+ end
125
+
126
+ def template_exists?(name)
127
+ File.exists?("#{@src_dir}/pages/#{name}.haml") || File.exists?("#{@src_dir}/stylesheets/#{name}.sass")
128
+ end
129
+
130
+ # Build HTML from the source files
131
+ def build_html
132
+ Dir["#{@src_dir}/pages/**/*.haml"].each do |path|
133
+ template = source_template_from_path(path)
134
+ save_page(template, generate_html_with_layout(template))
135
+ end
136
+ end
137
+
138
+ # Build CSS from the source files
139
+ def build_css
140
+ Dir["#{@src_dir}/stylesheets/**/*.sass"].each do |path|
141
+ template = source_template_from_path(path)
142
+ save_stylesheet(template, generate_css(template))
143
+ end
144
+ end
145
+
146
+ # Returns a raw template name from a source file path:
147
+ # source_template_from_path("/path/to/site/src/stylesheets/application.sass") -> "application"
148
+ def source_template_from_path(path)
149
+ File.basename(path).chomp(File.extname(path))
150
+ end
151
+
152
+ class << self
153
+ def base_dirs
154
+ @@base_dirs
155
+ end
156
+ end
157
+ end
158
+ end
@@ -1,14 +1,13 @@
1
1
  module Haml
2
2
  module Helpers
3
3
 
4
- # Generates links to all stylesheets in the site's directory
4
+ # Generates links to all stylesheets in the source directory
5
5
  def stylesheets
6
- stylesheet_dir = "#{base_dir}/site/stylesheets/"
6
+ stylesheet_dir = "#{base_dir}/src/stylesheets"
7
7
  output = ""
8
- Dir.entries(stylesheet_dir).each do |file|
9
- if file.match(/([a-z0-9_-]+)\.css$/)
10
- output << tag(:link, :href => "stylesheets/#{file}", :rel => 'stylesheet', :media => 'all')
11
- end
8
+ Dir["#{stylesheet_dir}/**/*.sass"].each do |path|
9
+ filename_without_extension = File.basename(path).chomp(File.extname(path))
10
+ output << tag(:link, :href => "stylesheets/#{filename_without_extension}.css", :rel => 'stylesheet', :media => 'all')
12
11
  end
13
12
 
14
13
  output
@@ -0,0 +1,67 @@
1
+ module StaticMatic
2
+ class Server < Mongrel::HttpHandler
3
+ @@file_only_methods = ["GET","HEAD"]
4
+
5
+ def initialize(base_dir)
6
+ @files = Mongrel::DirHandler.new("#{base_dir}/site",false)
7
+ @staticmatic = StaticMatic::Base.new(base_dir)
8
+ end
9
+
10
+ def process(request, response)
11
+ path_info = request.params[Mongrel::Const::PATH_INFO]
12
+ get_or_head = @@file_only_methods.include? request.params[Mongrel::Const::REQUEST_METHOD]
13
+
14
+ file_to_serve = path_info
15
+
16
+ file_name, file_ext = path_info.split('.')
17
+
18
+ if file_name == "/"
19
+ file_name = "index"
20
+ file_ext = "html"
21
+ end
22
+
23
+ # remove stylesheets/ directory if applicable
24
+ file_name.gsub!("/stylesheets/", "")
25
+
26
+ if file_ext && file_ext.match(/html|css/)
27
+ response.start(200) do |head, out|
28
+ output = ""
29
+
30
+ if @staticmatic.template_exists?(file_name)
31
+ if file_ext == "html"
32
+ output = @staticmatic.generate_html_with_layout("#{file_name}")
33
+ elsif file_ext == "css"
34
+ output = @staticmatic.generate_css("#{file_name}")
35
+ end
36
+ else
37
+ output = "File not Found"
38
+ end
39
+ out.write output
40
+ end
41
+ else
42
+ # try to serve static file from site dir
43
+ if @files.can_serve(path_info)
44
+ @files.process(request,response)
45
+ end
46
+ end
47
+ end
48
+
49
+ class << self
50
+ # Starts the StaticMatic preview server
51
+ #
52
+ # StaticMatic.start('/path/to/site/')
53
+ #
54
+ def start(base_dir, port = 3000)
55
+ config = Mongrel::Configurator.new :host => "127.0.0.1" do
56
+ puts "Running Preview of #{base_dir} on 127.0.0.1:#{port}"
57
+ listener :port => port do
58
+ uri "/", :handler => Server.new(base_dir)
59
+ end
60
+ trap("INT") { stop }
61
+ run
62
+ end
63
+ config.join
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,2 +1,4 @@
1
1
  body
2
- :font-family = "Verdana, Helvetica, sans-serif"
2
+ :font
3
+ :family Verdana
4
+ :size 10pt
data/lib/staticmatic.rb CHANGED
@@ -1,112 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'haml'
3
+ require 'mongrel'
3
4
  require File.dirname(__FILE__) + '/staticmatic/helpers'
4
-
5
- class StaticMatic
6
- VERSION = '0.1.0'
7
-
8
- # Directories generated for a new site setup
9
- cattr_reader :base_dirs
10
- @@base_dirs = %w{
11
- site/
12
- site/stylesheets
13
- site/images
14
- site/javascripts
15
- src/
16
- src/pages/
17
- src/layouts
18
- src/stylesheets
19
- }
20
-
21
- # Templates for setup and their location
22
- @@templates = {
23
- 'application.haml' => 'layouts',
24
- 'application.sass' => 'stylesheets',
25
- 'index.haml' => 'pages'
26
- }
27
-
28
- def initialize(base_dir)
29
- @base_dir = base_dir
30
- @src_dir = "#{@base_dir}/src"
31
- @site_dir = "#{@base_dir}/site"
32
- @templates_dir = File.dirname(__FILE__) + '/staticmatic/templates'
33
- end
34
-
35
- def build
36
- build_css
37
- build_html
38
- end
39
-
40
- def setup
41
- if !File.exists?(@base_dir)
42
- Dir.mkdir(@base_dir)
43
- end
44
-
45
- @@base_dirs.each do |directory|
46
- directory = "#{@base_dir}/#{directory}"
47
- if !File.exists?(directory)
48
- Dir.mkdir(directory)
49
- puts "created #{directory}"
50
- end
51
- end
52
-
53
- @@templates.each do |template, destination|
54
- copy_file("#{@templates_dir}/#{template}", "#{@src_dir}/#{destination}")
55
- end
56
-
57
- puts "Done"
58
- end
59
-
60
- private
61
-
62
- def copy_file(from, to)
63
- `cp #{from} #{to}`
64
- end
65
-
66
- def save_page(filename, content)
67
- filename = "#{filename}"
68
- generate_site_file(filename, 'html', content)
69
- end
70
-
71
- def save_stylesheet(filename, content)
72
- filename = "stylesheets/#{filename}"
73
- generate_site_file(filename, 'css', content)
74
- end
75
-
76
- def generate_site_file(filename, extension, content)
77
- site_file = File.new("#{@site_dir}/#{filename}.#{extension}", 'w+')
78
- site_file << content
79
- site_file.close
80
- puts "created #{@site_dir}/#{filename}.#{extension}"
81
- end
82
-
83
- def build_html
84
- layout_content = File.read("#{@src_dir}/layouts/application.haml")
85
-
86
- Dir.entries("#{@src_dir}/pages").each do |file|
87
- if matches = file.match(/([a-z0-9_-]+)\.haml$/)
88
-
89
- filename_without_extension = matches[1]
90
-
91
- page = Haml::Engine.new(File.read("#{@src_dir}/pages/#{file}"), :locals => {:base_dir => @base_dir})
92
- page_content = page.render
93
-
94
- content_with_layout = Haml::Engine.new(layout_content, :locals => {:base_dir => @base_dir}).render { page_content }
95
-
96
- save_page(filename_without_extension, content_with_layout)
97
- end
98
- end
99
- end
100
-
101
- def build_css
102
- Dir.entries("#{@src_dir}/stylesheets").each do |file|
103
- if matches = file.match(/([a-z0-9_-]+)\.sass$/)
104
- filename_without_extension = matches[1]
105
-
106
- stylesheet = Sass::Engine.new(File.read("#{@src_dir}/stylesheets/#{file}"))
107
-
108
- save_stylesheet(filename_without_extension, stylesheet.render)
109
- end
110
- end
111
- end
112
- end
5
+ require File.dirname(__FILE__) + '/staticmatic/base'
6
+ require File.dirname(__FILE__) + '/staticmatic/server'
data/test/base_test.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/staticmatic'
3
+
4
+ class StaticMaticBaseTest < Test::Unit::TestCase
5
+ def setup
6
+ @base_dir = File.dirname(__FILE__) + '/sandbox/test_site'
7
+ @tmp_dir = File.dirname(__FILE__) + '/sandbox/tmp'
8
+
9
+ @staticmatic = StaticMatic::Base.new(@base_dir)
10
+ end
11
+
12
+ def test_should_setup_directories
13
+ staticmatic = StaticMatic::Base.new(@tmp_dir)
14
+ staticmatic.run('setup')
15
+
16
+ StaticMatic::Base.base_dirs.each do |dir|
17
+ assert File.exists?("#{@tmp_dir}/#{dir}"), "Should create #{dir}"
18
+ end
19
+
20
+ StaticMatic::Base.base_dirs.reverse.each do |dir|
21
+ Dir.entries("#{@tmp_dir}/#{dir}").each do |file|
22
+ next if file.match(/^\./)
23
+ File.delete("#{@tmp_dir}/#{dir}/#{file}")
24
+ end
25
+ Dir.delete("#{@tmp_dir}/#{dir}") if File.exists?("#{@tmp_dir}/#{dir}")
26
+ end
27
+
28
+ end
29
+
30
+ def test_should_set_layout_template
31
+ @staticmatic.layout = "application"
32
+ assert_match "StaticMatic - Test Site Template", @staticmatic.layout
33
+ end
34
+
35
+ def test_should_generate_html_with_layout
36
+ content = @staticmatic.generate_html_with_layout("index")
37
+ assert_match "StaticMatic - Test Site Template", content
38
+ assert_match "This is some test content", content
39
+ end
40
+
41
+ def test_should_generate_css
42
+ content = @staticmatic.generate_css("application")
43
+ end
44
+
45
+ def test_should_find_source_filename_from_path
46
+ assert_equal "application", @staticmatic.source_template_from_path("@base_dir/src/stylesheets/application.css")
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/staticmatic'
3
+
4
+ class StaticMaticServerTest < Test::Unit::TestCase
5
+ def setup
6
+ @base_dir = File.dirname(__FILE__) + '/sandbox/tmp'
7
+ end
8
+
9
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: staticmatic
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2007-05-27 00:00:00 +01:00
6
+ version: 0.2.0
7
+ date: 2007-06-10 00:00:00 +01:00
8
8
  summary: Manage static sites using Haml & Sass
9
9
  require_paths:
10
10
  - lib
@@ -53,14 +53,17 @@ files:
53
53
  - example/src/stylesheets/application.sass
54
54
  - lib/staticmatic
55
55
  - lib/staticmatic.rb
56
+ - lib/staticmatic/base.rb
56
57
  - lib/staticmatic/helpers.rb
58
+ - lib/staticmatic/server.rb
57
59
  - lib/staticmatic/templates
58
60
  - lib/staticmatic/templates/application.haml
59
61
  - lib/staticmatic/templates/application.sass
60
62
  - lib/staticmatic/templates/index.haml
63
+ - test/base_test.rb
61
64
  - test/helpers_test.rb
62
65
  - test/sandbox
63
- - test/staticmatic_test.rb
66
+ - test/server_test.rb
64
67
  - test/sandbox/test_site
65
68
  - test/sandbox/tmp
66
69
  - test/sandbox/test_site/config.rb
@@ -78,8 +81,9 @@ files:
78
81
  - test/sandbox/test_site/src/pages/index.haml
79
82
  - test/sandbox/test_site/src/stylesheets/application.sass
80
83
  test_files:
84
+ - test/base_test.rb
81
85
  - test/helpers_test.rb
82
- - test/staticmatic_test.rb
86
+ - test/server_test.rb
83
87
  rdoc_options: []
84
88
 
85
89
  extra_rdoc_files: []
@@ -1,27 +0,0 @@
1
- require 'test/unit'
2
- require File.dirname(__FILE__) + '/../lib/staticmatic'
3
-
4
- class StaticMaticTest < Test::Unit::TestCase
5
- def setup
6
- @base_dir = File.dirname(__FILE__) + '/sandbox/tmp'
7
- end
8
-
9
- def test_should_setup_directories
10
- staticmatic = StaticMatic.new(@base_dir)
11
- staticmatic.setup
12
-
13
- StaticMatic.base_dirs.each do |dir|
14
- assert File.exists?("#{@base_dir}/#{dir}"), "Should create #{dir}"
15
- end
16
- end
17
-
18
- def teardown
19
- StaticMatic.base_dirs.reverse.each do |dir|
20
- Dir.entries("#{@base_dir}/#{dir}").each do |file|
21
- next if file.match(/^\./)
22
- File.delete("#{@base_dir}/#{dir}/#{file}")
23
- end
24
- Dir.delete("#{@base_dir}/#{dir}") if File.exists?("#{@base_dir}/#{dir}")
25
- end
26
- end
27
- end