markdown_titles_to_svg 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 04730d2d939be49a169d9b99eb05af099968131245401fc9a87bc4c791ce8806
4
+ data.tar.gz: 398032d4a02f9b254c40be22966672e6b3bcf811e02702f5386620d849202180
5
+ SHA512:
6
+ metadata.gz: 30d457f7ad4932c2fad26fc4e5e5aae7c173d2a7762a226d7041a68f8a61ddcc1eb28411c2ec1f456e17a7c30a4454ee21f6ead4f7e38c0998c143697fec177e
7
+ data.tar.gz: 547e57eef58726527b3102718c1507b7c39c8bd14b11a88e42a595215ad51d78104c4fff22c1982b3fba24382cad4c6e9e1b8eeb120f2fdf798e43f08885a490
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /test/*/
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-06-17
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at andreasbanholzer@me.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in markdown_titles_to_svg.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rubocop", "~> 1.7"
11
+
12
+ gem 'text2svg'
13
+ gem 'nokogiri'
14
+ gem 'fileutils'
15
+ gem 'json'
16
+ gem 'zip'
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ Markdown_titles_to_svg (0.1.0)
5
+ fileutils
6
+ json
7
+ nokogiri
8
+ text2svg
9
+ zip
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ ast (2.4.2)
15
+ ffi (1.15.3)
16
+ fileutils (1.5.0)
17
+ freetype (0.0.6)
18
+ ffi
19
+ json (2.5.1)
20
+ nokogiri (1.11.7-x86_64-darwin)
21
+ racc (~> 1.4)
22
+ parallel (1.20.1)
23
+ parser (3.0.1.1)
24
+ ast (~> 2.4.1)
25
+ racc (1.5.2)
26
+ rainbow (3.0.0)
27
+ rake (13.0.3)
28
+ regexp_parser (2.1.1)
29
+ rexml (3.2.5)
30
+ rubocop (1.17.0)
31
+ parallel (~> 1.10)
32
+ parser (>= 3.0.0.0)
33
+ rainbow (>= 2.2.2, < 4.0)
34
+ regexp_parser (>= 1.8, < 3.0)
35
+ rexml
36
+ rubocop-ast (>= 1.7.0, < 2.0)
37
+ ruby-progressbar (~> 1.7)
38
+ unicode-display_width (>= 1.4.0, < 3.0)
39
+ rubocop-ast (1.7.0)
40
+ parser (>= 3.0.1.1)
41
+ ruby-progressbar (1.11.0)
42
+ text2svg (0.5.1)
43
+ freetype (>= 0.0.6)
44
+ unicode-display_width (2.0.0)
45
+ zip (2.0.2)
46
+
47
+ PLATFORMS
48
+ x86_64-darwin-20
49
+
50
+ DEPENDENCIES
51
+ Markdown_titles_to_svg!
52
+ fileutils
53
+ json
54
+ nokogiri
55
+ rake (~> 13.0)
56
+ rubocop (~> 1.7)
57
+ text2svg
58
+ zip
59
+
60
+ BUNDLED WITH
61
+ 2.2.19
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 a6b8
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # MarkdownHeadlinesToSvg
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/markdown_headlines_to_svg`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'markdown_headlines_to_svg'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install markdown_headlines_to_svg
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/markdown_headlines_to_svg. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/markdown_headlines_to_svg/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the MarkdownHeadlinesToSvg project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/markdown_headlines_to_svg/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "markdown_titles_to_svg"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,479 @@
1
+ # frozen_string_literal: true
2
+ require 'text2svg'
3
+ require 'nokogiri'
4
+ require 'net/http'
5
+ require 'fileutils'
6
+ # require 'active_support/core_ext/hash/indifferent_access'
7
+ require 'json'
8
+ require 'zip/zip'
9
+
10
+
11
+ require_relative "markdown_titles_to_svg/version"
12
+
13
+ module MarkdownTitlesToSvg
14
+ class Error < StandardError; end
15
+
16
+ @TEMPLATE= {
17
+ github: {
18
+ profile: nil,
19
+ source: 'https://raw.githubusercontent.com/'
20
+ },
21
+ font: {
22
+ text_align: :left,
23
+ bold: true,
24
+ local: false,
25
+ source: {
26
+ local_path: '',
27
+ google_fonts: {
28
+ name: 'Amatic SC',
29
+ variant: 'regular',
30
+ subset: 'latin'
31
+ },
32
+ current: nil,
33
+ }
34
+ },
35
+ view: {
36
+ offset: {
37
+ height: -800,
38
+ widht: 0
39
+ }
40
+ },
41
+ style: {
42
+ color: {
43
+ opacity: 1.0,
44
+ default: '#A5834B',
45
+ palette: []
46
+ },
47
+ stroke: {
48
+ color: 'none',
49
+ width: '10px',
50
+ opacity: 1.0,
51
+ linecap: 'butt'
52
+ }
53
+ }
54
+ }
55
+
56
+
57
+ def self.get_options()
58
+ return @TEMPLATE
59
+ end
60
+
61
+
62
+ def self.generate( markdowns, gh_name, options = {} )
63
+ if self.validate( markdowns, gh_name, options, @TEMPLATE )
64
+ obj = self.options_update( options, @TEMPLATE, 'set_options' )
65
+ obj[:github][:profile] = gh_name
66
+
67
+ if obj[:font][:source][:user].nil?
68
+ obj[:font][:source][:current] = obj[:font][:source][:default]
69
+ else
70
+ obj[:font][:source][:current] = obj[:font][:source][:user]
71
+ end
72
+
73
+ if obj[:font][:local]
74
+ obj[:font][:source][:current] = obj[:font][:source][:local_path]
75
+ else
76
+ obj[:font][:source][:current] = "./tmp-#{Time.now.getutc.to_i}.ttf"
77
+ google_font_download(
78
+ obj[:font][:source][:google_fonts][:name],
79
+ obj[:font][:source][:google_fonts][:variant],
80
+ obj[:font][:source][:google_fonts][:subset],
81
+ obj[:font][:source][:current]
82
+ )
83
+ end
84
+
85
+ cmds = self.markdowns_read( markdowns, obj )
86
+ root = './'
87
+ cmds.each do | cmd |
88
+ svg = self.svg_generate( cmd[:headline], obj )
89
+ FileUtils.mkdir_p ( File.dirname( cmd[:path] ) )
90
+ File.open( cmd[:path], "w" ) { | f | f.write( svg ) }
91
+ end
92
+ end
93
+
94
+ !obj[:font][:local] ? File.delete( obj[:font][:source][:current] ) : ''
95
+
96
+ return true
97
+ end
98
+
99
+
100
+ private
101
+
102
+
103
+ def self.str_difference( a, b )
104
+ a = a.to_s.downcase.split( '_' ).join( '' )
105
+ b = b.to_s.downcase.split( '_' ).join( '' )
106
+ longer = [ a.size, b.size ].max
107
+ same = a
108
+ .each_char
109
+ .zip( b.each_char )
110
+ .select { | a, b | a == b }
111
+ .size
112
+ ( longer - same ) / a.size.to_f
113
+ end
114
+
115
+
116
+ def self.google_font_download( font, style, subset, file_name )
117
+ def self.unzip_file( file, destination, file_name )
118
+ Zip::ZipFile.open( file ) do | zip_file |
119
+ zip_file.each do | f |
120
+ f_path=File.join( destination, file_name )
121
+ FileUtils.mkdir_p( File.dirname( f_path ) )
122
+ zip_file.extract( f, f_path ) unless File.exist?( f_path )
123
+ end
124
+ end
125
+ end
126
+
127
+
128
+ server = 'https://google-webfonts-helper.herokuapp.com'
129
+ url = "#{server}/api/fonts"
130
+ uri = URI( url )
131
+ response = Net::HTTP.get( uri )
132
+ fonts_all = JSON.parse( response )
133
+
134
+ fonts_subset = fonts_all.select { | a | a["subsets"].include?( subset ) }
135
+
136
+ id = fonts_subset
137
+ .map { | a | { name: a["id"], score: self.str_difference( font, a["id"] ) } }
138
+ .min_by { | h | h[:score] }
139
+
140
+ font = fonts_subset
141
+ .find { | a | a["id"].eql? id[:name] }
142
+
143
+ variant = font['variants']
144
+ .map { | a | { name: a, score: self.str_difference( style.to_s, a.to_s ) } }
145
+ .min_by { | h | h[:score] }
146
+
147
+ puts "Font: #{font["id"]} #{variant[:name]} (#{subset})"
148
+
149
+ url = ''
150
+ url += server
151
+ url += "/api/fonts/#{id[:name]}?download=zip&subsets=#{subset}&formats=ttf&variants=#{variant[:name]}"
152
+
153
+ uri = URI( url )
154
+ response = Net::HTTP.get( uri )
155
+
156
+ path_zip = "./tmp-#{Time.now.getutc.to_i}.zip"
157
+
158
+ File.open( path_zip, "w" ) { | f | f.write( response ) }
159
+ unzip_file( path_zip, File.dirname( path_zip ), file_name )
160
+
161
+ File.delete( path_zip )
162
+ end
163
+
164
+
165
+ def self.validate( markdowns, gh_name, vars, template )
166
+ messages = {
167
+ markdowns: [],
168
+ github: [],
169
+ options: [],
170
+ other: []
171
+ }
172
+
173
+ begin
174
+ if markdowns.class.to_s.eql? 'Array'
175
+ if markdowns.map { | a | a.start_with?( template[:github][:source] ) }.all?
176
+ if markdowns.map { | a | a.end_with?( '.md' ) }.all?
177
+
178
+ else
179
+ messages[:markdowns].push( "Make sure all your .md Files end with .md")
180
+ end
181
+ else
182
+ messages[:markdowns].push( "Make sure all your .md Files start with: #{template[:github][:source]}")
183
+ end
184
+ else
185
+ messages[:markdowns].push( 'Is not Type "Array" or "String".' )
186
+ end
187
+
188
+ if gh_name.class.to_s.eql? 'String'
189
+ if !gh_name.eql? ''
190
+
191
+ else
192
+ messages[:github].push( "Github Name is required." )
193
+ end
194
+ else
195
+ messages[:github].push( 'Is not Type "String".' )
196
+ end
197
+
198
+ if vars.class.to_s == 'Hash'
199
+ messages[:options] = self.options_update( vars, template, 'options_valid?' )
200
+ else
201
+ messages[:options].push( 'Is not Type "Hash".')
202
+ end
203
+
204
+ rescue
205
+ messages[:other].push( "Undefined error occured.")
206
+ end
207
+
208
+ valid = messages.keys.map { | key | messages[ key ].length }.sum == 0
209
+
210
+ if !valid
211
+ puts 'Following errors occured:'
212
+ messages.keys.each do | key |
213
+ if messages[ key ].length != 0
214
+
215
+ puts " #{key[ 0, 1 ].upcase}#{key[ 1, key.length ]}"
216
+ messages[ key ].each do | m |
217
+ puts " - #{m}"
218
+ end
219
+ end
220
+ end
221
+ end
222
+
223
+ return valid
224
+ end
225
+
226
+
227
+ def self.options_update( vars, template, mode )
228
+ allow_list = [
229
+ :font__text_align,
230
+ :font__bold,
231
+ :font__local,
232
+ :font__source__local_path,
233
+ :font__source__google_fonts__name,
234
+ :font__source__google_fonts__variant,
235
+ :font__source__google_fonts__subset,
236
+ :view__offset__height,
237
+ :view__offset__widht,
238
+ :style__color__opacity,
239
+ :style__color__default,
240
+ :style__color__palette,
241
+ :style__stroke__color,
242
+ :style__stroke__width,
243
+ :style__stroke__opacity,
244
+ :style__stroke__linecap
245
+ ]
246
+
247
+ messages = []
248
+ _options = Marshal.load( Marshal.dump( template ) )
249
+
250
+ vars.keys.each do | key |
251
+ if allow_list.include?( key )
252
+
253
+ keys = key.to_s.split( '__' ).map { | a | a.to_sym }
254
+ case( keys.length )
255
+ when 1
256
+ _options[ keys[ 0 ] ] = vars[ key ]
257
+ when 2
258
+ _options[ keys[ 0 ] ][ keys[ 1 ] ] = vars[ key ]
259
+ when 3
260
+ _options[ keys[ 0 ] ][ keys[ 1 ] ][ keys[ 2 ] ] = vars[ key ]
261
+ when 4
262
+ _options[ keys[ 0 ] ][ keys[ 1 ] ][ keys[ 2 ] ][ keys[ 3 ] ] = vars[ key ]
263
+ end
264
+ else
265
+ nearest = allow_list
266
+ .map { | word | { score: self.str_difference( key, word ), word: word } }
267
+ .min_by { | item | item[:score] }
268
+
269
+ message = "\"#{key}\" is not a valid key, did you mean \"<--similar-->\"?"
270
+ message = message.gsub( '<--similar-->', nearest[:word].to_s )
271
+ messages.push( message )
272
+ end
273
+ end
274
+
275
+ result = nil
276
+ case mode
277
+ when 'options_valid?'
278
+ result = messages
279
+ when 'set_options'
280
+ result = _options
281
+ end
282
+
283
+ return result
284
+ end
285
+
286
+
287
+ def self.svg_elements( svg )
288
+ svg = svg[ svg.index( "<path transform" ), svg.length ]
289
+ svg = svg[ 0, svg.rindex( "\n</g>\n</svg>\n" ) ]
290
+ elements = []
291
+ found = true
292
+ while found
293
+ to = '/>'
294
+ element = svg[ 0, svg.index( to ) + to.length ].strip
295
+ elements.push( element )
296
+ svg = svg[ svg.index( to ) + to.length, svg.length ]
297
+ !svg.include?( 'path' ) ? found = false : ''
298
+ end
299
+
300
+ return elements
301
+ end
302
+
303
+
304
+ def self.svg_items_decode( str, elements, obj )
305
+ def self.svg_item( char, color, element, obj )
306
+ item = {
307
+ "character": nil,
308
+ "fill": nil,
309
+ "fill-opacity": nil,
310
+ "stroke": nil,
311
+ "stroke-width": nil,
312
+ "stroke-linecap": nil,
313
+ "stroke-opacity": nil,
314
+ "transform": nil,
315
+ "d": nil
316
+ }
317
+
318
+ item[:"character"] = char
319
+ item[:"fill"] = color
320
+ item[:"fill-opacity"] = obj[:style][:color][:opacity]
321
+ item[:"stroke"] = obj[:style][:stroke][:color]
322
+ item[:"stroke-width"] = obj[:style][:stroke][:width]
323
+ item[:"stroke-linecap"] = obj[:style][:stroke][:linecap]
324
+ item[:"stroke-opacity"] = obj[:style][:stroke][:opacity]
325
+
326
+ [ :"transform", :"d" ].each do | key |
327
+ search = key.to_s
328
+ doc = Nokogiri::XML( element )
329
+ item[ key ] = doc.xpath("//*[@#{search}]")[ 0 ].attribute( search ).to_s
330
+ end
331
+
332
+ return item
333
+ end
334
+
335
+
336
+ characters = str.gsub( ' ', '' )
337
+ items = []
338
+ elements.each.with_index do | element, index |
339
+ if obj[:style][:color][:palette].length <= index
340
+ color = obj[:style][:color][:default]
341
+ else
342
+ color = obj[:style][:color][:palette][ index ]
343
+ end
344
+
345
+ item = self.svg_item( characters[ index ], color, element, obj )
346
+ items.push( item )
347
+ end
348
+ return items
349
+ end
350
+
351
+
352
+ def self.svg_header( svg, items, obj )
353
+ element = svg[ 0, svg.index( "<path transform" ) ]
354
+ search = 'viewBox'
355
+ doc = Nokogiri::XML( element )
356
+ values = doc.xpath("//*[@#{search}]")[ 0 ].attribute( search ).to_s
357
+ values_mod = values.split( ' ' ).map { | a | a.to_i }
358
+ values_mod[ 3 ] = values_mod[ 3 ] + obj[:view][:offset][:height]
359
+ from = "viewBox=\"#{values}\""
360
+ to = "viewBox=\"#{values_mod.join(' ')}\""
361
+ element = element.gsub( from, to )
362
+ return element
363
+ end
364
+
365
+
366
+ def self.svg_create_code( items, svg, obj )
367
+ el = items.map do | item |
368
+ p = ''
369
+ p += "\t\t<path "
370
+ p += item.map { | c | "#{c[ 0 ]}=\"#{c[ 1 ]}\"" }.join( ' ' )
371
+ p += '/>'
372
+ end
373
+
374
+ el = el.join( "\n" )
375
+
376
+ code = ''
377
+ code += self.svg_header( svg, items, obj )
378
+ code += el.to_s
379
+ code += "\n</g>\n</svg>"
380
+ return code
381
+ end
382
+
383
+
384
+ def self.svg_generate( str, obj )
385
+ svg = Text2svg(
386
+ str,
387
+ font: obj[:font][:source][:current],
388
+ text_align: obj[:font][:text_align],
389
+ bold: obj[:font][:bold]
390
+ ).to_s
391
+
392
+ elements = self.svg_elements( svg )
393
+ items = self.svg_items_decode( str, elements, obj )
394
+ code = self.svg_create_code( items, svg, obj )
395
+ return code
396
+ end
397
+
398
+
399
+ def self.markdowns_read( markdowns, obj )
400
+ def self.markdown_read( url, obj )
401
+ messages = []
402
+ cmds = []
403
+
404
+ uri = URI( url )
405
+ response = Net::HTTP.get( uri )
406
+ doc = Nokogiri::HTML( response )
407
+ doc.xpath( '//img' ).each do | element |
408
+ begin
409
+ if !element.attribute( 'banner' ).nil?
410
+ validation = "#{obj[:github][:source]}#{obj[:github][:profile]}/"
411
+ cmd = {
412
+ headline: nil,
413
+ src: nil,
414
+ file: nil,
415
+ path: nil
416
+ }
417
+
418
+ cmd[:src] = element.attribute( 'src' ).value
419
+ cmd[:file] = File.basename( cmd[:src] )
420
+
421
+ if cmd[:src].start_with?( validation )
422
+ if cmd[:src].end_with?( '.svg' )
423
+ if url =~ URI::regexp
424
+ cmd[:headline] = element.attribute( 'banner' ).value
425
+
426
+ tmp = cmd[:src][ cmd[:src].index( validation ) + validation.length, cmd[:src].length ]
427
+ tmp = tmp
428
+ .split( '/' )
429
+ .reject { | a | a.empty? }
430
+ .drop( 2 )
431
+ .join( '/' )
432
+ cmd[:path] = './' + tmp
433
+ cmds.push( cmd )
434
+ else
435
+ messages.push( "#{cmd[:file]}: Invalid URL Structure." )
436
+ end
437
+ else
438
+ messages.push( "#{cmd[:file]}: Suffix is not valid. Please use .svg instead." )
439
+ end
440
+ else
441
+ messages.push( "#{cmd[:file]}: URL not start with: \"#{validation}\"" )
442
+ end
443
+ end
444
+ rescue
445
+ messages.push( "#{url}: Can not load or a parsing Error occured." )
446
+ end
447
+ end
448
+ return [ cmds, messages ]
449
+ end
450
+
451
+
452
+ errors = {}
453
+ cmds = []
454
+ markdowns.each do | url |
455
+ cmds_tmp, errors_tmp = self.markdown_read( url, obj )
456
+ cmds = cmds.concat( cmds_tmp )
457
+ errors[ url ] = errors_tmp
458
+ end
459
+
460
+ err = errors.keys.map { | key | errors[ key ].length }.sum
461
+ if err != 0
462
+ str = 'Following Error'
463
+ err > 1 ? str += 's' : ''
464
+ str += ' occured: '
465
+ puts str
466
+
467
+ errors.keys.each do | key |
468
+ if errors[ key ].length != 0
469
+ puts " " + key + ':'
470
+ errors[ key ].each do | error |
471
+ puts ' - ' + error
472
+ end
473
+ end
474
+ end
475
+ end
476
+
477
+ return cmds
478
+ end
479
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownTitlesToSvg
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/markdown_titles_to_svg/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "markdown_titles_to_svg"
7
+ spec.version = MarkdownTitlesToSvg::VERSION
8
+ spec.authors = ["a6b8"]
9
+ spec.email = ["hello@13plu4.com"]
10
+
11
+ spec.summary = "Generate Headlines for (multiple) Github Markdown Pages"
12
+ spec.description = "Generate beautiful Headlines with Google Fonts for (multiple) Github Markdown Pages."
13
+ spec.homepage = "https://github.com/a6b8/markdown-titles-to-svg-for-ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/a6b8/markdown-titles-to-svg-for-ruby"
21
+ spec.metadata["changelog_uri"] = "https://raw.githubusercontent.com/a6b8/markdown-titles-to-svg-for-ruby/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+
34
+ spec.add_dependency 'text2svg', '0.5.1'
35
+ spec.add_dependency 'nokogiri', '1.11.7'
36
+ # spec.add_dependency 'net/http'
37
+ spec.add_dependency 'fileutils', '1.5.0'
38
+ # spec.add_dependency 'active_support/core_ext/hash/indifferent_access'
39
+ spec.add_dependency 'json', '2.5.1'
40
+ spec.add_dependency 'zip', '2.0.2'
41
+
42
+ # For more information and examples about making a new gem, checkout our
43
+ # guide at: https://bundler.io/guides/creating_gem.html
44
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markdown_titles_to_svg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - a6b8
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: text2svg
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.11.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.11.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: fileutils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.5.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.5.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: zip
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.2
83
+ description: Generate beautiful Headlines with Google Fonts for (multiple) Github
84
+ Markdown Pages.
85
+ email:
86
+ - hello@13plu4.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rubocop.yml"
93
+ - CHANGELOG.md
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/markdown_titles_to_svg.rb
103
+ - lib/markdown_titles_to_svg/version.rb
104
+ - markdown_titles_to_svg.gemspec
105
+ homepage: https://github.com/a6b8/markdown-titles-to-svg-for-ruby
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ homepage_uri: https://github.com/a6b8/markdown-titles-to-svg-for-ruby
111
+ source_code_uri: https://github.com/a6b8/markdown-titles-to-svg-for-ruby
112
+ changelog_uri: https://raw.githubusercontent.com/a6b8/markdown-titles-to-svg-for-ruby/main/CHANGELOG.md
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: 2.4.0
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubygems_version: 3.2.3
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Generate Headlines for (multiple) Github Markdown Pages
132
+ test_files: []