html_meta 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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in html_meta.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Thomas Brewer
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,87 @@
1
+ # HtmlMeta (beta)
2
+
3
+ Helps with constructing meta tags
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'html_meta'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install html_meta
18
+
19
+ ## Usage
20
+
21
+ In an initializer file set any configuration options:
22
+
23
+ HtmlMeta.configure do |config|
24
+ config.title_separator = ' | '
25
+ end
26
+
27
+ Set some defaults in an initializer file:
28
+
29
+ HtmlMeta.define do
30
+ meta :title, '21purple Web Studios'
31
+ meta :keywords, 'expressionengine developer development php custom web application'
32
+ meta :description, 'We are an awesome web development shop'
33
+ end
34
+
35
+ And then later in a controller action or where ever modify the meta tag values:
36
+
37
+ HtmlMeta.define do
38
+ meta :title, 'Ruby Development', prepend: true
39
+ meta :keywords, 'ruby rails padrino', append: true
40
+ meta :description, 'We are an awesome ruby web development shop'
41
+ open_graph :title, 'Ruby Developer'
42
+ end
43
+
44
+ You can also use the shorthand method og for setting open graph tags:
45
+
46
+ HtmlMeta.define do
47
+ og :title, 'Ruby Developer'
48
+ end
49
+
50
+ You can also set a single meta tag value:
51
+
52
+ HtmlMeta.meta :title, 'Rails Development', prepend: true
53
+
54
+ Or you can get a meta tag value:
55
+
56
+ HtmlMeta.meta :title
57
+
58
+ The open graph tags have similar setters:
59
+
60
+ HtmlMeta.open_graph :title, 'Rails Development', prepend: true
61
+
62
+ or
63
+
64
+ HtmlMeta.og :title, 'Rails Development', prepend: true
65
+
66
+ And getters:
67
+
68
+ HtmlMeta.open_graph :title
69
+
70
+ or
71
+
72
+ HtmlMeta.og :title
73
+
74
+
75
+ And then render the html for the meta tags:
76
+
77
+ HtmlMeta.render
78
+
79
+
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/html_meta/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Thomas Brewer"]
6
+ gem.email = ["tom@21purple.com"]
7
+ gem.description = %q{Helps with constructing meta tags}
8
+ gem.summary = gem.description
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "html_meta"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = HtmlMeta::VERSION
17
+ gem.add_development_dependency('rspec')
18
+ gem.add_dependency('arbre')
19
+ end
@@ -0,0 +1,66 @@
1
+ require "html_meta/version"
2
+ require "html_meta/configuration"
3
+ require "html_meta/data"
4
+ require "arbre"
5
+
6
+ module HtmlMeta
7
+
8
+ class << self
9
+
10
+ def configuration
11
+ self.configuration
12
+ end
13
+
14
+ attr_accessor :configuration, :data
15
+
16
+ def configure
17
+ self.configuration ||= Configuration.new
18
+ yield(self.configuration) if block_given?
19
+ self.configuration
20
+ end
21
+
22
+ def define(&block)
23
+ self.data ||= Data.new
24
+ raise ArgumentError.new("You must pass a block to the method") unless block_given?
25
+ self.data.instance_eval(&block)
26
+ end
27
+
28
+ def meta(key, value = nil, options = {})
29
+ self.data ||= Data.new
30
+ self.data.meta(key, value, options)
31
+ end
32
+
33
+ def open_graph(key, value = nil, options = {})
34
+ self.data ||= Data.new
35
+ self.data.open_graph(key, value, options)
36
+ end
37
+
38
+ alias :og :open_graph
39
+
40
+ def reset
41
+ self.data ||= Data.new
42
+ self.data.reset
43
+ end
44
+
45
+ def render
46
+ self.data ||= Data.new
47
+ default = self.data.get(:default)
48
+ open_graph = self.data.get(:og)
49
+ html = Arbre::Context.new do
50
+ default.each do |key, value|
51
+ if key == :title
52
+ title value
53
+ else
54
+ meta(name: key.to_s, content: value)
55
+ end
56
+ end
57
+ open_graph.each do |key, value|
58
+ meta(property: 'og:' + key.to_s, content: value)
59
+ end
60
+ end
61
+ html.to_s
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,5 @@
1
+ module HtmlMeta
2
+ class Configuration
3
+ attr_accessor :title_separator
4
+ end
5
+ end
@@ -0,0 +1,62 @@
1
+ module HtmlMeta
2
+
3
+ class Data
4
+
5
+ def initialize
6
+ @data = {}
7
+ end
8
+
9
+ def reset
10
+ @data = {}
11
+ end
12
+
13
+ def data(scope = :default)
14
+ @data[scope] ||= {}
15
+ end
16
+
17
+ def get(scope, key = nil)
18
+ if key.nil?
19
+ data(scope)
20
+ else
21
+ data(scope).fetch(key) { nil }
22
+ end
23
+ end
24
+
25
+ def set(scope, key, value = nil, options = {})
26
+ separator = options.fetch(:separator, ' ')
27
+ value = Array(value)
28
+ if options.fetch(:prepend, false)
29
+ value << data(scope).fetch(key, '')
30
+ elsif options.fetch(:append, false)
31
+ value.insert(0, data(scope).fetch(key, ''))
32
+ end
33
+ data(scope)[key] = value.compact.join(separator)
34
+ end
35
+
36
+ def meta(key, value = nil, options = {})
37
+ if value.nil?
38
+ return get(:default, key)
39
+ else
40
+ if key == :title &&
41
+ unless options.key?(:separator)
42
+ options[:separator] = HtmlMeta.configuration.title_separator
43
+ end
44
+ end
45
+ set(:default, key, value, options)
46
+ end
47
+ end
48
+
49
+ def open_graph(key, value = nil, options = {})
50
+ if value.nil?
51
+ return get(:og, key)
52
+ else
53
+ #we are setting a value
54
+ set(:og, key, value, options)
55
+ end
56
+ end
57
+
58
+ alias :og :open_graph
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,3 @@
1
+ module HtmlMeta
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,154 @@
1
+ require 'spec_helper'
2
+
3
+ describe HtmlMeta do
4
+
5
+ context "Configuration" do
6
+
7
+ it "should present a configuration object via a block" do
8
+ configuration = nil
9
+ HtmlMeta.configure do |config|
10
+ configuration = config
11
+ end
12
+ configuration.should be_instance_of(HtmlMeta::Configuration)
13
+ end
14
+
15
+ it "should let me set a title_separator option on the configuration object" do
16
+ configuration = nil
17
+ HtmlMeta.configure do |config|
18
+ configuration = config
19
+ config.title_separator = ' | '
20
+ end
21
+ configuration.title_separator.should eq(' | ')
22
+ end
23
+
24
+ end
25
+
26
+ context "Defining" do
27
+
28
+ before do
29
+ HtmlMeta.reset
30
+ end
31
+
32
+ it "should allow you to set meta tag data via a define block" do
33
+ title = '21purple'
34
+ HtmlMeta.define do
35
+ meta :title, title
36
+ end
37
+ HtmlMeta.meta(:title).should eq(title)
38
+ end
39
+
40
+ end
41
+
42
+ context "ReDefining" do
43
+
44
+ before do
45
+ HtmlMeta.reset
46
+ HtmlMeta.define do
47
+ meta :keywords, 'rails development'
48
+ end
49
+ end
50
+
51
+ it "should allow you to set new values for meta tag data that has already been set" do
52
+ HtmlMeta.define do
53
+ meta :keywords, 'rails padrino development'
54
+ end
55
+ HtmlMeta.meta(:keywords).should_not eq('rails development')
56
+ HtmlMeta.meta(:keywords).should eq('rails padrino development')
57
+ end
58
+
59
+ it "should allow you to pass a prepend option to prepend the new value for a meta tag" do
60
+ HtmlMeta.define do
61
+ meta :keywords, 'padrino', prepend: true
62
+ end
63
+ HtmlMeta.meta(:keywords).should_not eq('rails development')
64
+ HtmlMeta.meta(:keywords).should eq('padrino rails development')
65
+ end
66
+
67
+ it "should allow you to pass a append option to prepend the new value for a meta tag" do
68
+ HtmlMeta.define do
69
+ meta :keywords, 'padrino', append: true
70
+ end
71
+ HtmlMeta.meta(:keywords).should_not eq('rails development')
72
+ HtmlMeta.meta(:keywords).should eq('rails development padrino')
73
+ end
74
+
75
+ it "should allow you to pass a separator option" do
76
+ HtmlMeta.define do
77
+ meta :keywords, 'padrino', append: true, separator: ','
78
+ end
79
+ HtmlMeta.meta(:keywords).should_not eq('rails development')
80
+ HtmlMeta.meta(:keywords).should eq('rails development,padrino')
81
+ end
82
+
83
+ it "should use the configuration title_separator when modifying the :title" do
84
+ HtmlMeta.define do
85
+ meta :title, '21purple'
86
+ end
87
+ HtmlMeta.define do
88
+ meta :title, 'Blog', prepend: true
89
+ end
90
+ HtmlMeta.meta(:title).should eq('Blog | 21purple')
91
+ end
92
+
93
+ it "should not use the configuration title_separator when modifying the :title if a separator options is passed" do
94
+ HtmlMeta.define do
95
+ meta :title, '21purple'
96
+ end
97
+ HtmlMeta.define do
98
+ meta :title, 'Blog', prepend: true, separator: ' - '
99
+ end
100
+ HtmlMeta.meta(:title).should_not eq('Blog | 21purple')
101
+ HtmlMeta.meta(:title).should eq('Blog - 21purple')
102
+ end
103
+
104
+ end
105
+
106
+ context "OpenGraph" do
107
+
108
+ before do
109
+ HtmlMeta.reset
110
+ end
111
+
112
+ it "should allow you to set open graph meta tags" do
113
+ HtmlMeta.define do
114
+ open_graph :title, '21purple'
115
+ end
116
+ HtmlMeta.open_graph(:title).should eq('21purple')
117
+ end
118
+
119
+ it "should allow you to set open graph meta tags with the shorthand method" do
120
+ HtmlMeta.define do
121
+ og :title, '21purple'
122
+ end
123
+ HtmlMeta.og(:title).should eq('21purple')
124
+ end
125
+
126
+ end
127
+
128
+ context "Rendering" do
129
+
130
+ before do
131
+ HtmlMeta.reset
132
+ end
133
+
134
+ it "should render the proper html for the meta tags set" do
135
+
136
+ HtmlMeta.define do
137
+ meta :title, '21purple'
138
+ end
139
+
140
+ HtmlMeta.define do
141
+ meta :title, 'Blog', prepend: true
142
+ meta :keywords, 'rails development'
143
+ og :title, '21purple'
144
+ end
145
+
146
+ HtmlMeta.render.should eq("<title>Blog | 21purple</title>\n<meta name=\"keywords\" content=\"rails development\"/>\n<meta property=\"og:title\" content=\"21purple\"/>\n")
147
+ end
148
+
149
+ end
150
+
151
+ end
152
+
153
+
154
+
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'rspec'
3
+ require 'html_meta'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_meta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Brewer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70367021747960 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70367021747960
25
+ - !ruby/object:Gem::Dependency
26
+ name: arbre
27
+ requirement: &70367021747540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70367021747540
36
+ description: Helps with constructing meta tags
37
+ email:
38
+ - tom@21purple.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - html_meta.gemspec
49
+ - lib/html_meta.rb
50
+ - lib/html_meta/configuration.rb
51
+ - lib/html_meta/data.rb
52
+ - lib/html_meta/version.rb
53
+ - spec/html_meta_spec.rb
54
+ - spec/spec_helper.rb
55
+ homepage: ''
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.10
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Helps with constructing meta tags
79
+ test_files:
80
+ - spec/html_meta_spec.rb
81
+ - spec/spec_helper.rb