templette 0.3.2 → 0.4.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.
- data/lib/tasks/templette_tasks.rb +23 -0
- data/lib/templette.rb +2 -2
- data/lib/templette/engineer.rb +40 -0
- data/lib/templette/engines/erb.rb +11 -0
- data/lib/templette/engines/haml.rb +13 -0
- data/lib/templette/errors.rb +3 -0
- data/lib/templette/method_collector.rb +1 -1
- data/lib/templette/page.rb +88 -88
- data/lib/templette/template.rb +25 -6
- metadata +6 -3
@@ -7,6 +7,29 @@ task(:build) do
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
desc "Build and view the site locally using WEBrick"
|
11
|
+
task(:preview) do
|
12
|
+
gen = Templette::Generator.new('preview')
|
13
|
+
gen.run
|
14
|
+
|
15
|
+
require 'webrick'
|
16
|
+
|
17
|
+
server = WEBrick::HTTPServer.new(
|
18
|
+
:BindAddress => "localhost",
|
19
|
+
:Port => 4444,
|
20
|
+
:DocumentRoot => 'preview/'
|
21
|
+
)
|
22
|
+
|
23
|
+
%w(INT).each do |signal|
|
24
|
+
trap(signal) {
|
25
|
+
server.shutdown
|
26
|
+
gen.clean
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
server.start
|
31
|
+
end
|
32
|
+
|
10
33
|
desc "Remove all generated files"
|
11
34
|
task :clean do
|
12
35
|
Templette::Generator.new.clean
|
data/lib/templette.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
%w{erb yaml fileutils}.each {|lib| require lib}
|
2
2
|
|
3
|
-
%w{data_accessors errors generator method_collector page template}.each do |file|
|
4
|
-
require File.dirname(__FILE__) +"/templette/#{file}"
|
3
|
+
%w{data_accessors errors generator method_collector page template engineer}.each do |file|
|
4
|
+
require File.dirname(__FILE__) + "/templette/#{file}"
|
5
5
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Templette
|
2
|
+
|
3
|
+
# The engineer is simply in charge of the engines.
|
4
|
+
#
|
5
|
+
# Additional templating languages can be supported by adding a class in the Templette::Engines
|
6
|
+
# module which implements render(html, the_binding) in the templates/engines/ folder. Name
|
7
|
+
# the class the same as the 'type' for the template.
|
8
|
+
|
9
|
+
module Engineer
|
10
|
+
|
11
|
+
ENGINES_DIR = File.dirname(__FILE__) + "/engines/"
|
12
|
+
|
13
|
+
@@engines = {}
|
14
|
+
|
15
|
+
# Takes a string type and attempts to load a rendering engine of the same name.
|
16
|
+
# If the Engine is found, an instance of the class will be returned.
|
17
|
+
# A failure to find a matching engine file will result in a RenderError.
|
18
|
+
|
19
|
+
def self.engine_for(type)
|
20
|
+
load_engine(type)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def self.load_engine(type)
|
26
|
+
unless @@engines[type]
|
27
|
+
unless File.exists?(ENGINES_DIR + type + '.rb')
|
28
|
+
raise RenderError.new("Rendering engine '#{type}' is not supported!")
|
29
|
+
end
|
30
|
+
begin
|
31
|
+
require ENGINES_DIR + type
|
32
|
+
@@engines[type] = Templette::Engines.const_get(type.capitalize)
|
33
|
+
rescue
|
34
|
+
raise RenderError.new("Rendering engine '#{type}' failed to load!")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
@@engines[type].new
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/templette/errors.rb
CHANGED
data/lib/templette/page.rb
CHANGED
@@ -1,88 +1,88 @@
|
|
1
|
-
module Templette
|
2
|
-
|
3
|
-
# The Page is the core object of a Templette project. Pages won't get generate
|
4
|
-
# unless there are available .yml files to build out the necessary info.
|
5
|
-
#
|
6
|
-
# A general Page yaml structure example:
|
7
|
-
#
|
8
|
-
# template_name: main
|
9
|
-
# sections:
|
10
|
-
# title: Page Title
|
11
|
-
# nav:
|
12
|
-
# active-class: foo
|
13
|
-
# title: Foo!
|
14
|
-
#
|
15
|
-
# The <tt>template_name</tt> will be used to load a template.
|
16
|
-
#
|
17
|
-
# Everything in <tt>sections</tt> will be made available as methods in the template when
|
18
|
-
# it's evaluated by ERB. Yaml hash items nested within others will be loaded into nested
|
19
|
-
# objects. To call the nav title, the template should call <tt>nav.title</tt>.
|
20
|
-
|
21
|
-
class Page
|
22
|
-
|
23
|
-
include Templette::DataAccessors
|
24
|
-
attr_reader :name, :template
|
25
|
-
|
26
|
-
class <<self
|
27
|
-
def pages_dir
|
28
|
-
@@pages_dir ||= 'pages'
|
29
|
-
end
|
30
|
-
|
31
|
-
def pages_dir=(path)
|
32
|
-
@@pages_dir = path
|
33
|
-
end
|
34
|
-
|
35
|
-
# Grabs all of the yaml files found in /pages, and loads them as
|
36
|
-
# Page objects.
|
37
|
-
def find_all
|
38
|
-
Dir["#{pages_dir}/**/*.yml"].map {|f| new f }
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def initialize(page_config)
|
43
|
-
raise PageError.new(self, "missing page #{page_config}") unless File.exists?(page_config)
|
44
|
-
|
45
|
-
data = YAML::load_file(page_config)
|
46
|
-
@name = page_config.dup
|
47
|
-
@name.slice!(self.class.pages_dir + '/')
|
48
|
-
@name.chomp!('.yml')
|
49
|
-
raise PageError.new(self, "missing required section \"template_name\" for page config #{page_config}") unless data['template_name']
|
50
|
-
@template = Template.new(data['template_name'])
|
51
|
-
|
52
|
-
raise PageError.new(self, "missing sections in yml for page config #{page_config}") unless data['sections']
|
53
|
-
generate_accessors(data['sections'])
|
54
|
-
include_helpers(template.helpers)
|
55
|
-
end
|
56
|
-
|
57
|
-
def generate(out_dir)
|
58
|
-
generate_subdirectory(out_dir)
|
59
|
-
File.open(output_file_name(out_dir), 'w') do |f|
|
60
|
-
f <<
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
# A requriement of the Templette::DataAccessors interface. Returns self.
|
65
|
-
def page; self end
|
66
|
-
|
67
|
-
private
|
68
|
-
|
69
|
-
def output_file_name(out_dir)
|
70
|
-
"#{out_dir}/#{@name}.html"
|
71
|
-
end
|
72
|
-
|
73
|
-
def generate_subdirectory(out_dir)
|
74
|
-
FileUtils.mkdir_p(File.expand_path("#{out_dir}/" + name.chomp(File.basename(name)))) if name.index('/')
|
75
|
-
end
|
76
|
-
|
77
|
-
class Section # :nodoc:
|
78
|
-
include Templette::DataAccessors
|
79
|
-
attr_reader :page
|
80
|
-
|
81
|
-
def initialize(page, hash={})
|
82
|
-
@page = page
|
83
|
-
generate_accessors(hash)
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
1
|
+
module Templette
|
2
|
+
|
3
|
+
# The Page is the core object of a Templette project. Pages won't get generate
|
4
|
+
# unless there are available .yml files to build out the necessary info.
|
5
|
+
#
|
6
|
+
# A general Page yaml structure example:
|
7
|
+
#
|
8
|
+
# template_name: main
|
9
|
+
# sections:
|
10
|
+
# title: Page Title
|
11
|
+
# nav:
|
12
|
+
# active-class: foo
|
13
|
+
# title: Foo!
|
14
|
+
#
|
15
|
+
# The <tt>template_name</tt> will be used to load a template.
|
16
|
+
#
|
17
|
+
# Everything in <tt>sections</tt> will be made available as methods in the template when
|
18
|
+
# it's evaluated by ERB. Yaml hash items nested within others will be loaded into nested
|
19
|
+
# objects. To call the nav title, the template should call <tt>nav.title</tt>.
|
20
|
+
|
21
|
+
class Page
|
22
|
+
|
23
|
+
include Templette::DataAccessors
|
24
|
+
attr_reader :name, :template
|
25
|
+
|
26
|
+
class <<self
|
27
|
+
def pages_dir
|
28
|
+
@@pages_dir ||= 'pages'
|
29
|
+
end
|
30
|
+
|
31
|
+
def pages_dir=(path)
|
32
|
+
@@pages_dir = path
|
33
|
+
end
|
34
|
+
|
35
|
+
# Grabs all of the yaml files found in /pages, and loads them as
|
36
|
+
# Page objects.
|
37
|
+
def find_all
|
38
|
+
Dir["#{pages_dir}/**/*.yml"].map {|f| new f }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize(page_config)
|
43
|
+
raise PageError.new(self, "missing page #{page_config}") unless File.exists?(page_config)
|
44
|
+
|
45
|
+
data = YAML::load_file(page_config)
|
46
|
+
@name = page_config.dup
|
47
|
+
@name.slice!(self.class.pages_dir + '/')
|
48
|
+
@name.chomp!('.yml')
|
49
|
+
raise PageError.new(self, "missing required section \"template_name\" for page config #{page_config}") unless data['template_name']
|
50
|
+
@template = Template.new(data['template_name'])
|
51
|
+
|
52
|
+
raise PageError.new(self, "missing sections in yml for page config #{page_config}") unless data['sections']
|
53
|
+
generate_accessors(data['sections'])
|
54
|
+
include_helpers(template.helpers)
|
55
|
+
end
|
56
|
+
|
57
|
+
def generate(out_dir)
|
58
|
+
generate_subdirectory(out_dir)
|
59
|
+
File.open(output_file_name(out_dir), 'w') do |f|
|
60
|
+
f << @template.render(binding)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# A requriement of the Templette::DataAccessors interface. Returns self.
|
65
|
+
def page; self end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def output_file_name(out_dir)
|
70
|
+
"#{out_dir}/#{@name}.html"
|
71
|
+
end
|
72
|
+
|
73
|
+
def generate_subdirectory(out_dir)
|
74
|
+
FileUtils.mkdir_p(File.expand_path("#{out_dir}/" + name.chomp(File.basename(name)))) if name.index('/')
|
75
|
+
end
|
76
|
+
|
77
|
+
class Section # :nodoc:
|
78
|
+
include Templette::DataAccessors
|
79
|
+
attr_reader :page
|
80
|
+
|
81
|
+
def initialize(page, hash={})
|
82
|
+
@page = page
|
83
|
+
generate_accessors(hash)
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/templette/template.rb
CHANGED
@@ -2,9 +2,15 @@ module Templette
|
|
2
2
|
# The Template acts as a layout for pages. It contains an html layout for the
|
3
3
|
# page and contains method calls which are answered by helper methods or the
|
4
4
|
# loaded yaml of a page.
|
5
|
+
#
|
6
|
+
# Templates now support ERB or Haml for rendering.
|
7
|
+
# * template.html.haml
|
8
|
+
# * template.html.erb
|
9
|
+
# * template.html -> defaults to ERB
|
5
10
|
|
6
11
|
class Template
|
7
12
|
TEMPLATE_DIR = 'templates' unless defined?(TEMPLATE_DIR)
|
13
|
+
|
8
14
|
attr_accessor :name
|
9
15
|
|
10
16
|
# The name parameter refers to the actual filename of the template. To load
|
@@ -12,11 +18,6 @@ module Templette
|
|
12
18
|
def initialize(name)
|
13
19
|
@name = name
|
14
20
|
end
|
15
|
-
|
16
|
-
def to_html
|
17
|
-
raise TemplateError.new(self, "Template rendering failed. File not found.") unless File.exists?(path)
|
18
|
-
File.read(path)
|
19
|
-
end
|
20
21
|
|
21
22
|
# Generates the yaml necessary to render empty page yaml files.
|
22
23
|
def to_yaml
|
@@ -27,10 +28,28 @@ module Templette
|
|
27
28
|
def helpers
|
28
29
|
["default_helper","#{name}_helper"]
|
29
30
|
end
|
31
|
+
|
32
|
+
# Generates the final HTML.
|
33
|
+
def render(the_binding)
|
34
|
+
raise TemplateError.new(self, "Template rendering failed. File not found.") unless File.exists?(path)
|
35
|
+
Engineer.engine_for(type).render(to_html, the_binding)
|
36
|
+
rescue RenderError => e
|
37
|
+
raise TemplateError.new(self, e.message)
|
38
|
+
end
|
30
39
|
|
31
40
|
private
|
41
|
+
|
32
42
|
def path
|
33
|
-
"#{TEMPLATE_DIR}/#{@name}.html"
|
43
|
+
Dir["#{TEMPLATE_DIR}/#{@name}.html*"].first || ''
|
34
44
|
end
|
45
|
+
|
46
|
+
def type
|
47
|
+
path.match(/html\.?(\w+)?/)[1] || 'erb'
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_html
|
51
|
+
File.read(path)
|
52
|
+
end
|
53
|
+
|
35
54
|
end
|
36
55
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: templette
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Dunphy and Steve Holder
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-02-04 00:00:00 -08:00
|
13
13
|
default_executable: templette
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -25,6 +25,9 @@ files:
|
|
25
25
|
- lib/file_generator.rb
|
26
26
|
- lib/tasks/templette_tasks.rb
|
27
27
|
- lib/templette/data_accessors.rb
|
28
|
+
- lib/templette/engineer.rb
|
29
|
+
- lib/templette/engines/erb.rb
|
30
|
+
- lib/templette/engines/haml.rb
|
28
31
|
- lib/templette/errors.rb
|
29
32
|
- lib/templette/generator.rb
|
30
33
|
- lib/templette/method_collector.rb
|
@@ -59,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
62
|
requirements: []
|
60
63
|
|
61
64
|
rubyforge_project: templette
|
62
|
-
rubygems_version: 1.
|
65
|
+
rubygems_version: 1.3.1
|
63
66
|
signing_key:
|
64
67
|
specification_version: 2
|
65
68
|
summary: HTML site generation through templates
|