tiny-template 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,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tiny-template.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 James Brooks
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Tiny::Template
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tiny-template'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tiny-template
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ require 'tiny-template/version'
2
+ require 'tiny-template/base'
3
+
4
+ require 'tiny-template/node'
5
+ require 'tiny-template/node/tag'
6
+ require 'tiny-template/node/text'
7
+ require 'tiny-template/node/mismatched_tag'
8
+
9
+ require 'tiny-template/renderer/string'
10
+
11
+
12
+ module TinyTemplate
13
+ extend Base
14
+ end
@@ -0,0 +1,31 @@
1
+ module TinyTemplate::Base
2
+ def parse(str)
3
+ TinyTemplate::Node::Tag.new.tap do |node|
4
+ str.split(/(?<!\\)<(.+?)>/).zip([:text, :tag].cycle).each do |content, type|
5
+ content = content.strip
6
+ next if content.empty?
7
+
8
+ case type
9
+ when :text
10
+ node << TinyTemplate::Node::Text.new(content)
11
+
12
+ when :tag
13
+ if /\/(?<name>.*)/ =~ content
14
+ # Simple recovery; close tags until we see the close tag we expect
15
+ until name == node.name || node.parent.nil?
16
+ node = node.parent
17
+ end
18
+
19
+ unless name == node.name
20
+ raise TinyTemplate::Node::MismatchedTag.new
21
+ end
22
+
23
+ node = node.parent
24
+ else
25
+ node << (node = TinyTemplate::Node::Tag.new(content))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ class TinyTemplate::Node
2
+ attr_accessor :parent
3
+
4
+
5
+ def to_s
6
+ TinyTemplate::Renderer::String.render(self)
7
+ end
8
+ end
@@ -0,0 +1,2 @@
1
+ class TinyTemplate::Node::MismatchedTag < StandardError
2
+ end
@@ -0,0 +1,14 @@
1
+ class TinyTemplate::Node::Tag < TinyTemplate::Node
2
+ attr_accessor :name, :children
3
+
4
+
5
+ def initialize(name=nil)
6
+ self.name = name
7
+ self.children = []
8
+ end
9
+
10
+ def <<(node)
11
+ children << node
12
+ node.parent = self
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ class TinyTemplate::Node::Text < TinyTemplate::Node
2
+ attr_accessor :value
3
+
4
+
5
+ def initialize(value='')
6
+ self.value = value
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module TinyTemplate::Renderer
2
+ class String
3
+ def self.render(node)
4
+ ''.tap do |str|
5
+ case node
6
+ when TinyTemplate::Node::Tag
7
+ str << "<#{node.name}>" if node.name
8
+ node.children.each { |child| str << render(child) }
9
+ str << "</#{node.name}>" if node.name
10
+
11
+ when TinyTemplate::Node::Text
12
+ str << node.value
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module TinyTemplate
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe "TinyTemplate::Base#parse acceptance" do
4
+ it 'should accept an empty string' do
5
+ parse('')
6
+ end
7
+
8
+ it 'should accept a empty tag' do
9
+ parse('<a></a>')
10
+ end
11
+
12
+ it 'should accept text without any tags' do
13
+ parse('text')
14
+ end
15
+
16
+ it 'should accept a tag containing text' do
17
+ parse('<a>text</a>')
18
+ end
19
+
20
+ it 'should accept leading spaces' do
21
+ parse(' <a>text</a>')
22
+ end
23
+
24
+ it 'should accept trailing spaces' do
25
+ parse('<a>text</a> ')
26
+ end
27
+
28
+ it 'should accept spaces within tags' do
29
+ parse('<a> text </a>')
30
+ end
31
+
32
+ it 'should accept correctly nested tags' do
33
+ parse('<a><b></b></a>')
34
+ end
35
+
36
+ it 'should accept incorrectly closed tags if they can be recovered' do
37
+ parse('<a>foo')
38
+ end
39
+
40
+ it 'should accept incorrectly nested tags if they can be recovered' do
41
+ parse('<a><b></a>')
42
+ end
43
+
44
+ it 'should not accept incorrectly closed tags' do
45
+ expect {
46
+ parse('<a></b>')
47
+ }.to raise_error(TinyTemplate::Node::MismatchedTag)
48
+ end
49
+
50
+ it 'should not accept incorrectly nested tags if they cannot be recovered' do
51
+ expect {
52
+ parse('<a><b></b></b>')
53
+ }.to raise_error(TinyTemplate::Node::MismatchedTag)
54
+ end
55
+
56
+ it 'should return a root tag node for valid strings' do
57
+ [
58
+ 'text',
59
+ ' text',
60
+ 'text ',
61
+ '<tag></tag>',
62
+ ' <tag></tag>',
63
+ '<tag></tag> ',
64
+ '<a>text</a>',
65
+ '<a> text</a>',
66
+ '<a>text </a>',
67
+ '<a><b>text</b></a>',
68
+ '<a><b>text',
69
+ '<a><b>text</b>',
70
+ '<a><b>text</a>'
71
+ ].each do |template|
72
+ parse(template).should be_a_kind_of(TinyTemplate::Node::Tag)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe "TinyTemplate::Base#parse structure" do
4
+ describe "using simple text" do
5
+ let(:template) { parse('text') }
6
+ let(:node) { template.children.first }
7
+
8
+
9
+ it 'should contain one child' do
10
+ template.children.size.should be(1)
11
+ end
12
+
13
+ it 'should be a text node' do
14
+ node.should be_a_kind_of(TinyTemplate::Node::Text)
15
+ end
16
+
17
+ it 'should contain the value "text' do
18
+ node.value.should eq('text')
19
+ end
20
+ end
21
+
22
+ describe "using a simple tag" do
23
+ let(:template) { parse('<tag></tag>') }
24
+ let(:node) { template.children.first }
25
+
26
+
27
+ it 'should contain one child' do
28
+ template.children.size.should be(1)
29
+ end
30
+
31
+ it 'should be a tag node' do
32
+ node.should be_a_kind_of(TinyTemplate::Node::Tag)
33
+ end
34
+
35
+ it 'should have a tag name of "tag"' do
36
+ node.name.should eq('tag')
37
+ end
38
+
39
+ it 'should not have any inner children' do
40
+ node.children.should be_empty
41
+ end
42
+ end
43
+
44
+ describe "multiple parallel tags" do
45
+ let(:template) { parse('<a></a><a></a><a></a>') }
46
+
47
+
48
+ it 'should contain three childen' do
49
+ template.children.size.should be(3)
50
+ end
51
+
52
+ it 'should have all children as tags' do
53
+ template.children.each do |child|
54
+ child.should be_a_kind_of(TinyTemplate::Node::Tag)
55
+ end
56
+ end
57
+
58
+ it 'should have all children as "a" tags' do
59
+ template.children.each do |child|
60
+ child.name.should eq('a')
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,16 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'tiny-template'
4
+
5
+ Dir[File.join('spec', 'support', '**', '*.rb')].each { |f| load f }
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+
11
+ # Run specs in random order to surface order dependencies. If you find an
12
+ # order dependency and want to debug it, you can fix the order by providing
13
+ # the seed, which is printed after each run.
14
+ # --seed 1234
15
+ config.order = 'random'
16
+ end
@@ -0,0 +1,3 @@
1
+ def parse(str)
2
+ TinyTemplate.parse(str)
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tiny-template/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "tiny-template"
8
+ gem.version = TinyTemplate::VERSION
9
+ gem.authors = ["James Brooks"]
10
+ gem.email = ["james@gooddogdesign.com"]
11
+ gem.description = 'Simple tag based templating engine for ruby'
12
+ gem.summary = 'Simple tag based templating engine for ruby'
13
+ gem.homepage = 'https://github.com/JamesBrooks/tiny-template'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny-template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James Brooks
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple tag based templating engine for ruby
15
+ email:
16
+ - james@gooddogdesign.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/tiny-template.rb
28
+ - lib/tiny-template/base.rb
29
+ - lib/tiny-template/node.rb
30
+ - lib/tiny-template/node/mismatched_tag.rb
31
+ - lib/tiny-template/node/tag.rb
32
+ - lib/tiny-template/node/text.rb
33
+ - lib/tiny-template/renderer/string.rb
34
+ - lib/tiny-template/version.rb
35
+ - spec/parse_acceptance_spec.rb
36
+ - spec/parse_structure_spec.rb
37
+ - spec/spec_helper.rb
38
+ - spec/support/shorthand_methods.rb
39
+ - tiny-template.gemspec
40
+ homepage: https://github.com/JamesBrooks/tiny-template
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.24
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Simple tag based templating engine for ruby
64
+ test_files:
65
+ - spec/parse_acceptance_spec.rb
66
+ - spec/parse_structure_spec.rb
67
+ - spec/spec_helper.rb
68
+ - spec/support/shorthand_methods.rb