panorama 0.0.1

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.
@@ -0,0 +1,21 @@
1
+ README
2
+ ======
3
+
4
+
5
+ Summary
6
+ -------
7
+ Panorama, the template system.
8
+
9
+
10
+ Installation
11
+ ------------
12
+ `gem install panorama`
13
+
14
+
15
+ Usage
16
+ -----
17
+
18
+
19
+ Description
20
+ -----------
21
+ Panorama provides a templating system, which allows the use of partials and content insertion from other places of a template.
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../rake/lib', __FILE__))
2
+ Dir.glob(File.expand_path('../rake/tasks/**/*.{rake,task,rb}', __FILE__)) do |task_file|
3
+ begin
4
+ import task_file
5
+ rescue LoadError => e
6
+ warn "Failed to load task file #{task_file}"
7
+ warn " #{e.class} #{e.message}"
8
+ warn " #{e.backtrace.first}"
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+
4
+
5
+ require 'panorama/version'
6
+ require 'panorama/view'
7
+
8
+
9
+ # Panorama
10
+ # Panorama provides a templating system, which allows the use of partials and content
11
+ # insertion from other places of a template.
12
+ module Panorama
13
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'rubygems/version' # newer rubygems use this
5
+ rescue LoadError
6
+ require 'gem/version' # older rubygems use this
7
+ end
8
+
9
+ module Panorama
10
+ Version = Gem::Version.new("0.0.1")
11
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+
3
+
4
+
5
+ require 'pathname'
6
+ require 'haml'
7
+ require 'panorama/view/content_for'
8
+
9
+
10
+
11
+ module Panorama
12
+ class View
13
+ Renderer = {
14
+ '.haml' => proc { |locals|
15
+ Haml::Engine.new(@template).render(self, locals) { |*args|
16
+ yielded(*args)
17
+ }
18
+ }
19
+ }
20
+
21
+ def self.file(path, options=nil)
22
+ options = options ? options.merge(:path => path) : {:path => path}
23
+
24
+ new(File.read(path), options)
25
+ end
26
+
27
+
28
+
29
+ attr_reader :template
30
+ attr_reader :path
31
+ attr_reader :dir
32
+
33
+ def initialize(template, options=nil)
34
+ if options then
35
+ @path = options[:path] && Pathname(options[:path])
36
+ @dir = @path && @path.expand_path.dirname
37
+ @renderer = @path ? Renderer[@path.extname] : Renderer['.haml']
38
+ else
39
+ @path = nil
40
+ @dir = nil
41
+ @renderer = Renderer['.haml']
42
+ end
43
+ @template = template
44
+
45
+ raise ArgumentError, "No renderer found for #{@path.extname.inspect}" unless @renderer
46
+ end
47
+
48
+ def find_partial(path)
49
+ full_path = catch :path do
50
+ (@dir+"_#{path}.html.haml").tap { |try| throw(:path, try) if try.readable? }
51
+ (@dir+"#{path}.html.haml").tap { |try| throw(:path, try) if try.readable? }
52
+ end
53
+ raise ArgumentError, "Could not find partial #{path.inspect}" unless full_path
54
+
55
+ View.file(full_path)
56
+ end
57
+
58
+ def partial(path, locals={})
59
+ find_partial(path).render_internal(@content_for, locals)
60
+ end
61
+
62
+ def content_for(name, *args, &block)
63
+ @content_for.add_content(name, capture_haml(&block))
64
+ ""
65
+ end
66
+
67
+ def yielded(name, *args)
68
+ @content_for.add_yield(name, *args)
69
+ end
70
+
71
+ def render(locals={})
72
+ render_internal(ContentFor.new, locals).gsub(@content_for.regex) { |key|
73
+ @content_for[key]
74
+ }
75
+ end
76
+ alias to_s render
77
+
78
+ def render_internal(content_for, locals)
79
+ @content_for = content_for
80
+ instance_exec(locals, &@renderer)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+
4
+
5
+ module Panorama
6
+ class View
7
+ class ContentFor
8
+ MaxRand = 1<<128
9
+
10
+ attr_reader :yieldings, :contents, :prefix, :regex
11
+
12
+ def initialize
13
+ @yieldings = {}
14
+ @contents = {}
15
+ @prefix = sprintf "__yielded_%32X_", rand(MaxRand)
16
+ @regex = /#{@prefix}\w+/
17
+ end
18
+
19
+ def [](key)
20
+ @contents[key] || ""
21
+ end
22
+
23
+ def key(name)
24
+ "#{@prefix}#{name}"
25
+ end
26
+
27
+ def add_content(name, string)
28
+ (@contents[key(name)] ||= "") << string
29
+ end
30
+
31
+ def add_yield(name, *args)
32
+ key = key(name)
33
+ raise ArgumentError, "Already yielded #{name.inspect}" if @yieldings.has_key?(key)
34
+ @yieldings[key] = [name, args]
35
+
36
+ key
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "panorama"
5
+ s.version = "0.0.1"
6
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1")
7
+ s.authors = "Stefan Rusterholz"
8
+ s.homepage = "http://github.com/apeiros/panorama"
9
+ s.description = <<-DESCRIPTION.gsub(/^ /, '').chomp
10
+ Panorama provides a templating system, which allows the use of partials and content insertion from other places of a template.
11
+ DESCRIPTION
12
+ s.summary = <<-SUMMARY.gsub(/^ /, '').chomp
13
+ Panorama, the template system.
14
+ SUMMARY
15
+ s.email = "stefan.rusterholz@gmail.com"
16
+ s.files =
17
+ Dir['bin/**/*'] +
18
+ Dir['lib/**/*'] +
19
+ Dir['rake/**/*'] +
20
+ Dir['test/**/*'] +
21
+ %w[
22
+ panorama.gemspec
23
+ Rakefile
24
+ README.markdown
25
+ ]
26
+ s.require_paths = %w[lib]
27
+ if File.directory?('bin') then
28
+ executables = Dir.chdir('bin') { Dir.glob('**/*').select { |f| File.executable?(f) } }
29
+ s.executables = executables unless executables.empty?
30
+ end
31
+ s.rubygems_version = "1.3.1"
32
+ s.specification_version = 3
33
+ end
@@ -0,0 +1,18 @@
1
+ namespace :gem do
2
+ desc "Builds the gem and puts it into the 'build' directory."
3
+ task :build do
4
+ sh 'gem build *.gemspec'
5
+ mkdir 'build' unless File.directory?('build')
6
+ sh 'mv *.gem build'
7
+ end
8
+
9
+ desc "Clobbers, then rebuilds and installs the gem"
10
+ task :install => [:clobber, :build] do
11
+ sh 'gem install -l ./build/*.gem'
12
+ end
13
+
14
+ desc "Removes all built gems from the 'build' directory."
15
+ task :clobber do
16
+ sh 'rm build/*.gem'
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ = content_for :stylesheets do
2
+ %style
3
+ :sass
4
+ #bar
5
+ p
6
+ font-family: helvetica
7
+
8
+ %p This is bar
@@ -0,0 +1,15 @@
1
+ !!!
2
+ %html
3
+ %head
4
+ %title Foo
5
+
6
+ / Stylesheets here
7
+ = yield :stylesheets
8
+
9
+ / Javascripts here
10
+ = yield :javascripts
11
+ %body
12
+ = content_for :javascripts do
13
+ :javascript
14
+ function mystuff() {}
15
+ = partial 'inner'
@@ -0,0 +1,24 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
+ <html>
3
+ <head>
4
+ <title>Foo</title>
5
+ <!-- Stylesheets here -->
6
+ <style>
7
+ #bar p {
8
+ font-family: helvetica; }
9
+ </style>
10
+
11
+ <!-- Javascripts here -->
12
+ <script type='text/javascript'>
13
+ //<![CDATA[
14
+ function mystuff() {}
15
+ //]]>
16
+ </script>
17
+
18
+ </head>
19
+ <body>
20
+
21
+
22
+ <p>This is bar</p>
23
+ </body>
24
+ </html>
@@ -0,0 +1,26 @@
1
+ ## HOW TO RUN THE TESTS
2
+ # ruby -r ./test/helper.rb test/units/panorama_view_test.rb
3
+
4
+
5
+
6
+ ## Add the lib dir to $LOAD_PATH
7
+ lib_dir = File.expand_path('../../lib', __FILE__)
8
+ if File.directory?(lib_dir) then
9
+ $LOAD_PATH.unshift lib_dir
10
+ else
11
+ puts "WARNING",
12
+ "Could not find the lib dir.",
13
+ "Tests may not run for the expected version of the library, or even not at all."
14
+ end
15
+
16
+
17
+
18
+ ## requires
19
+ require 'pathname'
20
+ require 'test/unit'
21
+ require 'panorama'
22
+
23
+
24
+
25
+ ## prepare global data
26
+ $data = Pathname('../data').expand_path(__FILE__)
@@ -0,0 +1,10 @@
1
+ require_relative '../helper.rb'
2
+
3
+ class TestView < Test::Unit::TestCase
4
+ def test_all
5
+ assert_equal(
6
+ File.read($data+'test1/result.html'),
7
+ p(Panorama::View.file($data+'test1/outer.html.haml').render)
8
+ )
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: panorama
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stefan Rusterholz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Panorama provides a templating system, which allows the use of partials
15
+ and content insertion from other places of a template.
16
+ email: stefan.rusterholz@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/panorama/version.rb
22
+ - lib/panorama/view/content_for.rb
23
+ - lib/panorama/view.rb
24
+ - lib/panorama.rb
25
+ - rake/tasks/gem.rake
26
+ - test/data/test1/_inner.html.haml
27
+ - test/data/test1/outer.html.haml
28
+ - test/data/test1/result.html
29
+ - test/helper.rb
30
+ - test/units/panorama_view_test.rb
31
+ - panorama.gemspec
32
+ - Rakefile
33
+ - README.markdown
34
+ homepage: http://github.com/apeiros/panorama
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>'
50
+ - !ruby/object:Gem::Version
51
+ version: 1.3.1
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.15
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Panorama, the template system.
58
+ test_files: []