massimo 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/massimo/config.rb +9 -0
- data/lib/massimo/helpers.rb +2 -0
- data/lib/massimo/page.rb +11 -6
- data/lib/massimo/resource.rb +15 -2
- data/lib/massimo/site.rb +13 -0
- data/lib/massimo/watcher.rb +3 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.6
|
data/lib/massimo/config.rb
CHANGED
@@ -14,6 +14,8 @@ module Massimo
|
|
14
14
|
:stylesheets_url => '/stylesheets'
|
15
15
|
}.freeze
|
16
16
|
|
17
|
+
# Creates a new configuration. Takes either a hash of options
|
18
|
+
# or a file path to a .yaml file.
|
17
19
|
def initialize(options = nil)
|
18
20
|
hash = DEFAULT_OPTIONS.dup
|
19
21
|
|
@@ -23,14 +25,18 @@ module Massimo
|
|
23
25
|
super hash
|
24
26
|
end
|
25
27
|
|
28
|
+
# The full, expanded path to the source path.
|
26
29
|
def source_path
|
27
30
|
File.expand_path(super)
|
28
31
|
end
|
29
32
|
|
33
|
+
# The full, expanded path to the output path.
|
30
34
|
def output_path
|
31
35
|
File.expand_path(super)
|
32
36
|
end
|
33
37
|
|
38
|
+
# Get a full, expanded path for the given resource name. This is either set
|
39
|
+
# in the configuration or determined dynamically based on the name.
|
34
40
|
def path_for(resource_name)
|
35
41
|
path_method = "#{resource_name}_path"
|
36
42
|
if resource_path = (respond_to?(path_method) and send(path_method))
|
@@ -40,6 +46,7 @@ module Massimo
|
|
40
46
|
end
|
41
47
|
end
|
42
48
|
|
49
|
+
# Get the configured URL for th given resource name.
|
43
50
|
def url_for(resource_name)
|
44
51
|
url_method = "#{resource_name}_url"
|
45
52
|
resource_url = respond_to?(url_method) && send(url_method)
|
@@ -47,6 +54,8 @@ module Massimo
|
|
47
54
|
File.join(base_url, resource_url)
|
48
55
|
end
|
49
56
|
|
57
|
+
# Get an array of all the file paths found in the given resource name's path,
|
58
|
+
# restricted to the given extension.
|
50
59
|
def files_in(resource_name, extension = '*')
|
51
60
|
Dir.glob(File.join(path_for(resource_name), "**/*.#{extension}"))
|
52
61
|
end
|
data/lib/massimo/helpers.rb
CHANGED
@@ -9,10 +9,12 @@ module Massimo
|
|
9
9
|
include SinatraMore::FormHelpers
|
10
10
|
include SinatraMore::FormatHelpers
|
11
11
|
|
12
|
+
# Returns an instance of the Site
|
12
13
|
def site
|
13
14
|
Massimo.site
|
14
15
|
end
|
15
16
|
|
17
|
+
# Renders a view with the given locals. Kind of like `render :partial` in Rails
|
16
18
|
def render(view_name, locals = {})
|
17
19
|
view = Massimo::View.find(view_name)
|
18
20
|
view && view.render(locals)
|
data/lib/massimo/page.rb
CHANGED
@@ -1,19 +1,24 @@
|
|
1
1
|
require 'active_support/core_ext/hash/keys'
|
2
|
+
require 'active_support/core_ext/string/starts_ends_with'
|
2
3
|
require 'active_support/inflector'
|
3
4
|
require 'tilt'
|
4
5
|
require 'yaml'
|
5
6
|
|
6
|
-
Tilt.register 'html', Tilt::StringTemplate
|
7
|
-
|
8
7
|
module Massimo
|
9
8
|
class Page < Resource
|
10
9
|
def render
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
output = content
|
11
|
+
|
12
|
+
if template_type = Tilt[source_path.basename.to_s]
|
13
|
+
template = template_type.new(nil, @line || 1) { output }
|
14
|
+
meta_data = @meta_data.merge(self.class.resource_name.singularize.to_sym => self)
|
15
|
+
output = template.render(Massimo.site.template_scope, meta_data)
|
16
|
+
end
|
17
|
+
|
14
18
|
if found_layout = Massimo::View.find("layouts/#{layout}")
|
15
19
|
output = found_layout.render(:page => self) { output }
|
16
20
|
end
|
21
|
+
|
17
22
|
output
|
18
23
|
end
|
19
24
|
|
@@ -42,7 +47,7 @@ module Massimo
|
|
42
47
|
def output_path
|
43
48
|
@output_path ||= begin
|
44
49
|
output_path = super.to_s
|
45
|
-
output_path << "index.html" if output_path
|
50
|
+
output_path << "index.html" if output_path.ends_with? '/'
|
46
51
|
Pathname.new output_path
|
47
52
|
end
|
48
53
|
end
|
data/lib/massimo/resource.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support/core_ext/string/starts_ends_with'
|
1
2
|
require 'active_support/inflector'
|
2
3
|
require 'fileutils'
|
3
4
|
require 'pathname'
|
@@ -5,29 +6,35 @@ require 'pathname'
|
|
5
6
|
module Massimo
|
6
7
|
class Resource
|
7
8
|
class << self
|
9
|
+
# The underscored, pluralized name of the resource.
|
8
10
|
def resource_name
|
9
11
|
self.name.underscore.sub(/.*\//, '').pluralize
|
10
12
|
end
|
11
13
|
|
14
|
+
# The path to the resource's directory.
|
12
15
|
def path
|
13
16
|
Massimo.config.path_for resource_name
|
14
17
|
end
|
15
18
|
|
19
|
+
# The base url for the resource.
|
16
20
|
def url
|
17
21
|
Massimo.config.url_for resource_name
|
18
22
|
end
|
19
23
|
|
24
|
+
# Finds a Resource by the given name.
|
20
25
|
def find(name)
|
21
26
|
resource_path = Dir.glob(File.join(path, "#{name}.*")).first
|
22
27
|
resource_path && self.new(resource_path)
|
23
28
|
end
|
24
29
|
|
30
|
+
# Finds all the Resources in the resource's directory.
|
25
31
|
def all
|
26
32
|
files = Massimo.config.files_in resource_name
|
27
|
-
files.reject! { |file| File.basename(file)
|
33
|
+
files.reject! { |file| File.basename(file).starts_with?('_') }
|
28
34
|
files.map { |file| self.new(file) }
|
29
35
|
end
|
30
36
|
|
37
|
+
# Whether or not massimo should process the resource's files.
|
31
38
|
def processable?
|
32
39
|
true
|
33
40
|
end
|
@@ -42,24 +49,30 @@ module Massimo
|
|
42
49
|
|
43
50
|
attr_reader :source_path, :content
|
44
51
|
|
52
|
+
# Creates a new resource for the given source file.
|
53
|
+
# The contents of the file will automatically be read.
|
45
54
|
def initialize(source)
|
46
55
|
@source_path = source.is_a?(Pathname) ? source.expand_path : Pathname.new(source).expand_path
|
47
56
|
read_source
|
48
57
|
end
|
49
58
|
|
59
|
+
# The basename of the source file.
|
50
60
|
def filename
|
51
61
|
@filename ||= source_path.basename.to_s
|
52
62
|
end
|
53
63
|
|
64
|
+
# The extension to output with.
|
54
65
|
def extension
|
55
66
|
@extension ||= source_path.extname
|
56
67
|
end
|
57
68
|
|
69
|
+
# The url to the resource. This is created by swiching the base path
|
70
|
+
# of the source file with the base url.
|
58
71
|
def url
|
59
72
|
@url ||= begin
|
60
73
|
url = source_path.to_s.sub(/^#{Regexp.escape(self.class.path)}/, '')
|
61
74
|
url = url.sub(/\.[^\.]+$/, extension)
|
62
|
-
url = File.join(self.class.url, url) unless url.
|
75
|
+
url = File.join(self.class.url, url) unless url.starts_with? self.class.url
|
63
76
|
url = url.dasherize
|
64
77
|
url
|
65
78
|
end
|
data/lib/massimo/site.rb
CHANGED
@@ -5,6 +5,8 @@ module Massimo
|
|
5
5
|
class Site
|
6
6
|
attr_accessor :config
|
7
7
|
|
8
|
+
# Creates a new Site, passing the given options to a new configuration.
|
9
|
+
# If a block is given, it is evaluated in the scope of the new Site.
|
8
10
|
def initialize(options = nil, &block)
|
9
11
|
@config = Config.new(options)
|
10
12
|
@template_scope_blocks = []
|
@@ -14,10 +16,15 @@ module Massimo
|
|
14
16
|
instance_eval(&block) if block_given?
|
15
17
|
end
|
16
18
|
|
19
|
+
# The resources used in this Site.
|
17
20
|
def resources
|
18
21
|
@resources ||= [ Massimo::Page, Massimo::Javascript, Massimo::Stylesheet, Massimo::View ]
|
19
22
|
end
|
20
23
|
|
24
|
+
# Adds a new, custom resource to the Site. If a Class constant is given,
|
25
|
+
# it is added to directly to the `#resources`. If a Symbol or String is given, a new
|
26
|
+
# Class (inheriting from Massimo::Page) is created using that name
|
27
|
+
# with the given block used as the Class body.
|
21
28
|
def resource(name_or_class, &block)
|
22
29
|
resource = case name_or_class
|
23
30
|
when Class
|
@@ -28,6 +35,8 @@ module Massimo
|
|
28
35
|
resources << resource
|
29
36
|
end
|
30
37
|
|
38
|
+
# The scope used for templating. It includes helpers from Massimo::Helpers along
|
39
|
+
# with any custom helpers.
|
31
40
|
def template_scope
|
32
41
|
@template_scope ||= begin
|
33
42
|
scope = Object.new.extend(Massimo::Helpers, Tilt::CompileSite)
|
@@ -38,11 +47,15 @@ module Massimo
|
|
38
47
|
end
|
39
48
|
end
|
40
49
|
|
50
|
+
# Adds custom helpers to the `#template_scope`. Takes either an array of Modules
|
51
|
+
# that will extend the scope, or a block of method definitions that will be added
|
52
|
+
# to the scope.
|
41
53
|
def helpers(*extensions, &block)
|
42
54
|
@template_scope_blocks << block if block_given?
|
43
55
|
@template_scope_extensions += extensions
|
44
56
|
end
|
45
57
|
|
58
|
+
# Processes all the current resources.
|
46
59
|
def process
|
47
60
|
@template_scope = nil
|
48
61
|
reload_libs
|
data/lib/massimo/watcher.rb
CHANGED
@@ -11,6 +11,7 @@ module Massimo
|
|
11
11
|
@files = []
|
12
12
|
end
|
13
13
|
|
14
|
+
# Runs a loop, processing the whenever files have changed.
|
14
15
|
def run
|
15
16
|
loop do
|
16
17
|
process
|
@@ -18,6 +19,7 @@ module Massimo
|
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
22
|
+
# Processes the Site if any of the files have changed.
|
21
23
|
def process
|
22
24
|
if changed?
|
23
25
|
begin
|
@@ -31,6 +33,7 @@ module Massimo
|
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
36
|
+
# Determine if any of the Site's files have changed.
|
34
37
|
def changed?
|
35
38
|
@files != files
|
36
39
|
end
|