rhb 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82bd730da575512073d30ab8114830273b1971eb
4
+ data.tar.gz: eb74f22ca547d5e83cc0b8eaea4b0c7efe6a2660
5
+ SHA512:
6
+ metadata.gz: 58f5af68acf14e88ae9da5430615f6c7255a8c643fe68e9459ecb1344b1162fa6edc492649681b551c6faf952a18a094209d56a9154ce73c770eeffea3fcb0d6
7
+ data.tar.gz: a71cdb1da24aeda6bd1476e1315901d2927be00b48085a115c58aac0d78f9aaacb12be25f49b476473306c3dd3ba8ebe6152dd7641f8d6a27f035d5da964892b
@@ -0,0 +1,5 @@
1
+ .ruby-version
2
+ .ruby-gemset
3
+ Gemfile.lock
4
+ tmp
5
+ pkg
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - jruby
7
+ - rbx
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,5 @@
1
+ guard :rspec do
2
+ watch('spec/spec_helper.rb') { "spec" }
3
+ watch(%r{^spec/.+_spec\.rb$})
4
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
5
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexey Volodkin
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,45 @@
1
+ # Rhb (Ruby HTML builder)
2
+
3
+ Lightweight html from ruby code builder. Inspired by [Mab](https://github.com/camping/mab).
4
+
5
+ ## Installation
6
+
7
+ Add to Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rhb'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Example:
16
+ ```ruby
17
+ builder = Rhb::Builder.new
18
+
19
+ builder.doctype
20
+ builder.html do
21
+ head do
22
+ title 'Awesome page'
23
+ link rel: 'stylesheet', href: 'style.css'
24
+ end
25
+ body id: :body do
26
+ h1 'My Awesome Page', class: 'awesome', data: { awesomeness: { level: 'high' } }
27
+ end
28
+ end
29
+ end
30
+
31
+ builder.to_html
32
+ ```
33
+
34
+ Result:
35
+ ```html
36
+ <!DOCTYPE html><html><head><title>Awesome page</title><link rel="stylesheet" href="style.css"></head><body id="body"><h1 class="awesome" data-awesomeness-level="high">My Awesome Page</h1></body></html>
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 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,5 @@
1
+ require 'rhb/version'
2
+ require 'rhb/builder'
3
+
4
+ module Rhb
5
+ end
@@ -0,0 +1,67 @@
1
+ module Rhb
2
+ class Builder
3
+ VOID_ELEMENTS = %w{area base br col command embed hr img input
4
+ keygen link meta param source track wbr}.freeze
5
+
6
+ undef :p
7
+
8
+ def initialize
9
+ @buffer = ''
10
+ end
11
+
12
+ def to_s
13
+ @buffer
14
+ end
15
+ alias :to_html :to_s
16
+
17
+ def doctype
18
+ @buffer << '<!DOCTYPE html>'
19
+ end
20
+
21
+ private
22
+
23
+ def element name, *args, void, &block
24
+ attrs = html_attrs extract_options! args
25
+ @buffer << "<#{name}#{' ' unless attrs.empty?}#{attrs}>"
26
+ return if void
27
+ if args.empty? and block_given?
28
+ instance_exec &block
29
+ else
30
+ @buffer << args.first.to_s
31
+ end
32
+ @buffer << "</#{name}>"
33
+ end
34
+
35
+ def cache_element name
36
+ self.class.class_eval <<-CODE, __FILE__, __LINE__ + 1
37
+ def #{name} *args, &block
38
+ element '#{name}', *args, #{void_element? name}, &block
39
+ end
40
+ CODE
41
+ end
42
+
43
+ def html_attrs attrs, prefix = ''
44
+ attrs.each_with_object('') do |(name, value), result|
45
+ result << if value.kind_of? Hash
46
+ html_attrs value, "#{prefix}#{name}-"
47
+ else
48
+ %Q{#{prefix}#{name}="#{value}" }
49
+ end
50
+ end.strip
51
+ end
52
+
53
+ def extract_options! array
54
+ return [] unless array.last.kind_of? Hash
55
+ array.pop
56
+ end
57
+
58
+ def void_element? name
59
+ VOID_ELEMENTS.include? name.to_s
60
+ end
61
+
62
+ def method_missing name, *args, &block
63
+ cache_element name
64
+ public_send name, *args, &block
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module Rhb
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'rhb/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rhb'
7
+ spec.version = Rhb::VERSION
8
+ spec.authors = ['Alexey Volodkin']
9
+ spec.email = ['a@vldkn.net']
10
+ spec.summary = %q{Builds html from ruby code}
11
+ spec.homepage = 'https://github.com/miraks/rhb'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_development_dependency 'bundler'
20
+ spec.add_development_dependency 'rake'
21
+ spec.add_development_dependency 'rspec', '3.0.0.beta2'
22
+ spec.add_development_dependency 'guard-rspec'
23
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rhb::Builder do
4
+ let(:builder) { Rhb::Builder.new }
5
+
6
+ subject { builder.to_html }
7
+
8
+ describe "content" do
9
+ it "accepts it as a string" do
10
+ builder.div 'content'
11
+ should eq '<div>content</div>'
12
+ end
13
+
14
+ it "accepts it as a block" do
15
+ builder.section do
16
+ div 'content 1'
17
+ div 'content 2'
18
+ end
19
+ should eq <<-HTML.strip
20
+ <section><div>content 1</div><div>content 2</div></section>
21
+ HTML
22
+ end
23
+ end
24
+
25
+ describe "attributes" do
26
+ it "accepts it as hash" do
27
+ builder.a 'content', href: 'link', class: 'class'
28
+ should eq '<a href="link" class="class">content</a>'
29
+ end
30
+
31
+ it "accepts nested hash" do
32
+ builder.a 'content', href: 'link', data: { link: 'link' }
33
+ should eq '<a href="link" data-link="link">content</a>'
34
+ end
35
+
36
+ it "accepts it as first argument instead content" do
37
+ builder.a href: 'link', class: 'class'
38
+ should eq '<a href="link" class="class"></a>'
39
+ end
40
+ end
41
+
42
+ describe "#doctype" do
43
+ it "adds doctype" do
44
+ builder.doctype
45
+ should eq '<!DOCTYPE html>'
46
+ end
47
+ end
48
+
49
+ context "void element" do
50
+ it "is not close it" do
51
+ builder.link
52
+ should eq '<link>'
53
+ end
54
+
55
+ it "ignores content" do
56
+ builder.link 'content'
57
+ should eq '<link>'
58
+ end
59
+ end
60
+
61
+ context "tags" do
62
+ it "processes 'p' inside block" do
63
+ builder.div do
64
+ p 'paragraph 1'
65
+ p 'paragraph 2'
66
+ end
67
+ should eq <<-HTML.strip
68
+ <div><p>paragraph 1</p><p>paragraph 2</p></div>
69
+ HTML
70
+ end
71
+ end
72
+ end
@@ -0,0 +1 @@
1
+ require 'rhb'
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rhb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Volodkin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0.beta2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0.beta2
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - a@vldkn.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - Guardfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/rhb.rb
84
+ - lib/rhb/builder.rb
85
+ - lib/rhb/version.rb
86
+ - rhb.gemspec
87
+ - spec/rhb/builder_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: https://github.com/miraks/rhb
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Builds html from ruby code
113
+ test_files:
114
+ - spec/rhb/builder_spec.rb
115
+ - spec/spec_helper.rb