staticmatic 0.8.6 → 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
data/bin/staticmatic CHANGED
@@ -1,26 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
- require 'optparse'
3
- DEFAULT_PORT = 3000
4
- DEFAULT_HOST = "127.0.0.1"
5
- require File.dirname(__FILE__) + '/../lib/staticmatic'
6
-
7
- $options = {:host=>DEFAULT_HOST,:port=>DEFAULT_PORT}
8
2
 
9
- optparse = OptionParser.new do |opt|
10
- opt.banner = "Usage: #{$0} <build|setup|preview> [OPTIONS]"
11
- opt.separator ""
12
- opt.on "-p", "--port [PORT]", "Select the server port, defaults to #{DEFAULT_PORT}" do |port|
13
- $options[:port] = port
14
- end
15
- opt.on "-h", "--host [HOST]", "Select the server host, defaults to #{DEFAULT_HOST}" do |host|
16
- $options[:host] = host
17
- end
18
- opt.on_tail "-h", "--help", "Display this help message" do
19
- puts opt
20
- exit
21
- end
22
- end
23
- optparse.parse!
3
+ require File.dirname(__FILE__) + '/../lib/staticmatic'
24
4
 
25
5
  command = ARGV[0]
26
6
  directory = ARGV[1]
@@ -30,5 +10,14 @@ if !command || !directory
30
10
  exit
31
11
  end
32
12
 
33
- staticmatic = StaticMatic::Base.new(directory)
13
+ configuration = StaticMatic::Configuration.new
14
+
15
+ config_file = "#{directory}/src/configuration.rb"
16
+
17
+ if File.exists?(config_file)
18
+ config = File.read(config_file)
19
+ eval(config)
20
+ end
21
+
22
+ staticmatic = StaticMatic::Base.new(directory, configuration)
34
23
  staticmatic.run(command)
@@ -12,6 +12,8 @@ module StaticMatic
12
12
  src/pages/
13
13
  src/layouts
14
14
  src/stylesheets
15
+ src/partials
16
+ src/helpers
15
17
  }
16
18
 
17
19
  # Templates for setup and their location
@@ -21,7 +23,11 @@ module StaticMatic
21
23
  'index.haml' => 'pages'
22
24
  }
23
25
 
24
- def initialize(base_dir)
26
+ attr_accessor :configuration
27
+
28
+ def initialize(base_dir, configuration = Configuration.new)
29
+
30
+ @configuration = configuration
25
31
  @base_dir = base_dir
26
32
  @src_dir = "#{@base_dir}/src"
27
33
  @site_dir = "#{@base_dir}/site"
@@ -29,9 +35,9 @@ module StaticMatic
29
35
  @layout = "application"
30
36
  @scope = Object.new
31
37
  @scope.instance_variable_set("@staticmatic", self)
38
+ load_helpers
32
39
  end
33
40
 
34
-
35
41
  def base_dir
36
42
  @base_dir
37
43
  end
@@ -40,6 +46,7 @@ module StaticMatic
40
46
  valid_commands = ['build', 'setup', 'preview']
41
47
 
42
48
  if valid_commands.include?(command)
49
+
43
50
  send(command)
44
51
  else
45
52
  puts "#{command} is not a valid StaticMatic command"
@@ -73,7 +80,7 @@ module StaticMatic
73
80
 
74
81
  def preview
75
82
  puts "StaticMatic Preview Server Starting..."
76
- StaticMatic::Server.start(@base_dir)
83
+ StaticMatic::Server.start(@base_dir, self, configuration)
77
84
  end
78
85
 
79
86
  def copy_file(from, to)
@@ -175,7 +182,6 @@ module StaticMatic
175
182
  # generate_html_from_template_source("content:\n= yield") { "blah" } -> "content: blah"
176
183
  #
177
184
  def generate_html_from_template_source(source)
178
-
179
185
  html = Haml::Engine.new(source)
180
186
 
181
187
  html.render(@scope) { yield }
@@ -241,7 +247,16 @@ module StaticMatic
241
247
  file_name.chomp!(File.extname(file_name))
242
248
  [ file_dir, file_name ]
