jekyll-fontello 0.1.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +65 -0
  4. data/lib/jekyll-fontello.rb +146 -0
  5. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ecf7cd2aabfffcc5d47563a709d91fe7fe3fed83
4
+ data.tar.gz: bbece952a958c5dbb3371b4be4ecbd8880057467
5
+ SHA512:
6
+ metadata.gz: 64987c1f9953de429673ad914486908520245ef55940ad2d761b7c9efeae68015caef03c1b6cf7576cae6d9f290f8e0bc677885a21c96672308267cdab5a4d75
7
+ data.tar.gz: 8ae4391e5f194c41b4cdf8a7c2e62b6f79fb2d2370c41de1eccedbb330ad0d7ad392eba621e1cef8441953ee8a50120abc4cc3e346fabe30faae598118af7b27
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Yihang Ho
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # jekyll-fontello
2
+
3
+ Jekyll plugin that automatically downloads your webfont from Fontello.
4
+
5
+ ## Installation
6
+
7
+ 1. Install the `jekyll-fontello` gem:
8
+ ```
9
+ gem install jekyll-fontello
10
+ ```
11
+ 2. Add `jekyll-fontello` to the list of gems in `_config.yml`:
12
+ ```yaml
13
+ gems: ["jekyll-fontello"]
14
+ ```
15
+ 3. Add a Fontello configuration file named `fontello_config.json` to your project.
16
+ 4. Use Fontello icons on your website or blog!
17
+
18
+ _Or_ copy `lib/jekyll-fontello.rb` into your `_plugins` directory.
19
+
20
+ ## Options
21
+
22
+ #### Config file
23
+
24
+ Change the name/path of the Fontello configuration file, the default value is `'fontello_config.json'`.
25
+
26
+ ```yaml
27
+ fontello:
28
+ config: 'config.json'
29
+ ```
30
+
31
+ #### Output fonts
32
+
33
+ Change the output path of the font files, the default value is `'fontello/fonts'`.
34
+
35
+ ```yaml
36
+ fontello:
37
+ output_fonts: 'assets/fonts/fontello'
38
+ ```
39
+
40
+ #### Output stylesheets
41
+
42
+ Change the output path of the stylesheet files, the default value is `'fontello/styles'`.
43
+
44
+ ```yaml
45
+ fontello:
46
+ output_styles: 'styles/fontello'
47
+ ```
48
+
49
+ #### Custom fonts path
50
+
51
+ The path to the font files that should be put in the stylesheets By default this is computed as the relative path from `output_styles` to `output_fonts`.
52
+
53
+ ```yaml
54
+ fontello:
55
+ fonts_path: '/assets/fonts/fontello'
56
+ ```
57
+
58
+ #### Preprocessor
59
+
60
+ Change what CSS preprocessor is used, by default no preprocessor is used. Allowed values are `'none'`, `'less'` and `'scss'`.
61
+
62
+ ```yaml
63
+ fontello:
64
+ preprocessor: 'scss'
65
+ ```
@@ -0,0 +1,146 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'zip'
4
+
5
+ Zip.on_exists_proc = true
6
+
7
+ module Fontello
8
+ class Generator < Jekyll::Generator
9
+ FONTELLO_URL = 'http://fontello.com'
10
+ SESSION_FILE = "#{Dir.tmpdir}/jekyll_fontello_session"
11
+ ZIP_FILE = "#{Dir.tmpdir}/jekyll_fontello.zip"
12
+
13
+ # Read the plugin configuration and generate the
14
+ # Fontello files based on that.
15
+ #
16
+ # required by Jekyll
17
+ def generate(site)
18
+ @download_tries = 0
19
+
20
+ # Load configuration from _config.yml
21
+ plugin_config = site.config['fontello'] || { }
22
+ @config_file = plugin_config['config'] || 'fontello_config.json'
23
+ @output_fonts = plugin_config['output_fonts']|| 'fontello/fonts'
24
+ @output_styles = plugin_config['output_styles'] || 'fontello/styles'
25
+ @fonts_path = plugin_config['fonts_path'] || relative_path_styles_to_fonts
26
+ @preprocessor = plugin_config['preprocessor'] || 'none'
27
+
28
+ # Read the configuration
29
+ @fontello_config = File.read(@config_file)
30
+
31
+ # Main pipeline
32
+ download_zip()
33
+ extract_zip(site)
34
+ set_font_path()
35
+ clean_up()
36
+ end
37
+
38
+ # Downloads the zip file from Fontello based on
39
+ # the local configuration.
40
+ #
41
+ # see: https://github.com/fontello/fontello/#api-methods
42
+ def download_zip()
43
+ url = URI("#{FONTELLO_URL}/#{session_key}/get")
44
+ response = Net::HTTP.get_response(url)
45
+
46
+ if response.code != '200'
47
+ raise 'can\'t connect to Fontello' if @download_tries == 3
48
+
49
+ # Remove the session key so a new one is requested
50
+ File.delete(SESSION_FILE)
51
+
52
+ # And retry download
53
+ @download_tries += 1
54
+ download_zip()
55
+ else
56
+ File.write(ZIP_FILE, response.read_body, universal_newline: true)
57
+ end
58
+ end
59
+
60
+ # Extract the zip file downloaded from Fontello.
61
+ def extract_zip(site)
62
+ Zip::File.open(ZIP_FILE) do |zipfile|
63
+ # Create the output directories
64
+ FileUtils.mkdir_p("#{@output_fonts}")
65
+ FileUtils.mkdir_p("#{@output_styles}")
66
+
67
+ # Extract the relevant files from the .zip
68
+ zipfile.each do |file|
69
+ filename = File.basename(file.name)
70
+ case File.extname file.name
71
+ when '.woff', '.svg', '.ttf', '.eot', '.woff2'
72
+ site.static_files << Jekyll::StaticFile.new(site, site.source, @output_fonts, filename)
73
+ file.extract("#{@output_fonts}/#{filename}")
74
+ when '.css'
75
+ case @preprocessor
76
+ when 'none'
77
+ file.extract("#{@output_styles}/#{filename}")
78
+ when 'less'
79
+ file.extract("#{@output_styles}/#{filename.sub('.css', '.less')}")
80
+ when 'scss'
81
+ file.extract("#{@output_styles}/_#{filename.sub('.css', '.scss')}")
82
+ else
83
+ raise "unknown preprocessor #{@preprocessor}, should be any of \"none\", \"less\" or \"scss\""
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ # Update the font paths present in the stylesheets
91
+ # based on the output folders/configuration.
92
+ def set_font_path()
93
+ Dir.entries(@output_styles).each do |filename|
94
+ next if filename =~ /^\.\.?$/
95
+ filepath = "#{@output_styles}/#{filename}"
96
+
97
+ text = File.read(filepath)
98
+ new_text = text.gsub(/..\/font/, "#{@fonts_path}")
99
+ File.write(filepath, new_text)
100
+ end
101
+ end
102
+
103
+ # Clean up after the Generator.
104
+ def clean_up()
105
+ File.delete(ZIP_FILE)
106
+ end
107
+
108
+ # Get a session key from Fontello or a session key
109
+ # acquired before.
110
+ #
111
+ # see: https://github.com/fontello/fontello/#api-methods
112
+ def session_key
113
+ if File.exists? SESSION_FILE
114
+ return File.read(SESSION_FILE)
115
+ end
116
+
117
+ url = URI(FONTELLO_URL)
118
+ boundary = '----WebKitFormREQUEST_BOUNDARY7MA4YWxkTrZu0gW'
119
+
120
+ # Setup a http connection with Fontello
121
+ http = Net::HTTP.new url.host, url.port
122
+
123
+ # Construct the request to get a session key
124
+ request = Net::HTTP::Post.new url
125
+ request['Content-Type'] = "multipart/form-data; REQUEST_BOUNDARY=#{boundary}"
126
+ request['Content-Disposition'] = 'multipart/form-data'
127
+ request['Cache-Control'] = 'no-cache'
128
+ request.body = "--#{boundary}\r\nContent-Disposition: form-data; name=\"config\"; filename=\"#{@config_file}\"\r\nContent-Type: application/json\r\n\r\n#{@fontello_config}\r\n\r\n\r\n--#{boundary}"
129
+
130
+ # Send the request to Fontello
131
+ response = http.request(request)
132
+
133
+ # Store the session key for later use
134
+ File.write(SESSION_FILE, response.read_body)
135
+ return response.read_body
136
+ end
137
+
138
+ # Get the relative path between the fonts output
139
+ # folder and the styles output folder.
140
+ def relative_path_styles_to_fonts
141
+ style_path = Pathname.new(@output_styles)
142
+ fonts_path = Pathname.new(@output_fonts)
143
+ return fonts_path.relative_path_from(style_path)
144
+ end
145
+ end
146
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-fontello
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Cornelissen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Jekyll plugin that automatically downloads your webfont from Fontello.
14
+ email: ericornelissen@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.md
19
+ - LICENSE
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - lib/jekyll-fontello.rb
24
+ homepage: http://rubygems.org/gems/hola
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.6.12
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Fontello for jekyll
48
+ test_files: []