google_font_extractor 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.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # Introduction
2
+
3
+ The GoogleFontExtractor Gem will crawl the Google Webfonts directory on your command and return an array of Struct instances with details of fonts available in the Google Fonts directory.
4
+
5
+ PLEASE Note: this does NOT download the font files themselves.
6
+
7
+ Let's get started.
8
+
9
+ ## 1. In your Gemfile
10
+ gem "google_font_extractor"
11
+
12
+ ## 2. Bundle Install
13
+
14
+ ## 3. rails console example
15
+ ruby-1.9.2> fonts = GoogleFontExtractor.extract
16
+ ruby-1.9.2> fonts.first
17
+ => #<struct designer="Steve Matteson", name="Droid Serif", charsets=["latin"], weights_count="4", weights=["regular", "italic", "bold", "bolditalic"], families_count=nil, category="serif">
18
+ ruby-1.9.2>
19
+
20
+
21
+ # Additional notes
22
+ If you want to do anything real with this, you should save the results of the extract method to a YAML file or database table.
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib/', __FILE__)
3
+ $:.unshift lib unless $:.include?(lib)
4
+
5
+ require 'version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "google_font_extractor"
9
+ s.version = GoogleFontExtractor::VERSION
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = ["Jason Lambert"]
12
+ s.email = ["mail@jasonlambert.co.uk"]
13
+ s.homepage = "https://github.com/mendable/google-font-extractor"
14
+ s.summary = "Extracts a list of all fonts available via the Google Webfonts API"
15
+ s.description = "Extracts a list of all fonts available via the Google Webfonts API, including details of character sets supported, weights, and categorization."
16
+
17
+ s.required_rubygems_version = ">= 1.3.6"
18
+
19
+ s.files = Dir.glob("lib/*") + %w(README.md google_font_extractor.gemspec)
20
+ s.require_paths = ['lib']
21
+ s.rubyforge_project = %q{[none]}
22
+
23
+
24
+ if s.respond_to? :specification_version then
25
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
29
+ s.add_runtime_dependency(%q<tidy_ffi>, [">= 0.1.3"])
30
+ s.add_runtime_dependency(%q<scrapi>, [">= 2.0.0"])
31
+ else
32
+ s.add_dependency(%q<tidy_ffi>, [">= 0.1.3"])
33
+ s.add_dependency(%q<scrapi>, [">= 2.0.0"])
34
+ end
35
+ else
36
+ s.add_dependency(%q<tidy_ffi>, [">= 0.1.3"])
37
+ s.add_dependency(%q<scrapi>, [">= 2.0.0"])
38
+ end
39
+
40
+ end
data/lib/extract.rb ADDED
@@ -0,0 +1,68 @@
1
+ module GoogleFontExtractor
2
+ module Extract
3
+
4
+ # Individual Font details page
5
+ class FontPage < Scraper::Base
6
+ array :weights
7
+
8
+ process("#_variants span.parametername", :weights => :text) do |el|
9
+ @weights.each_with_index do |r, idx|
10
+ if r =~ /Web Fonts API parameter name: (.*)/ then
11
+ @weights[idx] = $1.split(':').size == 1 ? "regular" : $1.split(':').last
12
+ end
13
+ end
14
+ end
15
+
16
+ result :weights
17
+ end
18
+
19
+ # Element within list of fonts
20
+ class FontListElement < Scraper::Base
21
+ array :weights
22
+ array :charsets
23
+
24
+ attr_accessor :category # for storing serif, sans-serif, etc
25
+
26
+ # Font Name
27
+ process "div.preview span", :name => :text
28
+
29
+ # Weights
30
+ process "div.godesigner span.variantcount span", :weights_count => :text
31
+
32
+ # Families. Note, FamiliesCount will always be nil in the final output
33
+ # because the main extract method breaks down to specific fonts, rather
34
+ # than their meta-family grouping.
35
+ process "div.godesigner span.familycount span", :families_count => :text
36
+
37
+ # Designer name, but remove variants and families count text
38
+ process("div.godesigner", :designer => :text) do |el|
39
+ @designer = @designer.gsub(" #{@weights_count}&nbsp;variants", '')
40
+ @designer = @designer.gsub(" #{@families_count}&nbsp;families", '')
41
+
42
+ # remove font name from designer name
43
+ if @designer =~ /by (.*)/ then
44
+ @designer = $1
45
+ end
46
+ end
47
+
48
+ # Each listing has a small png image icon denoting its available character set.
49
+ process("div.ranges img", :charsets => "@src") do |el|
50
+ @charsets.each_with_index do |r, idx|
51
+ if r =~ /ic_(\w+)\.png/ then
52
+ @charsets[idx] = $1
53
+ end
54
+ end
55
+ end
56
+
57
+ result :designer, :name, :charsets, :weights_count, :weights, :families_count, :category
58
+ end
59
+
60
+
61
+ # Main list of fonts
62
+ class FontList < Scraper::Base
63
+ array :fonts
64
+ process "#fontlist li", :fonts => FontListElement
65
+ result :fonts
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,50 @@
1
+ require 'tidy_ffi'
2
+ require 'scrapi'
3
+
4
+ require File.dirname(__FILE__) + '/extract.rb'
5
+
6
+
7
+ module GoogleFontExtractor
8
+
9
+ CATEGORIES = %w(serif sans-serif display handwriting)
10
+
11
+ def self.fonts
12
+ # main list
13
+ final_font_list = []
14
+
15
+
16
+ CATEGORIES.each do |category|
17
+ # Get intermediate list of fonts
18
+ font_list = Extract::FontList.scrape(URI.parse("http://www.google.com/webfonts?sort=pop&category=#{category}"))
19
+
20
+
21
+ # Build final list of real fonts. Some fonts are listed are grouped by family on the main font
22
+ # list, break those familiies down into individual fonts.
23
+ font_list.each_with_index do |font, idx|
24
+ if font.families_count then
25
+ Extract::FontList.scrape(URI.parse("http://www.google.com/webfonts/list?family=#{URI.encode(font.name)}&sort=pop&category=#{category}")).each do |intermediate|
26
+ intermediate[:category] = category
27
+ final_font_list << intermediate
28
+ end
29
+ else
30
+ font[:category] = category
31
+ final_font_list << font
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+ # Tidy.
38
+ final_font_list.flatten!
39
+
40
+
41
+ # Further enhance final list of fonts with additional information from the individual font pages
42
+ # so we can provide information about the font weights (bold, etc) that are available.
43
+ final_font_list.each_with_index do |font, idx|
44
+ if font.weights_count then
45
+ font.weights = Extract::FontPage.scrape(URI.parse("http://www.google.com/webfonts/family?family=#{URI.encode(font.name)}"))
46
+ end
47
+ end
48
+ end
49
+
50
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module GoogleFontExtractor
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_font_extractor
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Jason Lambert
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-18 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: tidy_ffi
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 1
31
+ - 3
32
+ version: 0.1.3
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: scrapi
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 0
46
+ - 0
47
+ version: 2.0.0
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: Extracts a list of all fonts available via the Google Webfonts API, including details of character sets supported, weights, and categorization.
51
+ email:
52
+ - mail@jasonlambert.co.uk
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - lib/extract.rb
61
+ - lib/google_font_extractor.rb
62
+ - lib/version.rb
63
+ - README.md
64
+ - google_font_extractor.gemspec
65
+ has_rdoc: true
66
+ homepage: https://github.com/mendable/google-font-extractor
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 1
89
+ - 3
90
+ - 6
91
+ version: 1.3.6
92
+ requirements: []
93
+
94
+ rubyforge_project: "[none]"
95
+ rubygems_version: 1.3.7
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Extracts a list of all fonts available via the Google Webfonts API
99
+ test_files: []
100
+