243
249
  end
244
-
250
+
251
+ # Loads any helpers present in the helpers dir and mixes them into the template helpers
252
+ def load_helpers
253
+
254
+ Dir["#{@src_dir}/helpers/**/*_helper.rb"].each do |helper|
255
+ load helper
256
+ Haml::Helpers.class_eval("include #{File.basename(helper, '.rb').classify}")
257
+ end
258
+ end
259
+
245
260
  class << self
246
261
  def base_dirs
247
262
  @@base_dirs
@@ -0,0 +1,20 @@
1
+ module StaticMatic
2
+ class Configuration
3
+ attr_accessor :preview_server_port
4
+
5
+ attr_accessor :preview_server_host
6
+
7
+ attr_accessor :use_relative_paths_for_links
8
+ attr_accessor :use_relative_paths_for_images
9
+ attr_accessor :use_relative_paths_for_stylesheets
10
+ attr_accessor :use_relative_paths_for_javascripts
11
+
12
+ def initialize
13
+ self.preview_server_port = 3000
14
+ self.use_relative_paths_for_links = false
15
+ self.use_relative_paths_for_images = false
16
+ self.use_relative_paths_for_stylesheets = false
17
+ self.use_relative_paths_for_javascripts = false
18
+ end
19
+ end
20
+ end
@@ -27,9 +27,10 @@ module StaticMatic
27
27
  filename_without_extension = File.basename(path).chomp(File.extname(path))
28
28
  href = "stylesheets/#{filename_without_extension}.css"
29
29
 
30
- if !options[:relative]
30
+ unless @staticmatic.configuration.use_relative_paths_for_stylesheets
31
31
  href = "/#{href}"
32
32
  end
33
+
33
34
  output << tag(:link, :href => href, :rel => 'stylesheet', :media => 'all')
34
35
  end
35
36
 
@@ -44,7 +45,12 @@ module StaticMatic
44
45
  output = ""
45
46
  files.each do |file|
