google-webfonts 0.2.0 → 0.3.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.
- checksums.yaml +5 -5
- data/.gitignore +13 -18
- data/Gemfile +5 -1
- data/Gemfile.lock +41 -0
- data/LICENSE +2 -2
- data/README.md +77 -51
- data/Rakefile +6 -3
- data/bin/console +7 -0
- data/bin/setup +7 -0
- data/google-webfonts.gemspec +23 -19
- data/lib/google-webfonts.rb +8 -20
- data/lib/google-webfonts/sinatra.rb +7 -3
- data/lib/google/webfonts.rb +20 -0
- data/lib/google/webfonts/helper.rb +32 -0
- data/lib/google/webfonts/link_tag.rb +153 -0
- data/lib/google/webfonts/rails.rb +8 -0
- data/lib/google/webfonts/sinatra.rb +8 -0
- data/lib/google/webfonts/version.rb +9 -0
- metadata +59 -56
- data/lib/google-webfonts/helper.rb +0 -44
- data/lib/google-webfonts/link_tag.rb +0 -68
- data/lib/google-webfonts/version.rb +0 -6
- data/test/lib/google-webfonts/helper_test.rb +0 -21
- data/test/lib/google-webfonts/link_tag_test.rb +0 -52
- data/test/lib/google-webfonts/sinatra_test.rb +0 -25
- data/test/lib/google-webfonts_test.rb +0 -11
- data/test/test_helper.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d67f80b14813326601c41f9f2865fdad8caa0b9d16b427a6eb1d20a0cd8ed89a
|
4
|
+
data.tar.gz: 0c20a66e55787843e8f5ddcc7d3c171eb604400fe07a464803008e42fcb7713f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99976493264cffc8728c444d8dc8791bc8f3422733287a61c05d27bece777948fbec023167642cd7314fd438572110d52dc505d63e2d39f5448e1a2c963a9af1
|
7
|
+
data.tar.gz: d403b86434a2b6020c3440b9149588e1edcedbd64f3763e2568ac7a28d3375867e9dc420605835981f0428e0ac3598d042fa05d23c06880fc114fe1de239c72e
|
data/.gitignore
CHANGED
@@ -1,18 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
spec/reports
|
15
|
-
test/tmp
|
16
|
-
test/version_tmp
|
17
|
-
tmp
|
18
|
-
.ruby-version
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
/.idea/
|
10
|
+
/.code/
|
11
|
+
.DS_Store
|
12
|
+
/.ruby-gemset
|
13
|
+
/.ruby-version
|
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
google-webfonts (0.3.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
builder (3.2.3)
|
11
|
+
docile (1.3.0)
|
12
|
+
json (2.1.0)
|
13
|
+
minitest (5.3.5)
|
14
|
+
minitest-reporters (1.2.0)
|
15
|
+
ansi
|
16
|
+
builder
|
17
|
+
minitest (>= 5.0)
|
18
|
+
ruby-progressbar
|
19
|
+
rake (10.5.0)
|
20
|
+
ruby-progressbar (1.9.0)
|
21
|
+
simplecov (0.16.1)
|
22
|
+
docile (~> 1.1)
|
23
|
+
json (>= 1.8, < 3)
|
24
|
+
simplecov-html (~> 0.10.0)
|
25
|
+
simplecov-html (0.10.2)
|
26
|
+
yard (0.9.12)
|
27
|
+
|
28
|
+
PLATFORMS
|
29
|
+
ruby
|
30
|
+
|
31
|
+
DEPENDENCIES
|
32
|
+
bundler (~> 1.16)
|
33
|
+
google-webfonts!
|
34
|
+
minitest (~> 5.0)
|
35
|
+
minitest-reporters (~> 1.2)
|
36
|
+
rake (~> 10.0)
|
37
|
+
simplecov (~> 0.16)
|
38
|
+
yard (~> 0.9)
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
1.16.1
|
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2018 Hi5 Development, LLC
|
2
2
|
|
3
3
|
MIT License
|
4
4
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
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.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Google::Webfonts
|
2
2
|
|
3
|
-
Provides a helper for using [Google Webfonts](http://www.google.com/webfonts)
|
4
|
-
|
3
|
+
Provides a helper for using [Google Webfonts](http://www.google.com/webfonts)
|
4
|
+
with the Ruby programming language.
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
@@ -19,77 +19,103 @@ Or install it yourself as:
|
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
22
|
-
|
22
|
+
```ruby
|
23
|
+
include Google::Webfonts::Helper
|
23
24
|
|
24
|
-
|
25
|
+
google_webfonts_link_tag font_name: [weights]
|
26
|
+
```
|
25
27
|
|
26
28
|
### Examples
|
27
29
|
|
28
|
-
Basic usage:
|
30
|
+
#### Basic usage:
|
29
31
|
|
30
|
-
|
31
|
-
|
32
|
+
```ruby
|
33
|
+
google_webfonts_link_tag droid_sans: [400, 700], yanone_kaffeesatz: [300, 400]
|
34
|
+
```
|
32
35
|
|
33
|
-
The
|
34
|
-
including one
|
36
|
+
The weights are optional, and do not have to be in an Array if you are only
|
37
|
+
including one. For example:
|
35
38
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
google_webfonts_link_tag :droid_sans => 400
|
40
|
-
# => generates a tag for Droid+Sans with 400 weight
|
41
|
-
|
42
|
-
google_webfonts_link_tag :droid_sans => [400, 700]
|
43
|
-
# => generates a tag for Droid+Sans with 400 and 700 weights
|
39
|
+
```ruby
|
40
|
+
# Generate a tag for Droid+Sans without specifying the font weight.
|
41
|
+
google_webfonts_link_tag :droid_sans
|
44
42
|
|
45
|
-
|
43
|
+
# Generate a tag for Droid+Sans with 400 weight.
|
44
|
+
google_webfonts_link_tag :droid_sans => 400
|
46
45
|
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
# Generate a tag for Droid+Sans with 400 and 700 weights.
|
47
|
+
google_webfonts_link_tag :droid_sans => [400, 700]
|
48
|
+
```
|
49
|
+
|
50
|
+
In certain cases it will be necessary to use Strings instead of Symbols for the
|
51
|
+
font names. The helper method titleizes the symbols when converting it to a
|
52
|
+
Google Webfonts compatible name. This does not work for every font name. For
|
53
|
+
example:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
# If a Symbol was provided here, the "T" would be lower-case, which would
|
57
|
+
# result in an invalid link.
|
58
|
+
google_webfonts_link_tag 'PT Sans'
|
59
|
+
```
|
50
60
|
|
51
61
|
### Using in Rails
|
52
62
|
|
53
|
-
|
54
|
-
|
55
|
-
|
63
|
+
Require the gem like this in your Gemfile when using with Rails:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
gem 'google-webfonts', require: 'google/webfonts/rails'
|
67
|
+
```
|
56
68
|
|
57
|
-
|
58
|
-
it outside of
|
69
|
+
The `google_webfonts_link_tag` helper method will automatically be available
|
70
|
+
to your views. If you need to use it outside of the views, include this module:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
include Google::Webfonts::Helper
|
74
|
+
```
|
59
75
|
|
60
76
|
### Using in Sinatra
|
61
77
|
|
62
78
|
Here is a simple "Hello World" example for using Google::Webfonts in a Sinatra
|
63
79
|
app:
|
64
80
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
81
|
+
```ruby
|
82
|
+
# app.rb
|
83
|
+
require 'rubygems'
|
84
|
+
require 'sinatra'
|
85
|
+
|
86
|
+
# This must be required after requiring sinatra.
|
87
|
+
require 'google/webfonts/sinatra'
|
88
|
+
|
89
|
+
get '/' do
|
90
|
+
erb :index
|
91
|
+
end
|
92
|
+
```
|
73
93
|
|
74
94
|
###
|
75
95
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
96
|
+
```erb
|
97
|
+
# views/index.erb
|
98
|
+
<html>
|
99
|
+
<head>
|
100
|
+
<%= google_webfonts_link_tag "Droid Sans" %>
|
101
|
+
</head>
|
102
|
+
<body>
|
103
|
+
<p style="font-family: 'Droid+Sans', sans-serif;">
|
104
|
+
Hello World!
|
105
|
+
</p>
|
106
|
+
</body>
|
107
|
+
</html>
|
108
|
+
```
|
109
|
+
|
110
|
+
## Development
|
111
|
+
|
112
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then,
|
113
|
+
run `rake test` to run the tests. You can also run `bin/console` for an
|
114
|
+
interactive prompt that will allow you to experiment.
|
115
|
+
|
116
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
87
117
|
|
88
118
|
## Contributing
|
89
119
|
|
90
|
-
|
91
|
-
|
92
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
93
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
94
|
-
5. Ensure what your code is well tested, and all the tests pass. (`rspec spec`)
|
95
|
-
6. Create new Pull Request
|
120
|
+
Bug reports and pull requests are welcome on GitHub at
|
121
|
+
https://github.com/hi5dev/google-webfonts.
|
data/Rakefile
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/gem_tasks'
|
2
5
|
require 'rake/testtask'
|
3
6
|
|
4
7
|
Rake::TestTask.new do |t|
|
5
|
-
t.libs
|
8
|
+
t.libs += %w[lib test]
|
6
9
|
t.test_files = FileList['test/**/*_test.rb']
|
7
10
|
end
|
8
11
|
|
9
|
-
task :
|
12
|
+
task default: :test
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/google-webfonts.gemspec
CHANGED
@@ -1,30 +1,34 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('lib', __dir__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
|
6
|
+
|
7
|
+
require 'google/webfonts/version'
|
5
8
|
|
6
9
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
10
|
+
spec.name = 'google-webfonts'
|
8
11
|
spec.version = Google::Webfonts::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
|
12
|
-
spec.summary =
|
13
|
-
"Sinatra, although it can be used outside of those " +
|
14
|
-
"frameworks as well."
|
12
|
+
spec.authors = ['Travis Haynes']
|
13
|
+
spec.email = ['travis@hi5dev.com']
|
14
|
+
spec.homepage = 'https://github.com/hi5dev/google-webfonts.git'
|
15
|
+
spec.summary = 'Simplifies creating link tags for Google Webfonts.'
|
15
16
|
|
16
|
-
spec.
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |file|
|
18
|
+
file.match(%r{^(test)/})
|
19
|
+
end
|
17
20
|
|
18
|
-
spec.
|
19
|
-
spec.files = `git ls-files -z`.split("\x0")
|
20
|
-
spec.executables = spec.files.grep(%r{^bin/}) {|f| File.basename(f) }
|
21
|
-
spec.test_files = spec.files.grep(%r{^test/})
|
21
|
+
spec.bindir = 'exe'
|
22
22
|
spec.require_paths = ['lib']
|
23
23
|
|
24
|
-
spec.
|
24
|
+
spec.executables = spec.files.grep(%r{^#{spec.bindir}/}) do |file|
|
25
|
+
File.basename(file)
|
26
|
+
end
|
25
27
|
|
26
|
-
spec.add_development_dependency 'bundler', '
|
27
|
-
spec.add_development_dependency '
|
28
|
-
spec.add_development_dependency 'minitest', '~>
|
29
|
-
spec.add_development_dependency '
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
29
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
30
|
+
spec.add_development_dependency 'minitest-reporters', '~> 1.2'
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
|
+
spec.add_development_dependency 'simplecov', '~> 0.16'
|
33
|
+
spec.add_development_dependency 'yard', '~> 0.9'
|
30
34
|
end
|
data/lib/google-webfonts.rb
CHANGED
@@ -1,22 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'active_support/core_ext'
|
4
|
-
require 'action_dispatch/http/mime_type'
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
PATH = '/css'
|
12
|
-
end
|
13
|
-
end
|
4
|
+
warn <<~TEXT
|
5
|
+
DEPRECATED: Requiring 'google-webfonts' has been deprecated since version
|
6
|
+
0.3.0, and will be removed in a future version. Require 'google/webfonts'
|
7
|
+
instead.
|
8
|
+
TEXT
|
14
9
|
|
15
|
-
require 'google
|
16
|
-
require 'google-webfonts/link_tag'
|
17
|
-
require 'google-webfonts/helper'
|
18
|
-
|
19
|
-
require 'google-webfonts/sinatra' if defined? Sinatra
|
20
|
-
|
21
|
-
# include the webfonts helper methods in the Rails view helpers
|
22
|
-
ActionView::Base.send :include, Google::Webfonts::Helper
|
10
|
+
require 'google/webfonts'
|
@@ -1,3 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
warn <<~TEXT
|
2
|
+
DEPRECATED: Requiring 'google-webfonts/sinatra' has been deprecated since
|
3
|
+
version 0.3.0, and will be removed in a future version. Require
|
4
|
+
'google/webfonts/sinatra' instead.
|
5
|
+
TEXT
|
6
|
+
|
7
|
+
require 'google/webfonts/sinatra'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'cgi'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
module Google
|
8
|
+
module Webfonts
|
9
|
+
# The host name for the Google Webfonts CDN.
|
10
|
+
CDN_HOST = 'https://fonts.googleapis.com'
|
11
|
+
|
12
|
+
# The path name for the Google Webfonts on the CDN.
|
13
|
+
CDN_PATH = 'css'
|
14
|
+
|
15
|
+
# Auto load all of the subclasses.
|
16
|
+
autoload :Helper, 'google/webfonts/helper'
|
17
|
+
autoload :LinkTag, 'google/webfonts/link_tag'
|
18
|
+
autoload :VERSION, 'google/webfonts/version'
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Google
|
2
|
+
module Webfonts
|
3
|
+
# Provides a helper method for creating HTML link tags for fonts hosted
|
4
|
+
# on Google's Webfonts CDN.
|
5
|
+
module Helper
|
6
|
+
# Generates a Google Webfonts link tag.
|
7
|
+
#
|
8
|
+
# @example Fonts without options.
|
9
|
+
# google_webfonts_link_tag :droid_sans, :open_sans
|
10
|
+
#
|
11
|
+
# @example Strings can be used for font names.
|
12
|
+
# # :pt_sans would be converted to 'Pt+Sans' (lower-case t), which is
|
13
|
+
# # an invalid font name. Strings do not get titleized, so this font
|
14
|
+
# # name would require a string instead of a Symbol.
|
15
|
+
# google_webfonts_link_tag 'PT Sans'
|
16
|
+
#
|
17
|
+
# @example Specifying font weights.
|
18
|
+
# google_webfonts_link_tag droid_sans: %w[400 500 bold bolditalic]
|
19
|
+
#
|
20
|
+
# @param [Array<String, Symbol, Hash{String, Symbol => Array<String>}>]
|
21
|
+
# options Options for the fonts.
|
22
|
+
# @return [String] HTML with the <link> tag.
|
23
|
+
def google_webfonts_link_tag(*options)
|
24
|
+
# Do not create the link tag unless at least one font was given.
|
25
|
+
fail ArgumentError, 'expected at least one font' unless options.any?
|
26
|
+
|
27
|
+
# Create the HTML for the link tag.
|
28
|
+
Google::Webfonts::LinkTag.new(*options).to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Google
|
5
|
+
module Webfonts
|
6
|
+
# Used to simplify creating <link> tags for Google's Webfonts CDN.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# tag = Google::Webfonts::LinkTag.new(:droid_sans => %w[400 500i])
|
10
|
+
# html = tag.to_s
|
11
|
+
class LinkTag
|
12
|
+
attr_reader :options
|
13
|
+
|
14
|
+
# Creates a new {Google::Webfonts::LinkTag} instance.
|
15
|
+
#
|
16
|
+
# @return [Google::Webfonts::LinkTag] The new {LinkTag} instance.
|
17
|
+
def initialize(*options)
|
18
|
+
@options = parse_options(options).freeze
|
19
|
+
end
|
20
|
+
|
21
|
+
# When making headers or display texts on your website, you'll often
|
22
|
+
# want to stylize your text in a decorative way. To simplify your work,
|
23
|
+
# Google has provided a collection of font effects that you can use
|
24
|
+
# with minimal effort to produce beautiful display text.
|
25
|
+
#
|
26
|
+
# To use feature, simply add an +effect:+ option when initializing the
|
27
|
+
# link tag. Then add the corresponding class name to the HTML element(s)
|
28
|
+
# that you want to affect.
|
29
|
+
#
|
30
|
+
# At the time that version 0.3.0 of this gem was released, this was a
|
31
|
+
# beta feature provided by Google.
|
32
|
+
#
|
33
|
+
# @see https://developers.google.com/fonts/docs/getting_started
|
34
|
+
# @return [Array<String>] Any effects to applied to the font.
|
35
|
+
def effect
|
36
|
+
options[:effect]
|
37
|
+
end
|
38
|
+
|
39
|
+
# Parses the options provided to the initializer into a list of font
|
40
|
+
# families.
|
41
|
+
#
|
42
|
+
# @return [String] The font family formatted for Google Webfont's API.
|
43
|
+
def family
|
44
|
+
@family ||= family_options.map do |font_name, font_options|
|
45
|
+
name = parse_font_name(font_name)
|
46
|
+
|
47
|
+
options = font_options.sort.uniq
|
48
|
+
|
49
|
+
options.any? ? "#{name}:#{options.join(',')}" : name
|
50
|
+
end.join('|')
|
51
|
+
end
|
52
|
+
|
53
|
+
# Creates the hypertext reference (URL) to the font.
|
54
|
+
#
|
55
|
+
# @return [URI] URL to the font on Google's CDN.
|
56
|
+
def href
|
57
|
+
# Return the cached value when present.
|
58
|
+
return @href unless @href.nil?
|
59
|
+
|
60
|
+
# Create a URI to Google's CDN.
|
61
|
+
uri = URI.join(Google::Webfonts::CDN_HOST, Google::Webfonts::CDN_PATH)
|
62
|
+
|
63
|
+
uri.query = "family=#{family}"
|
64
|
+
|
65
|
+
uri.query += "&text=#{CGI.escape(text.join)}" unless text.nil?
|
66
|
+
|
67
|
+
uri.query += "&effect=#{CGI.escape(effect.join(','))}" unless effect.nil?
|
68
|
+
|
69
|
+
@href = uri
|
70
|
+
end
|
71
|
+
|
72
|
+
# Oftentimes, when you want to use a web font on your website or
|
73
|
+
# application, you know in advance which letters you'll need. This
|
74
|
+
# often occurs when you're using a web font in a logo or heading.
|
75
|
+
#
|
76
|
+
# In these cases, you should consider specifying a text= value in your
|
77
|
+
# font request URL. This allows Google to return a font file that's
|
78
|
+
# optimized for your request. In some cases, this can reduce the size
|
79
|
+
# of the font file by up to 90%.
|
80
|
+
#
|
81
|
+
# At the time that version 0.3.0 of this gem was released, this was a
|
82
|
+
# beta feature provided by Google.
|
83
|
+
#
|
84
|
+
# @see https://developers.google.com/fonts/docs/getting_started
|
85
|
+
# @return [Array<String>] Which letters to include in the font.
|
86
|
+
def text
|
87
|
+
options[:text]
|
88
|
+
end
|
89
|
+
|
90
|
+
# Converts the link tag to HTML.
|
91
|
+
#
|
92
|
+
# @return [String] HTML for the link tag.
|
93
|
+
def to_s
|
94
|
+
%(<link href="#{href}" rel="stylesheet">)
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
# Extracts the options for the font families.
|
100
|
+
#
|
101
|
+
# @return [Hash{String, Symbol => Array<Integer, String>}] The families.
|
102
|
+
def family_options
|
103
|
+
options.reject {|k,_| %i[effect text].include?(k) }
|
104
|
+
end
|
105
|
+
|
106
|
+
# Converts the Ruby-friendly font name provided to the initializer to a
|
107
|
+
# URL-encoded font name that Google's CDN will recognize.
|
108
|
+
#
|
109
|
+
# @param [String, Symbol] font_name The given font name.
|
110
|
+
# @return [String] A URI-encoded key name.
|
111
|
+
def parse_font_name(font_name)
|
112
|
+
# Strings only get URI-encoded.
|
113
|
+
return CGI.escape(font_name) if font_name.is_a?(String)
|
114
|
+
|
115
|
+
# Convert the font name into an array of its words.
|
116
|
+
words = "#{font_name}".split('_')
|
117
|
+
|
118
|
+
# Capitalize each word in the font name.
|
119
|
+
words.map!(&:capitalize)
|
120
|
+
|
121
|
+
# Convert back to a String, joined by spaces instead of underscores.
|
122
|
+
font_name = words.join(' ')
|
123
|
+
|
124
|
+
# URI-encode the font name.
|
125
|
+
CGI.escape(font_name)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Parses the options given to the initializer into a Hash.
|
129
|
+
#
|
130
|
+
# @return [Hash{String, Symbol => Array}] Options for the LinkTag.
|
131
|
+
def parse_options(options)
|
132
|
+
{}.tap do |parsed|
|
133
|
+
options.each { |opt| parse_option(opt, parsed) }
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# @param [Hash, String, Symbol] opt
|
138
|
+
# @param [Hash] parsed
|
139
|
+
# @return [Array<String, Array>]
|
140
|
+
# @raise [ArgumentError] If an option cannot be parsed.
|
141
|
+
def parse_option(opt, parsed)
|
142
|
+
# Font names without options are converted to names with empty options.
|
143
|
+
return parsed[opt] = [] if opt.is_a?(String) || opt.is_a?(Symbol)
|
144
|
+
|
145
|
+
# Add any option in a Hash, ensuring that its value is an Array.
|
146
|
+
return opt.each { |k, v| parsed[k] = Array(v) } if opt.is_a?(Hash)
|
147
|
+
|
148
|
+
# Reject unknown arguments.
|
149
|
+
fail ArgumentError, "invalid argument: #{opt.inspect}", caller
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
metadata
CHANGED
@@ -1,115 +1,124 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-webfonts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Haynes
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
type: :
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '1.16'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: minitest
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
- - <
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version: '2.0'
|
33
|
+
version: '5.0'
|
37
34
|
type: :development
|
38
35
|
prerelease: false
|
39
36
|
version_requirements: !ruby/object:Gem::Requirement
|
40
37
|
requirements:
|
41
|
-
- -
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest-reporters
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
42
46
|
- !ruby/object:Gem::Version
|
43
|
-
version: 1.
|
44
|
-
|
47
|
+
version: '1.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
45
53
|
- !ruby/object:Gem::Version
|
46
|
-
version: '2
|
54
|
+
version: '1.2'
|
47
55
|
- !ruby/object:Gem::Dependency
|
48
56
|
name: rake
|
49
57
|
requirement: !ruby/object:Gem::Requirement
|
50
58
|
requirements:
|
51
|
-
- -
|
59
|
+
- - "~>"
|
52
60
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
61
|
+
version: '10.0'
|
54
62
|
type: :development
|
55
63
|
prerelease: false
|
56
64
|
version_requirements: !ruby/object:Gem::Requirement
|
57
65
|
requirements:
|
58
|
-
- -
|
66
|
+
- - "~>"
|
59
67
|
- !ruby/object:Gem::Version
|
60
|
-
version: '0'
|
68
|
+
version: '10.0'
|
61
69
|
- !ruby/object:Gem::Dependency
|
62
|
-
name:
|
70
|
+
name: simplecov
|
63
71
|
requirement: !ruby/object:Gem::Requirement
|
64
72
|
requirements:
|
65
|
-
- - ~>
|
73
|
+
- - "~>"
|
66
74
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
75
|
+
version: '0.16'
|
68
76
|
type: :development
|
69
77
|
prerelease: false
|
70
78
|
version_requirements: !ruby/object:Gem::Requirement
|
71
79
|
requirements:
|
72
|
-
- - ~>
|
80
|
+
- - "~>"
|
73
81
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
82
|
+
version: '0.16'
|
75
83
|
- !ruby/object:Gem::Dependency
|
76
|
-
name:
|
84
|
+
name: yard
|
77
85
|
requirement: !ruby/object:Gem::Requirement
|
78
86
|
requirements:
|
79
|
-
- - ~>
|
87
|
+
- - "~>"
|
80
88
|
- !ruby/object:Gem::Version
|
81
|
-
version:
|
89
|
+
version: '0.9'
|
82
90
|
type: :development
|
83
91
|
prerelease: false
|
84
92
|
version_requirements: !ruby/object:Gem::Requirement
|
85
93
|
requirements:
|
86
|
-
- - ~>
|
94
|
+
- - "~>"
|
87
95
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
89
|
-
description:
|
96
|
+
version: '0.9'
|
97
|
+
description:
|
90
98
|
email:
|
91
|
-
- travis
|
99
|
+
- travis@hi5dev.com
|
92
100
|
executables: []
|
93
101
|
extensions: []
|
94
102
|
extra_rdoc_files: []
|
95
103
|
files:
|
96
|
-
- .gitignore
|
104
|
+
- ".gitignore"
|
97
105
|
- Gemfile
|
106
|
+
- Gemfile.lock
|
98
107
|
- LICENSE
|
99
108
|
- README.md
|
100
109
|
- Rakefile
|
110
|
+
- bin/console
|
111
|
+
- bin/setup
|
101
112
|
- google-webfonts.gemspec
|
102
113
|
- lib/google-webfonts.rb
|
103
|
-
- lib/google-webfonts/helper.rb
|
104
|
-
- lib/google-webfonts/link_tag.rb
|
105
114
|
- lib/google-webfonts/sinatra.rb
|
106
|
-
- lib/google
|
107
|
-
-
|
108
|
-
-
|
109
|
-
-
|
110
|
-
-
|
111
|
-
-
|
112
|
-
homepage: https://github.com/
|
115
|
+
- lib/google/webfonts.rb
|
116
|
+
- lib/google/webfonts/helper.rb
|
117
|
+
- lib/google/webfonts/link_tag.rb
|
118
|
+
- lib/google/webfonts/rails.rb
|
119
|
+
- lib/google/webfonts/sinatra.rb
|
120
|
+
- lib/google/webfonts/version.rb
|
121
|
+
homepage: https://github.com/hi5dev/google-webfonts.git
|
113
122
|
licenses: []
|
114
123
|
metadata: {}
|
115
124
|
post_install_message:
|
@@ -118,24 +127,18 @@ require_paths:
|
|
118
127
|
- lib
|
119
128
|
required_ruby_version: !ruby/object:Gem::Requirement
|
120
129
|
requirements:
|
121
|
-
- -
|
130
|
+
- - ">="
|
122
131
|
- !ruby/object:Gem::Version
|
123
132
|
version: '0'
|
124
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
134
|
requirements:
|
126
|
-
- -
|
135
|
+
- - ">="
|
127
136
|
- !ruby/object:Gem::Version
|
128
137
|
version: '0'
|
129
138
|
requirements: []
|
130
139
|
rubyforge_project:
|
131
|
-
rubygems_version: 2.
|
140
|
+
rubygems_version: 2.7.3
|
132
141
|
signing_key:
|
133
142
|
specification_version: 4
|
134
|
-
summary:
|
135
|
-
|
136
|
-
test_files:
|
137
|
-
- test/lib/google-webfonts/helper_test.rb
|
138
|
-
- test/lib/google-webfonts/link_tag_test.rb
|
139
|
-
- test/lib/google-webfonts/sinatra_test.rb
|
140
|
-
- test/lib/google-webfonts_test.rb
|
141
|
-
- test/test_helper.rb
|
143
|
+
summary: Simplifies creating link tags for Google Webfonts.
|
144
|
+
test_files: []
|
@@ -1,44 +0,0 @@
|
|
1
|
-
module Google
|
2
|
-
module Webfonts
|
3
|
-
|
4
|
-
# Public: Helper module that includes the google_webfonts_link_tag method.
|
5
|
-
# This module is automatically included in your Rails view helpers.
|
6
|
-
module Helper
|
7
|
-
# Public: Generates a Google Webfonts link tag
|
8
|
-
#
|
9
|
-
# If you need to specify a font size, use a Hash. Symbols will be
|
10
|
-
# titleized, so if the font you're using uses a specialized case
|
11
|
-
# (e.g: "PT Sans"), then you'll need to use a String.
|
12
|
-
#
|
13
|
-
# You can specify subsets by using the :subset option. Note that this must
|
14
|
-
# be a Symbol. "Subset" and "subset" would be turned into a font.
|
15
|
-
#
|
16
|
-
# options - The font options. This can be a String, Symbol, or Hash, or
|
17
|
-
# a combination of all three.
|
18
|
-
#
|
19
|
-
# Examples
|
20
|
-
#
|
21
|
-
# google_webfonts_link_tag "PT Sans"
|
22
|
-
#
|
23
|
-
# google_webfonts_link_tag :droid_sans
|
24
|
-
#
|
25
|
-
# google_webfonts_link_tag droid_sans: [400, 700]
|
26
|
-
#
|
27
|
-
# google_webfonts_link_tag droid_sans: [400, 700],
|
28
|
-
# yanone_kaffeesatz: 700
|
29
|
-
#
|
30
|
-
# google_webfonts_link_tag :droid_sans,
|
31
|
-
# yanone_kaffeesatz: 400,
|
32
|
-
# "PT Sans" => [400, 700]
|
33
|
-
#
|
34
|
-
# google_webfonts_link_tag :droid_sans, subset: %w[latin cyrillic]
|
35
|
-
#
|
36
|
-
# Returns a <link> tag for the Google Webfonts stylesheet.
|
37
|
-
# Raises ArgumentError if no options are passed.
|
38
|
-
def google_webfonts_link_tag(*opts)
|
39
|
-
raise ArgumentError, 'expected at least one font', caller if opts.empty?
|
40
|
-
LinkTag.new(request, *opts).result
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
module Google
|
2
|
-
module Webfonts
|
3
|
-
class LinkTag
|
4
|
-
include ActionView::Helpers::TagHelper
|
5
|
-
|
6
|
-
attr_reader :result
|
7
|
-
|
8
|
-
alias_method :to_s, :result
|
9
|
-
|
10
|
-
def initialize(request, *opts)
|
11
|
-
@request = request
|
12
|
-
@subsets = []
|
13
|
-
@fonts = fonts_from_options(opts)
|
14
|
-
@result = tag(:link, link_options, false, false)
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def fonts_from_options(opts)
|
20
|
-
opts.map {|option| parse_google_webfont_option(option) }.flatten
|
21
|
-
end
|
22
|
-
|
23
|
-
def parse_google_webfont_option(option)
|
24
|
-
case option
|
25
|
-
when Symbol, String then parse_google_webfont_name(option)
|
26
|
-
when Hash then parse_google_webfont_hash(option)
|
27
|
-
else ""
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def parse_google_webfont_hash(hash)
|
32
|
-
add_subsets(hash.delete(:subset))
|
33
|
-
|
34
|
-
hash.inject([]) do |result, (font_name, sizes)|
|
35
|
-
font_name = parse_google_webfont_name(font_name)
|
36
|
-
result << "#{font_name}:#{Array(sizes).join(",")}"
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def parse_google_webfont_name(name)
|
41
|
-
name = name.to_s.titleize if name.is_a?(Symbol)
|
42
|
-
name.gsub("_", " ")
|
43
|
-
end
|
44
|
-
|
45
|
-
def link_options
|
46
|
-
{ rel: 'stylesheet',
|
47
|
-
type: Mime::CSS,
|
48
|
-
href: uri.to_s }
|
49
|
-
end
|
50
|
-
|
51
|
-
def add_subsets(subsets)
|
52
|
-
@subsets += Array(subsets)
|
53
|
-
end
|
54
|
-
|
55
|
-
def uri
|
56
|
-
u = @request.ssl? ? URI::HTTPS : URI::HTTP
|
57
|
-
u.build(host: HOST, path: PATH, query: uri_query)
|
58
|
-
end
|
59
|
-
|
60
|
-
def uri_query
|
61
|
-
{}.tap {|q|
|
62
|
-
q['family'] = @fonts.join("|")
|
63
|
-
q['subset'] = @subsets.join(",") if @subsets.any?
|
64
|
-
}.to_query
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Google
|
4
|
-
module Webfonts
|
5
|
-
class HelperTest < Minitest::Test
|
6
|
-
include Helper
|
7
|
-
|
8
|
-
def test_google_webfonts_link_tag
|
9
|
-
options = { droid_sans: [400, 700] }
|
10
|
-
link_tag = LinkTag.new(request, options)
|
11
|
-
assert_equal link_tag.result, google_webfonts_link_tag(options)
|
12
|
-
end
|
13
|
-
|
14
|
-
private
|
15
|
-
|
16
|
-
def request
|
17
|
-
OpenStruct.new(:ssl? => false)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Google
|
4
|
-
module Webfonts
|
5
|
-
class LinkTagTest < Minitest::Test
|
6
|
-
def setup
|
7
|
-
@http_request = OpenStruct.new(:ssl? => false)
|
8
|
-
@https_request = OpenStruct.new(:ssl? => true)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_subsets
|
12
|
-
tag = LinkTag.new(@http_request, droid_sans: [400, 700],
|
13
|
-
subset: %w[latin cyrillic]).result
|
14
|
-
assert_tag tag, 'link',
|
15
|
-
href: 'http://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700&subset=latin%2Ccyrillic'
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_uri_protocol
|
19
|
-
tag = LinkTag.new(@http_request, droid_sans: [400, 700]).result
|
20
|
-
assert_tag tag, 'link',
|
21
|
-
href: 'http://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700'
|
22
|
-
|
23
|
-
tag = LinkTag.new(@https_request, droid_sans: [400, 700]).result
|
24
|
-
assert_tag tag, 'link',
|
25
|
-
href: 'https://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700'
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_with_font_sizes
|
29
|
-
tag = LinkTag.new(@http_request, droid_sans: [400, 700]).result
|
30
|
-
assert_tag tag, 'link',
|
31
|
-
href: 'http://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700'
|
32
|
-
|
33
|
-
tag = LinkTag.new(@http_request, droid_sans: [400, 700],
|
34
|
-
'PT Sans' => [400, 700]).result
|
35
|
-
assert_tag tag, 'link',
|
36
|
-
href: 'http://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700%7CPT+Sans%3A400%2C700'
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_strings_are_not_titleized
|
40
|
-
tag = LinkTag.new(@http_request, 'PT Sans').result
|
41
|
-
assert_tag tag, 'link',
|
42
|
-
href: 'http://fonts.googleapis.com/css?family=PT+Sans'
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_symbols_are_titleized
|
46
|
-
tag = LinkTag.new(@http_request, :droid_sans).result
|
47
|
-
assert_tag tag, 'link',
|
48
|
-
href: 'http://fonts.googleapis.com/css?family=Droid+Sans'
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
# create a Sinatra route for testing
|
4
|
-
get("/") { google_webfonts_link_tag droid_sans: [400, 700] }
|
5
|
-
|
6
|
-
class SinatraTest < Minitest::Test
|
7
|
-
include Rack::Test::Methods
|
8
|
-
|
9
|
-
def app
|
10
|
-
Sinatra::Application
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_google_webfonts_link_tag_helper_loaded_in_sinatra
|
14
|
-
get "/"
|
15
|
-
options = { droid_sans: [400, 700] }
|
16
|
-
link_tag = Google::Webfonts::LinkTag.new(request, options)
|
17
|
-
assert_equal link_tag.result, last_response.body
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def request
|
23
|
-
OpenStruct.new(:ssl? => false)
|
24
|
-
end
|
25
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class Google::WebfontsTest < Minitest::Test
|
4
|
-
def test_helper_is_included_in_action_view_base
|
5
|
-
assert ActionView::Base.ancestors.include?(Google::Webfonts::Helper)
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_helper_is_included_in_sinatra
|
9
|
-
assert Sinatra::Application.ancestors.include?(Google::Webfonts::Helper)
|
10
|
-
end
|
11
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
ENV['RACK_ENV'] = 'test'
|
2
|
-
|
3
|
-
require 'ostruct'
|
4
|
-
require 'sinatra'
|
5
|
-
require 'google-webfonts'
|
6
|
-
require 'minitest/autorun'
|
7
|
-
require 'minitest/unit'
|
8
|
-
require 'rack/test'
|
9
|
-
|
10
|
-
class Minitest::Test
|
11
|
-
def assert_tag(el, tag, props={})
|
12
|
-
assert el =~ /<#{tag}.*\/>/ , "expected a #{tag} tag"
|
13
|
-
assert props.all? {|key, value| el.include?("#{key}=\"#{value}\"") },
|
14
|
-
"tag's properties do not match"
|
15
|
-
end
|
16
|
-
end
|