page_meta 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby-tests.yml +2 -3
- data/.rubocop.yml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +17 -0
- data/gemfiles/{6_1.gemfile → 7_2.gemfile} +1 -1
- data/lib/page_meta/base.rb +102 -35
- data/lib/page_meta/meta_tag/dns_prefetch_control.rb +9 -2
- data/lib/page_meta/meta_tag/title.rb +1 -2
- data/lib/page_meta/meta_tag.rb +5 -2
- data/lib/page_meta/open_tag.rb +24 -0
- data/lib/page_meta/tag.rb +24 -0
- data/lib/page_meta/translator.rb +14 -5
- data/lib/page_meta/version.rb +1 -1
- data/lib/page_meta.rb +2 -0
- data/page_meta.gemspec +1 -2
- metadata +7 -23
- data/gemfiles/7_0.gemfile +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30f096278376e70c56074428c6ec88f786a6a1ac893a8da66829884efc9e34dc
|
4
|
+
data.tar.gz: 4d635e8a75a48be6acedd134df338e73085f0f7f8326143b9bcfc8436ffc12e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24cf0db7b385568fcc471bc390b85b3d6c5e24972c5d54bd0cce251f9ee1058f917c096534f4bccd7f66f220e9efdcfa3d87c75c4dbf9c0a3d6ac0afe576a758
|
7
|
+
data.tar.gz: bcdefe6bbc2b57010848b2466023b3f1da305e9d745715f2373aeac33c9bf2ff9220edfb9c298772d11dd49f443c7985f91a0c431156e7917009ca7505b1d9d2
|
@@ -19,12 +19,11 @@ jobs:
|
|
19
19
|
strategy:
|
20
20
|
fail-fast: false
|
21
21
|
matrix:
|
22
|
-
ruby: ["3.
|
22
|
+
ruby: ["3.2", "3.3", "3.4"]
|
23
23
|
gemfile:
|
24
24
|
- Gemfile
|
25
|
+
- gemfiles/7_2.gemfile
|
25
26
|
- gemfiles/7_1.gemfile
|
26
|
-
- gemfiles/7_0.gemfile
|
27
|
-
- gemfiles/6_1.gemfile
|
28
27
|
|
29
28
|
steps:
|
30
29
|
- uses: actions/checkout@v4
|
data/.rubocop.yml
CHANGED
@@ -3,7 +3,7 @@ inherit_gem:
|
|
3
3
|
rubocop-fnando: .rubocop.yml
|
4
4
|
|
5
5
|
AllCops:
|
6
|
-
TargetRubyVersion: 3.
|
6
|
+
TargetRubyVersion: 3.2
|
7
7
|
NewCops: enable
|
8
8
|
Exclude:
|
9
9
|
- test/support/dummy.rb
|
@@ -11,9 +11,6 @@ AllCops:
|
|
11
11
|
- bin/**/*
|
12
12
|
- gemfiles/vendor/**/*
|
13
13
|
|
14
|
-
Layout/LineLength:
|
15
|
-
Enabled: false
|
16
|
-
|
17
14
|
Metrics:
|
18
15
|
Enabled: false
|
19
16
|
|
@@ -22,3 +19,6 @@ Minitest/MultipleAssertions:
|
|
22
19
|
|
23
20
|
Gemspec/DevelopmentDependencies:
|
24
21
|
Enabled: false
|
22
|
+
|
23
|
+
Minitest/EmptyLineBeforeAssertionMethods:
|
24
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -11,6 +11,18 @@ Prefix your message with one of the following:
|
|
11
11
|
- [Security] in case of vulnerabilities.
|
12
12
|
-->
|
13
13
|
|
14
|
+
## Unreleased
|
15
|
+
|
16
|
+
- [Added] Add support for placeholders in base title.
|
17
|
+
- [Changed] Set minimum ruby version to 3.2.
|
18
|
+
- [Added] Add method to delete items by their name.
|
19
|
+
|
20
|
+
## 1.3.0
|
21
|
+
|
22
|
+
- [Added] `page_meta.base` will now set the base url.
|
23
|
+
- [Changed] Some tags will be enforced to show up first. It loosely follows
|
24
|
+
https://rviscomi.github.io/capo.js/user/rules/.
|
25
|
+
|
14
26
|
## 1.2.0
|
15
27
|
|
16
28
|
- [Added] The meta tag's content can also be any object that responds to the
|
data/README.md
CHANGED
@@ -112,6 +112,14 @@ en:
|
|
112
112
|
keywords: "myapp, thing, other thing"
|
113
113
|
```
|
114
114
|
|
115
|
+
### Defining base url
|
116
|
+
|
117
|
+
You can define the base url.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
page_meta.base "https://example.com/"
|
121
|
+
```
|
122
|
+
|
115
123
|
### Defining meta tags
|
116
124
|
|
117
125
|
To define other meta tags, you have to use `PageMeta::Base#tag` like the
|
@@ -223,6 +231,15 @@ would do.
|
|
223
231
|
[Please read Rails docs](http://guides.rubyonrails.org/i18n.html#using-safe-html-translations)
|
224
232
|
for more info.
|
225
233
|
|
234
|
+
### Removing meta tags
|
235
|
+
|
236
|
+
You can use `PageMeta::Base#delete` to remove meta tags. All matching tags will
|
237
|
+
be removed.
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
page_meta.delete(:description)
|
241
|
+
```
|
242
|
+
|
226
243
|
## Maintainer
|
227
244
|
|
228
245
|
- [Nando Vieira](https://github.com/fnando)
|
data/lib/page_meta/base.rb
CHANGED
@@ -6,11 +6,29 @@ module PageMeta
|
|
6
6
|
language
|
7
7
|
charset
|
8
8
|
title
|
9
|
+
viewport
|
9
10
|
keywords
|
10
11
|
description
|
11
|
-
viewport
|
12
12
|
].freeze
|
13
13
|
|
14
|
+
DEFAULT_ORDER = 99
|
15
|
+
|
16
|
+
META_TAG_ORDER = {
|
17
|
+
pragma: -1,
|
18
|
+
cache_control: -1,
|
19
|
+
expires: -1,
|
20
|
+
refresh: -1,
|
21
|
+
dns_prefetch_control: 4
|
22
|
+
}.freeze
|
23
|
+
|
24
|
+
LINK_ORDER = {
|
25
|
+
preconnect: 6,
|
26
|
+
preload: 7,
|
27
|
+
modulepreload: 8,
|
28
|
+
prefetch: 9,
|
29
|
+
dns_prefetch: 10
|
30
|
+
}.freeze
|
31
|
+
|
14
32
|
attr_reader :controller, :store
|
15
33
|
|
16
34
|
delegate :[], :[]=, to: :store
|
@@ -21,86 +39,135 @@ module PageMeta
|
|
21
39
|
@store = {}
|
22
40
|
end
|
23
41
|
|
24
|
-
def
|
25
|
-
@
|
42
|
+
def items
|
43
|
+
@items ||= []
|
44
|
+
end
|
45
|
+
|
46
|
+
# Add a new `<base>` tag.
|
47
|
+
def base(href)
|
48
|
+
items << {
|
49
|
+
type: :tag,
|
50
|
+
name: :base,
|
51
|
+
value: {href:},
|
52
|
+
order: 2,
|
53
|
+
open: true
|
54
|
+
}
|
26
55
|
end
|
27
56
|
|
28
|
-
|
29
|
-
|
57
|
+
# Delete all matching meta tags by name.
|
58
|
+
def delete(name)
|
59
|
+
items.delete_if { _1[:name] == name }
|
30
60
|
end
|
31
61
|
|
32
|
-
|
33
|
-
|
62
|
+
# Add a new meta tag.
|
63
|
+
def tag(name, value = nil, order: nil, **kwargs)
|
64
|
+
order = order || META_TAG_ORDER[name] || DEFAULT_ORDER
|
65
|
+
items << {type: :meta, name:, value: value || kwargs, order:}
|
34
66
|
end
|
35
67
|
|
36
|
-
|
37
|
-
|
68
|
+
# Add a new link tag.
|
69
|
+
def link(rel, order: nil, **options)
|
70
|
+
order = order || LINK_ORDER[rel] || DEFAULT_ORDER
|
71
|
+
items << {type: :link, rel:, options:, order:}
|
38
72
|
end
|
39
73
|
|
74
|
+
# Add new html tag, like `<base>` or `<title>`.
|
75
|
+
def html(name, value, open: false, order: DEFAULT_ORDER)
|
76
|
+
items << {type: :tag, name:, order:, open:, value:}
|
77
|
+
end
|
78
|
+
|
79
|
+
# The title translation.
|
40
80
|
def title
|
41
81
|
@title ||= Translator.new(:titles, naming, store)
|
42
82
|
end
|
43
83
|
|
84
|
+
# The description translation.
|
44
85
|
def description(html: false)
|
45
|
-
@description[html] ||= Translator.new(
|
86
|
+
@description[html] ||= Translator.new(
|
87
|
+
:descriptions,
|
88
|
+
naming,
|
89
|
+
store.merge(html:)
|
90
|
+
)
|
46
91
|
end
|
47
92
|
|
93
|
+
# The keywords translation.
|
48
94
|
def keywords
|
49
95
|
@keywords ||= Translator.new(:keywords, naming, store)
|
50
96
|
end
|
51
97
|
|
52
98
|
def render
|
53
|
-
|
54
|
-
|
99
|
+
compute_default_items
|
100
|
+
|
101
|
+
items
|
102
|
+
.sort_by { _1[:order] }
|
103
|
+
.map { send(:"build_#{_1[:type]}", _1).render }
|
104
|
+
.join
|
105
|
+
.html_safe
|
55
106
|
end
|
56
107
|
alias to_s render
|
57
108
|
|
58
|
-
def
|
59
|
-
|
109
|
+
private def build_tag(item)
|
110
|
+
klass = item[:open] ? OpenTag : Tag
|
111
|
+
|
112
|
+
klass.build(item[:name], item[:value])
|
60
113
|
end
|
61
114
|
|
62
|
-
def
|
63
|
-
|
64
|
-
.map {|name, value| MetaTag.build(name, value).render }
|
65
|
-
.join
|
66
|
-
.html_safe
|
115
|
+
private def build_meta(item)
|
116
|
+
MetaTag.build(item[:name], item[:value])
|
67
117
|
end
|
68
118
|
|
69
|
-
def
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
119
|
+
private def build_link(item)
|
120
|
+
Link.build(item[:rel], item[:options])
|
121
|
+
end
|
122
|
+
|
123
|
+
private def naming
|
124
|
+
@naming ||= Naming.new(controller)
|
74
125
|
end
|
75
126
|
|
76
|
-
def
|
127
|
+
private def compute_default_items
|
77
128
|
DEFAULT_META_TAGS.each do |method_name|
|
78
|
-
|
129
|
+
send(:"compute_default_#{method_name}")
|
79
130
|
end
|
80
131
|
end
|
81
132
|
|
82
|
-
def compute_default_language
|
133
|
+
private def compute_default_language
|
83
134
|
tag(:language, I18n.locale)
|
84
135
|
end
|
85
136
|
|
86
|
-
def compute_default_title
|
87
|
-
|
137
|
+
private def compute_default_title
|
138
|
+
return if has?(:title)
|
139
|
+
return if title.to_s.blank?
|
140
|
+
|
141
|
+
html(:title, title.to_s, order: 3)
|
142
|
+
tag(:title, title)
|
88
143
|
end
|
89
144
|
|
90
|
-
def compute_default_charset
|
91
|
-
|
145
|
+
private def compute_default_charset
|
146
|
+
return if has?(:charset)
|
147
|
+
|
148
|
+
tag(:charset, Rails.configuration.encoding, order: 0)
|
92
149
|
end
|
93
150
|
|
94
|
-
def compute_default_keywords
|
151
|
+
private def compute_default_keywords
|
152
|
+
return if has?(:keywords)
|
153
|
+
|
95
154
|
tag(:keywords, keywords.to_s) unless keywords.to_s.blank?
|
96
155
|
end
|
97
156
|
|
98
|
-
def compute_default_description
|
157
|
+
private def compute_default_description
|
158
|
+
return if has?(:description)
|
159
|
+
|
99
160
|
tag(:description, description.to_s) unless description.to_s.blank?
|
100
161
|
end
|
101
162
|
|
102
|
-
def compute_default_viewport
|
103
|
-
|
163
|
+
private def compute_default_viewport
|
164
|
+
return if has?(:viewport)
|
165
|
+
|
166
|
+
tag(:viewport, "width=device-width,initial-scale=1", order: 1)
|
167
|
+
end
|
168
|
+
|
169
|
+
private def has?(name)
|
170
|
+
items.any? { _1[:name] == name }
|
104
171
|
end
|
105
172
|
end
|
106
173
|
end
|
@@ -4,8 +4,15 @@ module PageMeta
|
|
4
4
|
class MetaTag
|
5
5
|
class DnsPrefetchControl < MetaTag
|
6
6
|
def render
|
7
|
-
helpers.tag(
|
8
|
-
|
7
|
+
meta = helpers.tag(
|
8
|
+
:meta,
|
9
|
+
"http-equiv" => "x-dns-prefetch-control",
|
10
|
+
content: "on"
|
11
|
+
)
|
12
|
+
|
13
|
+
link = helpers.tag(:link, rel: "dns-prefetch", href: content)
|
14
|
+
|
15
|
+
meta + link
|
9
16
|
end
|
10
17
|
end
|
11
18
|
end
|
@@ -4,8 +4,7 @@ module PageMeta
|
|
4
4
|
class MetaTag
|
5
5
|
class Title < MetaTag
|
6
6
|
def render
|
7
|
-
helpers.
|
8
|
-
helpers.tag(:meta, name: "DC.title", content: content.simple) +
|
7
|
+
helpers.tag(:meta, name: "DC.title", content: content.simple) +
|
9
8
|
helpers.tag(:meta, itemprop: "name", content: content.simple)
|
10
9
|
end
|
11
10
|
end
|
data/lib/page_meta/meta_tag.rb
CHANGED
@@ -20,8 +20,11 @@ module PageMeta
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def content
|
23
|
-
@content ||=
|
24
|
-
|
23
|
+
@content ||= if @raw_content.respond_to?(:call)
|
24
|
+
@raw_content.call
|
25
|
+
else
|
26
|
+
@raw_content
|
27
|
+
end
|
25
28
|
end
|
26
29
|
|
27
30
|
def render
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PageMeta
|
4
|
+
class OpenTag
|
5
|
+
def self.build(tag, attrs)
|
6
|
+
new(tag, attrs)
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :tag, :attrs
|
10
|
+
|
11
|
+
def initialize(tag, attrs)
|
12
|
+
@tag = tag
|
13
|
+
@attrs = attrs
|
14
|
+
end
|
15
|
+
|
16
|
+
def render
|
17
|
+
helpers.tag(tag, attrs)
|
18
|
+
end
|
19
|
+
|
20
|
+
def helpers
|
21
|
+
ActionController::Base.helpers
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PageMeta
|
4
|
+
class Tag
|
5
|
+
def self.build(tag, content)
|
6
|
+
new(tag, content)
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :tag, :content
|
10
|
+
|
11
|
+
def initialize(tag, content)
|
12
|
+
@tag = tag
|
13
|
+
@content = content
|
14
|
+
end
|
15
|
+
|
16
|
+
def render
|
17
|
+
helpers.content_tag(tag, content) unless content.blank?
|
18
|
+
end
|
19
|
+
|
20
|
+
def helpers
|
21
|
+
ActionController::Base.helpers
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/page_meta/translator.rb
CHANGED
@@ -14,15 +14,22 @@ module PageMeta
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def singular_scope
|
17
|
-
@singular_scope ||= scope == :keywords
|
17
|
+
@singular_scope ||= if scope == :keywords
|
18
|
+
"keywords"
|
19
|
+
else
|
20
|
+
scope.to_s.singularize
|
21
|
+
end
|
18
22
|
end
|
19
23
|
|
20
24
|
def to_s
|
21
25
|
return "" if simple.blank?
|
22
26
|
|
27
|
+
value = simple
|
28
|
+
|
23
29
|
[
|
24
|
-
t("page_meta.#{scope}.base", value
|
25
|
-
t("page_meta.#{singular_scope}_base", value
|
30
|
+
t("page_meta.#{scope}.base", value:, **override_options),
|
31
|
+
t("page_meta.#{singular_scope}_base", value:, **override_options,
|
32
|
+
default: value)
|
26
33
|
].reject(&:blank?).first || ""
|
27
34
|
end
|
28
35
|
|
@@ -33,9 +40,11 @@ module PageMeta
|
|
33
40
|
]
|
34
41
|
end
|
35
42
|
|
36
|
-
def
|
37
|
-
|
43
|
+
def override_options
|
44
|
+
options.merge(default: "")
|
45
|
+
end
|
38
46
|
|
47
|
+
def simple
|
39
48
|
translation = ""
|
40
49
|
|
41
50
|
translation_scope.each do |scope|
|
data/lib/page_meta/version.rb
CHANGED
data/lib/page_meta.rb
CHANGED
@@ -16,6 +16,8 @@ module PageMeta
|
|
16
16
|
require "page_meta/meta_tag/twitter"
|
17
17
|
require "page_meta/link"
|
18
18
|
require "page_meta/helpers"
|
19
|
+
require "page_meta/tag"
|
20
|
+
require "page_meta/open_tag"
|
19
21
|
require "page_meta/action"
|
20
22
|
require "page_meta/naming"
|
21
23
|
require "page_meta/translator"
|
data/page_meta.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.version = PageMeta::VERSION
|
8
8
|
spec.authors = ["Nando Vieira"]
|
9
9
|
spec.email = ["me@fnando.com"]
|
10
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 3.
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 3.2.0")
|
11
11
|
spec.metadata = {
|
12
12
|
"rubygems_mfa_required" => "true"
|
13
13
|
}
|
@@ -29,7 +29,6 @@ Gem::Specification.new do |spec|
|
|
29
29
|
|
30
30
|
spec.add_development_dependency "bundler"
|
31
31
|
spec.add_development_dependency "minitest-utils"
|
32
|
-
spec.add_development_dependency "pry-meta"
|
33
32
|
spec.add_development_dependency "rake"
|
34
33
|
spec.add_development_dependency "rubocop"
|
35
34
|
spec.add_development_dependency "rubocop-fnando"
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: page_meta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date: 2024-
|
10
|
+
date: 2024-12-29 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: rails
|
@@ -52,20 +51,6 @@ dependencies:
|
|
52
51
|
- - ">="
|
53
52
|
- !ruby/object:Gem::Version
|
54
53
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: pry-meta
|
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
54
|
- !ruby/object:Gem::Dependency
|
70
55
|
name: rake
|
71
56
|
requirement: !ruby/object:Gem::Requirement
|
@@ -148,9 +133,8 @@ files:
|
|
148
133
|
- Rakefile
|
149
134
|
- bin/console
|
150
135
|
- bin/setup
|
151
|
-
- gemfiles/6_1.gemfile
|
152
|
-
- gemfiles/7_0.gemfile
|
153
136
|
- gemfiles/7_1.gemfile
|
137
|
+
- gemfiles/7_2.gemfile
|
154
138
|
- lib/page_meta.rb
|
155
139
|
- lib/page_meta/action.rb
|
156
140
|
- lib/page_meta/base.rb
|
@@ -165,7 +149,9 @@ files:
|
|
165
149
|
- lib/page_meta/meta_tag/title.rb
|
166
150
|
- lib/page_meta/meta_tag/twitter.rb
|
167
151
|
- lib/page_meta/naming.rb
|
152
|
+
- lib/page_meta/open_tag.rb
|
168
153
|
- lib/page_meta/railtie.rb
|
154
|
+
- lib/page_meta/tag.rb
|
169
155
|
- lib/page_meta/translator.rb
|
170
156
|
- lib/page_meta/version.rb
|
171
157
|
- page_meta.gemspec
|
@@ -174,7 +160,6 @@ licenses:
|
|
174
160
|
- MIT
|
175
161
|
metadata:
|
176
162
|
rubygems_mfa_required: 'true'
|
177
|
-
post_install_message:
|
178
163
|
rdoc_options: []
|
179
164
|
require_paths:
|
180
165
|
- lib
|
@@ -182,15 +167,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
182
167
|
requirements:
|
183
168
|
- - ">="
|
184
169
|
- !ruby/object:Gem::Version
|
185
|
-
version: 3.
|
170
|
+
version: 3.2.0
|
186
171
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
187
172
|
requirements:
|
188
173
|
- - ">="
|
189
174
|
- !ruby/object:Gem::Version
|
190
175
|
version: '0'
|
191
176
|
requirements: []
|
192
|
-
rubygems_version: 3.
|
193
|
-
signing_key:
|
177
|
+
rubygems_version: 3.6.2
|
194
178
|
specification_version: 4
|
195
179
|
summary: Easily define <meta> and <link> tags. I18n support for descriptions, keywords
|
196
180
|
and titles.
|