46
47
  if !file.match(/^http\:\/\//)
47
- output << tag(:script, :language => 'javascript', :src => "javascripts/#{file}.js", :type => "text/javascript") { "" }
48
+ src = "javascripts/#{file}.js"
49
+ unless @staticmatic.configuration.use_relative_paths_for_javascripts
50
+ src = "/#{src}"
51
+ end
52
+
53
+ output << tag(:script, :language => 'javascript', :src => src, :type => "text/javascript") { "" }
48
54
  end
49
55
  end
50
56
  output
@@ -64,19 +70,33 @@ module StaticMatic
64
70
  #
65
71
  # link('Test') -> <a href="test.html">Test</a>
66
72
  #
67
- def link(title, page = "", options = {})
68
- if page.blank?
69
- page = urlify(title) + ".html"
73
+ def link(title, href = "", options = {})
74
+
75
+ if href.blank?
76
+ href = urlify(title) + ".html"
70
77
  end
71
78
 
72
- options.merge!(:href => page)
79
+ options.merge!(:href => href)
80
+
81
+ unless @staticmatic.configuration.use_relative_paths_for_links || href.match(/^#|\/|(http\:\/\/)/)
82
+ options[:href] = "/#{options[:href]}"
83
+ end
73
84
 
74
85
  tag(:a, options) { title }
75
86
  end
76
87
 
77
88
  # Generates an image tag
78
89
  def img(name, options = {})
79
- options[:src] = "/images/#{name}"
90
+ if !name.match(/^\//)
91
+ options[:src] = "images/#{name}"
92
+ else
93
+ options[:src] = name
94
+ end
95
+
96
+ unless @staticmatic.configuration.use_relative_paths_for_images || options[:src].match(/^#|\/|(http\:\/\/)/)
97
+ options[:src] = "/#{options[:src]}"
98
+ end
99
+
80
100
  options[:alt] ||= name.split('.').first.capitalize.gsub(/_|-/, ' ')
81
101
  tag :img, options
82
102
  end
@@ -2,9 +2,9 @@ module StaticMatic
2
2
  class Server < Mongrel::HttpHandler
3
3
  @@file_only_methods = ["GET","HEAD"]
4
4
 
5
- def initialize(base_dir)
5
+ def initialize(base_dir, staticmatic)
6
6
  @files = Mongrel::DirHandler.new("#{base_dir}/site",false)
7
- @staticmatic = StaticMatic::Base.new(base_dir)
7
+ @staticmatic = staticmatic
8
8
  end
9
9
 
10
10
  def process(request, response)
@@ -72,11 +72,12 @@ module StaticMatic
72
72
  #
73
73
  # StaticMatic.start('/path/to/site/')
74
74
  #
75
- def start(base_dir, port = $options[:port])
76
- config = Mongrel::Configurator.new :host => $options[:host] do
77
- puts "Running Preview of #{base_dir} on #{$options[:host]}:#{port}"
75
+ def start(base_dir, staticmatic, configuration = Configuration.new)
76
+ port = configuration.preview_server_port || 3000
77
+ config = Mongrel::Configurator.new :host => configuration.preview_server_host do
78
+ puts "Running Preview of #{base_dir} on #{configuration.preview_server_host||"localhost"}:#{port}"
78
79
  listener :port => port do
79
- uri "/", :handler => Server.new(base_dir)
80
+ uri "/", :handler => Server.new(base_dir, staticmatic)
80
81
  end
81
82
  trap("INT") { stop }
82
83
  run
data/lib/staticmatic.rb CHANGED
@@ -5,6 +5,7 @@ require File.dirname(__FILE__) + '/staticmatic/helpers'
5
5
 
6
6
  Haml::Helpers.class_eval("include StaticMatic::Helpers")
7
7
 
8
+ require File.dirname(__FILE__) + '/staticmatic/configuration'
8
9
  require File.dirname(__FILE__) + '/staticmatic/error'
9
10
  require File.dirname(__FILE__) + '/staticmatic/base'
10
11
  require File.dirname(__FILE__) + '/staticmatic/server'
data/test/base_test.rb CHANGED
@@ -4,30 +4,30 @@ require File.dirname(__FILE__) + '/../lib/staticmatic'
4
4
  class StaticMaticBaseTest < Test::Unit::TestCase
5
5
  def setup
6
6
  @base_dir = File.dirname(__FILE__) + '/sandbox/test_site'
7
- @tmp_dir = File.dirname(__FILE__) + '/sandbox/tmp'
8
-
9
7
  @staticmatic = StaticMatic::Base.new(@base_dir)
10
8
  end
11
9
 
12
10
  def test_should_setup_directories
13
- staticmatic = StaticMatic::Base.new(@tmp_dir)
11
+ tmp_dir = File.dirname(__FILE__) + '/sandbox/tmp'
12
+ staticmatic = StaticMatic::Base.new(tmp_dir)
14
13
  staticmatic.run('setup')
15
14
 
16
15
  StaticMatic::Base.base_dirs.each do |dir|
17
- assert File.exists?("#{@tmp_dir}/#{dir}"), "Should create #{dir}"
16
+ assert File.exists?("#{tmp_dir}/#{dir}"), "Should create #{dir}"
18
17
  end
19
18
 
20
19
  StaticMatic::Base.base_dirs.reverse.each do |dir|
21
- Dir.entries("#{@tmp_dir}/#{dir}").each do |file|
20
+ Dir.entries("#{tmp_dir}/#{dir}").each do |file|
22
21
  next if file.match(/^\./)
23
- File.delete("#{@tmp_dir}/#{dir}/#{file}")
22
+ File.delete("#{tmp_dir}/#{dir}/#{file}")
24
23
  end
25
- Dir.delete("#{@tmp_dir}/#{dir}") if File.exists?("#{@tmp_dir}/#{dir}")
24
+ Dir.delete("#{tmp_dir}/#{dir}") if File.exists?("#{tmp_dir}/#{dir}")
26
25
  end
27
26
 
28
27
  end
29
28
 
30
29
  def test_should_generate_html_with_layout
30
+
31
31
  content = @staticmatic.generate_html_with_layout("index")
32
32
  assert_match "StaticMatic", content
33
33
  assert_match "This is some test content", content
@@ -49,4 +49,9 @@ class StaticMaticBaseTest < Test::Unit::TestCase
49
49
  def test_should_find_layout_from_passed_path
50
50
  assert_equal "projects", @staticmatic.detirmine_layout("test/projects")
51
51
  end
52
+
53
+ def test_should_include_custom_helper
54
+ content = @staticmatic.generate_html_with_layout("index")
55
+ assert_match "Hello, Steve!", content
56
+ end
52
57
  end
data/test/helpers_test.rb CHANGED
@@ -12,18 +12,14 @@ class HelpersTest < Test::Unit::TestCase
12
12
  assert_match "href=\"/stylesheets\/application.css\"", stylesheets
13
13
  end
14
14
 
15
- def test_should_generate_stylesheet_links_with_relative_path
16
- assert_match "href=\"stylesheets\/application.css\"", stylesheets(:relative => true)
17
- end
18
-
19
15
  def test_should_autolink_page
20
- expected_output = %q{<a href="test.html">Test</a>}
16
+ expected_output = %q{<a href="/test.html">Test</a>}
21
17
  assert_match expected_output, link("Test")
22
18
  end
23
19
 
24
20
  def test_should_generate_tag_with_block
25
- expected_output = %q{<a href="test.html" title="My Test Link">Test</a>}
26
- assert_match expected_output, tag(:a, :href => "test.html", :title => 'My Test Link') { "Test" }
21
+ expected_output = %q{<a href="/test.html" title="My Test Link">Test</a>}
22
+ assert_match expected_output, tag(:a, :href => "/test.html", :title => 'My Test Link') { "Test" }
27
23
  end
28
24
 
29
25
  def test_should_generate_tag
@@ -41,7 +37,7 @@ class HelpersTest < Test::Unit::TestCase
41
37
  end
42
38
 
43
39
  def test_should_generate_js_links
44
- expected_output = %q{src="javascripts/test.js"}
40
+ expected_output = %q{src="/javascripts/test.js"}
45
41
  assert_match expected_output, javascripts('test')
46
42
  end
47
43
 
@@ -50,4 +46,5 @@ class HelpersTest < Test::Unit::TestCase
50
46
  expected_output = "My Menu"
51
47
  assert_match expected_output, partial("menu")
52
48
  end
49
+
53
50
  end
@@ -0,0 +1,5 @@
1
+ module ApplicationHelper
2
+ def greet(name)
3
+ "Hello, #{name}!"
4
+ end
5
+ end
@@ -1,3 +1,5 @@
1
1
  %h1 StaticMatic!
2
2
 
3
- %p This is some test content
3
+ %p This is some test content
4
+
5
+ = greet("Steve")
@@ -0,0 +1,5 @@
1
+ module ApplicationHelper
2
+ def greet(name)
3
+ "Hello, #{name}"
4
+ end
5
+ end
@@ -7,31 +7,22 @@
7
7
  %body
8
8
  #container
9
9
  #header
10
- .bycurve21= link(img("bycurve21.gif"), "http://www.curve21.com")
10
+ .bycurve21= link(img("/images/bycurve21.gif"), "http://www.curve21.com")
11
11
  .title StaticMatic
12
12
 
13
13
  #menu
14
14
 
15
15
  %ul
16
16
  %li= link "Home", "/"
17
- %li= link "Download", "/download.html"
18
- %li= link "How to use", "/how_to_use.html"
17
+ %li= link "Download", "download.html"
18
+ %li= link "How to use", "how_to_use.html"
19
19
  %li= link "Development", "http://rubyforge.org/projects/staticmatic"
20
20
  %li= link "Community", "http://groups.google.co.uk/group/staticmatic"
21
- %li= link "FAQ", "/faq.html"
21
+ %li= link "FAQ", "faq.html"
22
22
 
23
23
  #content_wrapper
24
24
  #side
25
- #news
26
- .heading News
27
- .title 0.8.4 Released!
28
- %p
29
- You can now enjoy:
30
- %ul
31
- %li Multiple directories
32
- %li Partials
33
- = link "And More!", "/releases/0_8_4.html"
34
-
25
+ #news= partial("news")
35
26
 
36
27
  #content
37
28
  = yield
@@ -1,4 +1,3 @@
1
-
2
1
  %h1 Static websites, the modern way
3
2
 
4
3
  %p
@@ -0,0 +1,31 @@
1
+ %h1 StaticMatic 0.8.8
2
+
3
+ %h2 Helpers
4
+
5
+ %p Thanks to Craig Webster, we now have Rails-style helpers.
6
+
7
+ src/application_helper.rb:
8
+
9
+ :preserve
10
+ <pre class="code">
11
+ module ApplicationHelper
12
+ def greet(name)
13
+ "Hello, #{name}!"
14
+ end
15
+ end
16
+ </pre>
17
+
18
+ %h2 Configuration
19
+
20
+ %p We also now have configuration. It's only basic right now but expect this to expand.
21
+
22
+ src/configuration.rb:
23
+
24
+ .code
25
+ configuration.preview_server_port = 3000
26
+ configuration.use_relative_paths_for_links = false
27
+ configuration.use_relative_paths_for_images = false
28
+ configuration.use_relative_paths_for_stylesheets = false
29
+ configuration.use_relative_paths_for_javascripts = false
30
+
31
+ %p The configuration file is also loaded before StaticMatic starts so it can be used to monkey-patch to demonstrate new features.
@@ -0,0 +1,8 @@
1
+ .heading News
2
+ .title 0.8.8 Released!
3
+ %p
4
+ Complete with:
5
+ %ul
6
+ %li Helpers
7
+ %li Configuration
8
+ = link "And More!", "/releases/0_8_8.html"
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.8.6
7
- date: 2007-09-24 00:00:00 +01:00
6
+ version: 0.8.8
7
+ date: 2007-09-25 00:00:00 +01:00
8
8
  summary: Manage static sites using Haml & Sass
9
9
  require_paths:
10
10
  - lib
@@ -50,6 +50,7 @@ files:
50
50
  - lib
51
51
  - lib/staticmatic
52
52
  - lib/staticmatic/base.rb
53
+ - lib/staticmatic/configuration.rb
53
54
  - lib/staticmatic/error.rb
54
55
  - lib/staticmatic/helpers.rb
55
56
  - lib/staticmatic/server.rb
@@ -65,7 +66,7 @@ files:
65
66
  - test/helpers_test.rb
66
67
  - test/sandbox
67
68
  - test/sandbox/test_site
68
- - test/sandbox/test_site/config.rb
69
+ - test/sandbox/test_site/configuration.rb
69
70
  - test/sandbox/test_site/site
70
71
  - test/sandbox/test_site/site/images
71
72
  - test/sandbox/test_site/site/index.html
@@ -73,6 +74,8 @@ files:
73
74
  - test/sandbox/test_site/site/stylesheets
74
75
  - test/sandbox/test_site/site/stylesheets/application.css
75
76
  - test/sandbox/test_site/src
77
+ - test/sandbox/test_site/src/helpers
78
+ - test/sandbox/test_site/src/helpers/application_helper.rb
76
79
  - test/sandbox/test_site/src/layouts
77
80
  - test/sandbox/test_site/src/layouts/alternate_layout.haml
78
81
  - test/sandbox/test_site/src/layouts/application.haml
@@ -104,6 +107,8 @@ files:
104
107
  - website/site/stylesheets
105
108
  - website/site/stylesheets/application.css
106
109
  - website/src
110
+ - website/src/helpers
111
+ - website/src/helpers/application_helper.rb
107
112
  - website/src/layouts
108
113
  - website/src/layouts/application.haml
109
114
  - website/src/layouts/test.haml
@@ -114,7 +119,9 @@ files:
114
119
  - website/src/pages/index.haml
115
120
  - website/src/pages/releases
116
121
  - website/src/pages/releases/0_8_4.haml
122
+ - website/src/pages/releases/0_8_8.haml
117
123
  - website/src/partials
124
+ - website/src/partials/news.haml
118
125
  - website/src/stylesheets
119
126
  test_files:
120
127
  - test/base_test.rb