metamagic 3.1.0 → 3.1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +70 -9
- data/lib/metamagic/renderer.rb +38 -5
- data/lib/metamagic/tag.rb +24 -2
- data/lib/metamagic/tags/custom_tag.rb +1 -1
- data/lib/metamagic/tags/link_tag.rb +1 -1
- data/lib/metamagic/tags/meta_tag.rb +2 -2
- data/lib/metamagic/tags/property_tag.rb +1 -1
- data/lib/metamagic/tags/title_tag.rb +1 -16
- data/lib/metamagic/version.rb +1 -1
- data/lib/metamagic/view_helper.rb +8 -2
- data/test/dummy/app/helpers/application_helper.rb +3 -0
- data/test/meta_tag_test.rb +19 -0
- data/test/property_tag_test.rb +7 -0
- data/test/title_tag_test.rb +42 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5339c62a7823d6648e9df00ece50ef96c63ae8e8
|
4
|
+
data.tar.gz: 2d520fb7ef485717ff88f0a839ca39f010592a73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c30629cae86fd8fbeaddfeca49d2ac19c05678541d9bfc75e3ed702cfbd9ca90ff4db26bbbeff3bef5e31d436aad198bd46f8bbbb9d5723322f22ef8c8fe50a
|
7
|
+
data.tar.gz: 372ce9d3f776f2fea1dfc42bdee3e9fe85a65d645c4a54ddc4243d392dd1bb06c1b52b1b6aa804b1b1e98cecd679725ab7eaabb5b260a5801d844386615bb341
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -56,7 +56,16 @@ This will generate the following:
|
|
56
56
|
</head>
|
57
57
|
```
|
58
58
|
|
59
|
-
###
|
59
|
+
### Using templates
|
60
|
+
|
61
|
+
Templates can be used to insert meta values from your views into your layouts.
|
62
|
+
This is usable for example if you want to add something to your title on all
|
63
|
+
pages, or have some default keywords added to all pages. Templates work with
|
64
|
+
all tag types, including OpenGraph, Twitter, and others.
|
65
|
+
|
66
|
+
See below for examples on using templates.
|
67
|
+
|
68
|
+
#### Title templates
|
60
69
|
|
61
70
|
Title templates can be used to automatically insert the name of your site into the meta title.
|
62
71
|
|
@@ -72,7 +81,7 @@ In your layout:
|
|
72
81
|
|
73
82
|
```erb
|
74
83
|
<%
|
75
|
-
metamagic site: "My Site",
|
84
|
+
metamagic site: "My Site", title: [:title, :site], separator: " — "
|
76
85
|
%>
|
77
86
|
```
|
78
87
|
|
@@ -85,16 +94,73 @@ This will render the following:
|
|
85
94
|
</head>
|
86
95
|
```
|
87
96
|
|
97
|
+
The default separator is ` - `.
|
98
|
+
|
99
|
+
If you hadn't set the title in your view, it would just display the site name.
|
100
|
+
|
88
101
|
You can also use a proc to enable custom processing:
|
89
102
|
|
90
103
|
```erb
|
91
104
|
<%
|
92
|
-
metamagic site: "My Site",
|
105
|
+
metamagic site: "My Site", title: -> { title.include?(site) ? title : "#{title} — #{site}" }
|
93
106
|
%>
|
94
107
|
```
|
95
108
|
|
96
109
|
This will insert the site name only if it is not already present in the title.
|
97
110
|
|
111
|
+
You could also do this with a view helper:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
module ApplicationHelper
|
115
|
+
def meta_title_for(title, site)
|
116
|
+
title.include?(site) ? title : "#{title} — #{site}"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
```erb
|
122
|
+
<%
|
123
|
+
metamagic site: "My Site", title: -> { meta_title_for(title, site) }
|
124
|
+
%>
|
125
|
+
```
|
126
|
+
|
127
|
+
The proc is still needed here to ensure the right context for the template.
|
128
|
+
|
129
|
+
#### Keywords template
|
130
|
+
|
131
|
+
Keyword templates can be used to add some default keywords to all pages on your site.
|
132
|
+
|
133
|
+
In your template:
|
134
|
+
|
135
|
+
```erb
|
136
|
+
<%
|
137
|
+
meta keywords: %{one two three}
|
138
|
+
%>
|
139
|
+
```
|
140
|
+
|
141
|
+
In your layout:
|
142
|
+
|
143
|
+
```erb
|
144
|
+
<%
|
145
|
+
metamagic keywords: [:keywords, "four", "five", "six"]
|
146
|
+
%>
|
147
|
+
```
|
148
|
+
|
149
|
+
This will render the following:
|
150
|
+
|
151
|
+
```html
|
152
|
+
<head>
|
153
|
+
<meta content="one, two, three, four, five, six" name="keywords" />
|
154
|
+
...
|
155
|
+
</head>
|
156
|
+
```
|
157
|
+
|
158
|
+
#### Adding templates for other tag types
|
159
|
+
|
160
|
+
Templates are supported on all tag types. You can access the values set in the
|
161
|
+
view by replacing colons (`:`) in your meta keys with underscores (`_`), so for
|
162
|
+
example `og:image` can be accessed with `og_image`.
|
163
|
+
|
98
164
|
### Shortcut helpers
|
99
165
|
|
100
166
|
For easy setting of meta tags, you can use the shortcut helpers like this:
|
@@ -335,17 +401,12 @@ Requirements
|
|
335
401
|
* Rails 3.0 or above
|
336
402
|
* Ruby 1.9 or above
|
337
403
|
|
338
|
-
Versioning
|
339
|
-
----------
|
340
|
-
|
341
|
-
Follows [semantic versioning](http://semver.org/).
|
342
|
-
|
343
404
|
Contributing
|
344
405
|
------------
|
345
406
|
|
346
407
|
1. Fork the project
|
347
408
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
348
|
-
3. Make your changes and make sure the tests pass (run `rake`)
|
409
|
+
3. Make your changes, including tests, and make sure the tests pass (run `rake`)
|
349
410
|
4. Commit your changes (`git commit -am 'Add new feature'`)
|
350
411
|
5. Push to the branch (`git push origin my-new-feature`)
|
351
412
|
6. Create new pull request
|
data/lib/metamagic/renderer.rb
CHANGED
@@ -10,6 +10,8 @@ module Metamagic
|
|
10
10
|
twitter: TwitterTag
|
11
11
|
}
|
12
12
|
|
13
|
+
DEFAULT_SEPARATOR = " - "
|
14
|
+
|
13
15
|
class << self
|
14
16
|
def tag_types
|
15
17
|
@tag_types ||= DEFAULT_TAG_TYPES.dup
|
@@ -35,20 +37,37 @@ module Metamagic
|
|
35
37
|
@tags ||= []
|
36
38
|
end
|
37
39
|
|
38
|
-
def add(hash = {})
|
40
|
+
def add(hash = {}, enable_templates = false)
|
39
41
|
raise ArgumentError, "Defining meta properties via arrays has been removed in Metamagic v3.0 and replaced by some pretty helpers. Please see the readme at https://github.com/lassebunk/metamagic for more info." if hash.is_a?(Array)
|
40
42
|
|
41
|
-
transform_hash(hash).each do |
|
42
|
-
|
43
|
+
transform_hash(hash).each do |key, value|
|
44
|
+
if enable_templates && is_template?(value)
|
45
|
+
add_template key, value
|
46
|
+
value = nil
|
47
|
+
end
|
48
|
+
|
49
|
+
klass = self.class.tag_type_for_key(key)
|
43
50
|
tag = if klass.is_a?(Proc)
|
44
|
-
CustomTag.new(self,
|
51
|
+
CustomTag.new(self, key, value, klass)
|
45
52
|
else
|
46
|
-
klass.new(self,
|
53
|
+
klass.new(self, key, value)
|
47
54
|
end
|
48
55
|
tags << tag unless tags.include?(tag)
|
49
56
|
end
|
50
57
|
end
|
51
58
|
|
59
|
+
def templates
|
60
|
+
@templates ||= {}
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_template(key, value)
|
64
|
+
templates[key] = value
|
65
|
+
end
|
66
|
+
|
67
|
+
def template_for(key)
|
68
|
+
templates[key]
|
69
|
+
end
|
70
|
+
|
52
71
|
def has_tag_type?(prefix)
|
53
72
|
self.class.tag_types.has_key?(prefix)
|
54
73
|
end
|
@@ -65,6 +84,12 @@ module Metamagic
|
|
65
84
|
|
66
85
|
attr_writer :site
|
67
86
|
|
87
|
+
def separator
|
88
|
+
@separator ||= DEFAULT_SEPARATOR
|
89
|
+
end
|
90
|
+
|
91
|
+
attr_writer :separator
|
92
|
+
|
68
93
|
def render
|
69
94
|
tags.sort.map(&:to_html).compact.join("\n").html_safe
|
70
95
|
end
|
@@ -75,6 +100,14 @@ module Metamagic
|
|
75
100
|
|
76
101
|
private
|
77
102
|
|
103
|
+
def is_template?(value)
|
104
|
+
Array(value).any? do |val|
|
105
|
+
val.is_a?(Proc) ||
|
106
|
+
val.is_a?(Symbol) ||
|
107
|
+
val =~ /:\w+/
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
78
111
|
# Transforms a nested hash into meta property keys.
|
79
112
|
def transform_hash(hash, path = "")
|
80
113
|
hash.each_with_object({}) do |(k, v), ret|
|
data/lib/metamagic/tag.rb
CHANGED
@@ -19,6 +19,27 @@ module Metamagic
|
|
19
19
|
true
|
20
20
|
end
|
21
21
|
|
22
|
+
def template
|
23
|
+
@template ||= template_for(key) || :value
|
24
|
+
end
|
25
|
+
|
26
|
+
def interpolated_values
|
27
|
+
@interpolated_values ||= Array(template).map do |template|
|
28
|
+
case template
|
29
|
+
when Proc
|
30
|
+
instance_exec(&template)
|
31
|
+
when Symbol
|
32
|
+
send(template)
|
33
|
+
when String
|
34
|
+
template.gsub(/:\w+/) do |key|
|
35
|
+
send(key[1..-1])
|
36
|
+
end
|
37
|
+
else
|
38
|
+
raise "Unknown template type #{template.class}."
|
39
|
+
end
|
40
|
+
end.flatten.compact.uniq
|
41
|
+
end
|
42
|
+
|
22
43
|
def ==(other)
|
23
44
|
self.class == other.class && self.key == other.key
|
24
45
|
end
|
@@ -27,8 +48,9 @@ module Metamagic
|
|
27
48
|
[sort_order, self.class.name] <=> [other.sort_order, other.class.name]
|
28
49
|
end
|
29
50
|
|
30
|
-
def method_missing(*args)
|
31
|
-
|
51
|
+
def method_missing(method, *args, &block)
|
52
|
+
return value if method.to_s == key.gsub(":", "_") # When calling e.g. `og_image`. Used for interpolating values.
|
53
|
+
context.send(method, *args, &block)
|
32
54
|
end
|
33
55
|
end
|
34
56
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module Metamagic
|
2
2
|
class MetaTag < Tag
|
3
3
|
def to_html
|
4
|
-
return
|
5
|
-
tag(:meta, name: key, content:
|
4
|
+
return if interpolated_values.empty?
|
5
|
+
tag(:meta, name: key, content: interpolated_values.join(", "))
|
6
6
|
end
|
7
7
|
|
8
8
|
def sort_order
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Metamagic
|
2
2
|
class PropertyTag < Tag
|
3
3
|
def to_html
|
4
|
-
|
4
|
+
interpolated_values.map { |value| tag(:meta, property: key, content: value) }.join("\n").html_safe.presence
|
5
5
|
end
|
6
6
|
|
7
7
|
def sort_order
|
@@ -1,22 +1,7 @@
|
|
1
1
|
module Metamagic
|
2
2
|
class TitleTag < Tag
|
3
3
|
def to_html
|
4
|
-
|
5
|
-
instance_exec(&title_template)
|
6
|
-
else
|
7
|
-
title_template.gsub(/:\w+/) do |key|
|
8
|
-
if key == ":title"
|
9
|
-
value
|
10
|
-
else
|
11
|
-
send(key[1..-1])
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
content_tag(:title, interpolated_title) if interpolated_title.present?
|
16
|
-
end
|
17
|
-
|
18
|
-
def title
|
19
|
-
value
|
4
|
+
content_tag(:title, interpolated_values.join(separator)) if interpolated_values.any?
|
20
5
|
end
|
21
6
|
|
22
7
|
def sort_order
|
data/lib/metamagic/version.rb
CHANGED
@@ -5,13 +5,19 @@ module Metamagic
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def metamagic(hash = {})
|
8
|
+
if title_template = hash.delete(:title_template)
|
9
|
+
# Deprecation warning
|
10
|
+
Rails.logger.warn "[Metamagic] Using `metamagic title_template: #{title_template.inspect}` has been deprecated. Please use `metamagic title: #{title_template.inspect}` instead."
|
11
|
+
hash[:title] = title_template
|
12
|
+
end
|
13
|
+
|
8
14
|
# Loop through special options
|
9
|
-
hash.slice(:
|
15
|
+
hash.slice(:site, :separator).each do |key, value|
|
10
16
|
metamagic_renderer.send("#{key}=", value)
|
11
17
|
hash.delete key
|
12
18
|
end
|
13
19
|
|
14
|
-
metamagic_renderer.add hash
|
20
|
+
metamagic_renderer.add hash, true
|
15
21
|
metamagic_renderer.render
|
16
22
|
end
|
17
23
|
|
data/test/meta_tag_test.rb
CHANGED
@@ -57,4 +57,23 @@ class MetaTagTest < ActionView::TestCase
|
|
57
57
|
assert_equal %{<title>Test Title</title>},
|
58
58
|
metamagic
|
59
59
|
end
|
60
|
+
|
61
|
+
test "keywords template" do
|
62
|
+
keywords %w{added keywords}
|
63
|
+
|
64
|
+
assert_equal %{<meta content="added, keywords, default, from, layout" name="keywords" />},
|
65
|
+
metamagic(keywords: [:keywords, "default", "from", "layout"])
|
66
|
+
end
|
67
|
+
|
68
|
+
test "keywords template with no keywords" do
|
69
|
+
assert_equal %{<meta content="default, from, layout" name="keywords" />},
|
70
|
+
metamagic(keywords: [:keywords, "default", "from", "layout"])
|
71
|
+
end
|
72
|
+
|
73
|
+
test "unique values using templates" do
|
74
|
+
keywords %w{added keywords}
|
75
|
+
|
76
|
+
assert_equal %{<meta content="added, keywords, default, from, layout" name="keywords" />},
|
77
|
+
metamagic(keywords: [:keywords, "added", "default", "keywords", "from", "layout"])
|
78
|
+
end
|
60
79
|
end
|
data/test/property_tag_test.rb
CHANGED
@@ -52,4 +52,11 @@ class PropertyTagTest < ActionView::TestCase
|
|
52
52
|
assert_equal %{<meta content="http://test.com/image.png" property="og:image" />\n<meta content="Leif Davidsen" property="og:book:author" />\n<meta content="Anders Mogensen" property="og:book:author" />},
|
53
53
|
metamagic
|
54
54
|
end
|
55
|
+
|
56
|
+
test "property template" do
|
57
|
+
og image: "http://test.com/image.jpg"
|
58
|
+
|
59
|
+
assert_equal %{<meta content="http://test.com/image.jpg" property="og:image" />\n<meta content="http://test.com/image2.jpg" property="og:image" />},
|
60
|
+
metamagic(og: { image: [:og_image, "http://test.com/image2.jpg"] })
|
61
|
+
end
|
55
62
|
end
|
data/test/title_tag_test.rb
CHANGED
@@ -2,6 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class TitleTagTest < ActionView::TestCase
|
4
4
|
include Metamagic::ViewHelper
|
5
|
+
include ApplicationHelper
|
5
6
|
|
6
7
|
test "title tag" do
|
7
8
|
meta title: "My Title"
|
@@ -28,27 +29,60 @@ class TitleTagTest < ActionView::TestCase
|
|
28
29
|
test "title template" do
|
29
30
|
title "Test Title"
|
30
31
|
|
31
|
-
assert_equal %{<title>Test Title
|
32
|
-
metamagic(site: "My Site",
|
32
|
+
assert_equal %{<title>Test Title - My Site</title>},
|
33
|
+
metamagic(site: "My Site", title: ":title - :site")
|
33
34
|
end
|
34
35
|
|
35
|
-
test "title template with
|
36
|
-
assert_equal %{<title>
|
37
|
-
metamagic(site: "My Site", title: "
|
36
|
+
test "title template with no title set" do
|
37
|
+
assert_equal %{<title> - My Site</title>},
|
38
|
+
metamagic(site: "My Site", title: ":title - :site")
|
39
|
+
end
|
40
|
+
|
41
|
+
test "title separator" do
|
42
|
+
title "Test Title"
|
43
|
+
|
44
|
+
assert_equal %{<title>Test Title - My Site</title>},
|
45
|
+
metamagic(site: "My Site", title: [:title, :site])
|
46
|
+
end
|
47
|
+
|
48
|
+
test "custom title separator" do
|
49
|
+
title "Test Title"
|
50
|
+
|
51
|
+
assert_equal %{<title>Test Title | My Site</title>},
|
52
|
+
metamagic(site: "My Site", separator: " | ", title: [:title, :site])
|
53
|
+
end
|
54
|
+
|
55
|
+
test "title separator with no title" do
|
56
|
+
assert_equal %{<title>My Site</title>},
|
57
|
+
metamagic(site: "My Site", title: [:title, :site])
|
38
58
|
end
|
39
59
|
|
40
60
|
test "title template with nil site" do
|
41
61
|
title "Test Title"
|
42
62
|
|
43
63
|
assert_raises RuntimeError do
|
44
|
-
metamagic(
|
64
|
+
metamagic(title: ":title - :site")
|
45
65
|
end
|
46
66
|
end
|
47
67
|
|
48
68
|
test "title template proc" do
|
49
69
|
title "Test Title"
|
50
70
|
|
51
|
-
assert_equal %{<title>Site: My Site
|
52
|
-
metamagic(site: "My Site",
|
71
|
+
assert_equal %{<title>Site: My Site - Title: Test Title</title>},
|
72
|
+
metamagic(site: "My Site", title: -> { "Site: #{site} - Title: #{title}" })
|
73
|
+
end
|
74
|
+
|
75
|
+
test "title template from view helper" do
|
76
|
+
title "Test Title"
|
77
|
+
|
78
|
+
assert_equal %{<title>From view helper: Test Title - My Site</title>},
|
79
|
+
metamagic(site: "My Site", title: -> { meta_title_for(site, title) })
|
80
|
+
end
|
81
|
+
|
82
|
+
test "deprecated title_template option" do
|
83
|
+
title "Test Title"
|
84
|
+
|
85
|
+
assert_equal %{<title>Test Title - My Site</title>},
|
86
|
+
metamagic(site: "My Site", title_template: ":title - :site")
|
53
87
|
end
|
54
88
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metamagic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.1.
|
4
|
+
version: 3.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lasse Bunk
|
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
134
|
version: '0'
|
135
135
|
requirements: []
|
136
136
|
rubyforge_project:
|
137
|
-
rubygems_version: 2.2.
|
137
|
+
rubygems_version: 2.2.2
|
138
138
|
signing_key:
|
139
139
|
specification_version: 4
|
140
140
|
summary: Simple Ruby on Rails plugin for creating meta tags.
|