markdown_titles_to_svg 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04730d2d939be49a169d9b99eb05af099968131245401fc9a87bc4c791ce8806
4
- data.tar.gz: 398032d4a02f9b254c40be22966672e6b3bcf811e02702f5386620d849202180
3
+ metadata.gz: d0624a1975dce5637462bc4f80c829ee684a717fea9ee09f2bcf6fcba8a14baa
4
+ data.tar.gz: 426aa2d3820655b802dbf5898fd0b5079b965c0f0aea0882cdd52a9fbe8c50cd
5
5
  SHA512:
6
- metadata.gz: 30d457f7ad4932c2fad26fc4e5e5aae7c173d2a7762a226d7041a68f8a61ddcc1eb28411c2ec1f456e17a7c30a4454ee21f6ead4f7e38c0998c143697fec177e
7
- data.tar.gz: 547e57eef58726527b3102718c1507b7c39c8bd14b11a88e42a595215ad51d78104c4fff22c1982b3fba24382cad4c6e9e1b8eeb120f2fdf798e43f08885a490
6
+ metadata.gz: c98302754778d80f0f3595293861cbe6050115902d41a0695204570f669f38e47b43c48e6af65dcf6582b0431ce22e1ad424c4201e34947ee10f7f15d9029c19
7
+ data.tar.gz: 16e14716757d19b86c807ab3adaad631fc8601ab3e9698c5d30989293b2b957ca7103e6dcb22d936fe6d2c3bf3f35b29ad755d83ad5d182aa87ed22c1644778e
data/CHANGELOG.md CHANGED
@@ -3,3 +3,8 @@
3
3
  ## [0.1.0] - 2021-06-17
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.3] - 2021-06-17
8
+
9
+ - Google Fonts Feature
10
+ - Multiple Headline types
@@ -20,37 +20,67 @@ module MarkdownTitlesToSvg
20
20
  },
21
21
  font: {
22
22
  text_align: :left,
23
- bold: true,
24
- local: false,
25
- source: {
26
- local_path: '',
27
- google_fonts: {
28
- name: 'Amatic SC',
23
+ bold: false,
24
+ mode: {
25
+ h1: :local,
26
+ default: :google_fonts
27
+ },
28
+ local: {
29
+ h1: '/Library/Fonts/Microsoft/Corbel Bold.ttf',
30
+ default: '/Library/Fonts/Microsoft/Consolas.ttf'
31
+ },
32
+ google_fonts: {
33
+ h1: {
34
+ name: 'Oswald',
29
35
  variant: 'regular',
30
- subset: 'latin'
36
+ subset: 'latin'
31
37
  },
32
- current: nil,
38
+ default: {
39
+ name: 'Amatic SC',
40
+ variant: 'regular',
41
+ subset: 'latin'
42
+ }
43
+ },
44
+ current: {
45
+ h1: nil,
46
+ default: nil
33
47
  }
34
48
  },
35
49
  view: {
36
50
  offset: {
37
- height: -800,
51
+ height: 0,
38
52
  widht: 0
39
53
  }
40
54
  },
41
55
  style: {
42
- color: {
43
- opacity: 1.0,
44
- default: '#A5834B',
45
- palette: []
56
+ h1: {
57
+ color: {
58
+ opacity: 1.0,
59
+ default: '#A5834B',
60
+ palette: []
61
+ },
62
+ stroke: {
63
+ color: 'none',
64
+ width: '0px',
65
+ opacity: 1.0,
66
+ linecap: 'butt'
67
+ }
46
68
  },
47
- stroke: {
48
- color: 'none',
49
- width: '10px',
50
- opacity: 1.0,
51
- linecap: 'butt'
69
+ default: {
70
+ color: {
71
+ opacity: 1.0,
72
+ default: '#A5834B',
73
+ palette: []
74
+ },
75
+ stroke: {
76
+ color: 'none',
77
+ width: '0px',
78
+ opacity: 1.0,
79
+ linecap: 'butt'
80
+ }
52
81
  }
53
- }
82
+ },
83
+ silent: false
54
84
  }
55
85
 
56
86
 
@@ -63,35 +93,27 @@ module MarkdownTitlesToSvg
63
93
  if self.validate( markdowns, gh_name, options, @TEMPLATE )
64
94
  obj = self.options_update( options, @TEMPLATE, 'set_options' )
65
95
  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
96
+ obj = self.set_fonts( obj )
84
97
 
85
98
  cmds = self.markdowns_read( markdowns, obj )
99
+
86
100
  root = './'
87
101
  cmds.each do | cmd |
88
- svg = self.svg_generate( cmd[:headline], obj )
102
+ svg = self.svg_generate( cmd, obj )
89
103
  FileUtils.mkdir_p ( File.dirname( cmd[:path] ) )
90
104
  File.open( cmd[:path], "w" ) { | f | f.write( svg ) }
91
105
  end
92
106
  end
93
107
 
94
- !obj[:font][:local] ? File.delete( obj[:font][:source][:current] ) : ''
108
+ obj[:font][:mode].keys.each do | key |
109
+ if obj[:font][:mode][ key ] == :google_fonts
110
+ if ( Time.now.getutc.to_i - obj[:font][:current][ key ].split( '-' )[ 1 ].to_i ) < 30
111
+ if File.basename( obj[:font][:current][ key ] ) .start_with?( key.to_s )
112
+ File.delete( obj[:font][:current][ key ] )
113
+ end
114
+ end
115
+ end
116
+ end
95
117
 
96
118
  return true
97
119
  end
@@ -100,6 +122,37 @@ module MarkdownTitlesToSvg
100
122
  private
101
123
 
102
124
 
125
+ def self.set_fonts( obj )
126
+ types = obj[:font][:mode].keys
127
+ behaviours = types.map { | key | obj[:font][:mode][ key ] }
128
+
129
+ !obj[:silent] ? puts( 'Used Fonts: ' ) : ''
130
+ behaviours.each.with_index do | behaviour, index |
131
+ t = types[ index ].to_s.length < 3 ? "\t\t" : "\t"
132
+ !obj[:silent] ? print( " :#{types[ index ].to_s}#{t}> " ) : ''
133
+ case behaviour
134
+ when :local
135
+ obj[:font][:current][ types[ index ] ] = obj[:font][:local][ types[ index ] ]
136
+ tmp = File.basename( obj[:font][:current][ types[ index ] ] )
137
+ !obj[:silent] ? print( "#{tmp} (Local)" ) : ''
138
+ when :google_fonts
139
+ obj[:font][:current][ types[ index ] ] = "./#{types[ index ].to_s}-#{Time.now.getutc.to_i}.ttf"
140
+ self.google_font_download(
141
+ obj[:font][:google_fonts][ types[ index ] ][:name],
142
+ obj[:font][:google_fonts][ types[ index ] ][:variant],
143
+ obj[:font][:google_fonts][ types[ index ] ][:subset],
144
+ obj[:font][:current][ types[ index ] ],
145
+ types[ index ],
146
+ obj[:silent]
147
+ )
148
+ end
149
+ !obj[:silent] ? puts : ''
150
+ end
151
+
152
+ return obj
153
+ end
154
+
155
+
103
156
  def self.str_difference( a, b )
104
157
  a = a.to_s.downcase.split( '_' ).join( '' )
105
158
  b = b.to_s.downcase.split( '_' ).join( '' )
@@ -113,7 +166,7 @@ module MarkdownTitlesToSvg
113
166
  end
114
167
 
115
168
 
116
- def self.google_font_download( font, style, subset, file_name )
169
+ def self.google_font_download( font, style, subset, file_name, type, silent )
117
170
  def self.unzip_file( file, destination, file_name )
118
171
  Zip::ZipFile.open( file ) do | zip_file |
119
172
  zip_file.each do | f |
@@ -144,7 +197,7 @@ module MarkdownTitlesToSvg
144
197
  .map { | a | { name: a, score: self.str_difference( style.to_s, a.to_s ) } }
145
198
  .min_by { | h | h[:score] }
146
199
 
147
- puts "Font: #{font["id"]} #{variant[:name]} (#{subset})"
200
+ !silent ? print( "#{font["id"]} #{variant[:name]} #{subset} (Google Fonts)" ) : ''
148
201
 
149
202
  url = ''
150
203
  url += server
@@ -170,7 +223,7 @@ module MarkdownTitlesToSvg
170
223
  other: []
171
224
  }
172
225
 
173
- begin
226
+ #begin
174
227
  if markdowns.class.to_s.eql? 'Array'
175
228
  if markdowns.map { | a | a.start_with?( template[:github][:source] ) }.all?
176
229
  if markdowns.map { | a | a.end_with?( '.md' ) }.all?
@@ -196,14 +249,14 @@ module MarkdownTitlesToSvg
196
249
  end
197
250
 
198
251
  if vars.class.to_s == 'Hash'
199
- messages[:options] = self.options_update( vars, template, 'options_valid?' )
252
+ messages[:options] = self.options_update( vars, template, 'check_options' )
200
253
  else
201
254
  messages[:options].push( 'Is not Type "Hash".')
202
255
  end
203
256
 
204
- rescue
205
- messages[:other].push( "Undefined error occured.")
206
- end
257
+ #rescue
258
+ #messages[:other].push( "Undefined error occured.")
259
+ #end
207
260
 
208
261
  valid = messages.keys.map { | key | messages[ key ].length }.sum == 0
209
262
 
@@ -225,23 +278,34 @@ module MarkdownTitlesToSvg
225
278
 
226
279
 
227
280
  def self.options_update( vars, template, mode )
228
- allow_list = [
281
+ allow_list = [
282
+ :github__source,
229
283
  :font__text_align,
230
284
  :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,
285
+ :font__mode__h1,
286
+ :font__mode__default,
287
+ :font__google_fonts__h1__name,
288
+ :font__google_fonts__h1__variant,
289
+ :font__google_fonts__h1__subset,
290
+ :font__google_fonts__default__name,
291
+ :font__google_fonts__default__variant,
292
+ :font__google_fonts__default__subset,
236
293
  :view__offset__height,
237
294
  :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
295
+ :style__h1__color__opacity,
296
+ :style__h1__color__default,
297
+ :style__h1__color__palette,
298
+ :style__h1__stroke__color,
299
+ :style__h1__stroke__width,
300
+ :style__h1__stroke__opacity,
301
+ :style__h1__stroke__linecap,
302
+ :style__default__color__opacity,
303
+ :style__default__color__default,
304
+ :style__default__color__palette,
305
+ :style__default__stroke__color,
306
+ :style__default__stroke__width,
307
+ :style__default__stroke__opacity,
308
+ :style__default__stroke__linecap,
245
309
  ]
246
310
 
247
311
  messages = []
@@ -274,7 +338,7 @@ module MarkdownTitlesToSvg
274
338
 
275
339
  result = nil
276
340
  case mode
277
- when 'options_valid?'
341
+ when 'check_options'
278
342
  result = messages
279
343
  when 'set_options'
280
344
  result = _options
@@ -301,8 +365,8 @@ module MarkdownTitlesToSvg
301
365
  end
302
366
 
303
367
 
304
- def self.svg_items_decode( str, elements, obj )
305
- def self.svg_item( char, color, element, obj )
368
+ def self.svg_items_decode( cmd, elements, obj )
369
+ def self.svg_item( char, color, element, obj, cmd )
306
370
  item = {
307
371
  "character": nil,
308
372
  "fill": nil,
@@ -317,11 +381,11 @@ module MarkdownTitlesToSvg
317
381
 
318
382
  item[:"character"] = char
319
383
  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]
384
+ item[:"fill-opacity"] = obj[:style][ cmd[:type] ][:color][:opacity]
385
+ item[:"stroke"] = obj[:style][ cmd[:type] ][:stroke][:color]
386
+ item[:"stroke-width"] = obj[:style][ cmd[:type] ][:stroke][:width]
387
+ item[:"stroke-linecap"] = obj[:style][ cmd[:type] ][:stroke][:linecap]
388
+ item[:"stroke-opacity"] = obj[:style][ cmd[:type] ][:stroke][:opacity]
325
389
 
326
390
  [ :"transform", :"d" ].each do | key |
327
391
  search = key.to_s
@@ -333,16 +397,16 @@ module MarkdownTitlesToSvg
333
397
  end
334
398
 
335
399
 
336
- characters = str.gsub( ' ', '' )
400
+ characters = cmd[:headline].gsub( ' ', '' )
337
401
  items = []
338
402
  elements.each.with_index do | element, index |
339
- if obj[:style][:color][:palette].length <= index
340
- color = obj[:style][:color][:default]
403
+ if obj[:style][ cmd[:type] ][:color][:palette].length <= index
404
+ color = obj[:style][ cmd[:type] ][:color][:default]
341
405
  else
342
- color = obj[:style][:color][:palette][ index ]
406
+ color = obj[:style][ cmd[:type] ][:color][:palette][ index ]
343
407
  end
344
408
 
345
- item = self.svg_item( characters[ index ], color, element, obj )
409
+ item = self.svg_item( characters[ index ], color, element, obj, cmd )
346
410
  items.push( item )
347
411
  end
348
412
  return items
@@ -381,16 +445,16 @@ module MarkdownTitlesToSvg
381
445
  end
382
446
 
383
447
 
384
- def self.svg_generate( str, obj )
448
+ def self.svg_generate( cmd, obj )
385
449
  svg = Text2svg(
386
- str,
387
- font: obj[:font][:source][:current],
450
+ cmd[:headline],
451
+ font: obj[:font][:current][ cmd[:type] ],
388
452
  text_align: obj[:font][:text_align],
389
453
  bold: obj[:font][:bold]
390
454
  ).to_s
391
455
 
392
456
  elements = self.svg_elements( svg )
393
- items = self.svg_items_decode( str, elements, obj )
457
+ items = self.svg_items_decode( cmd, elements, obj )
394
458
  code = self.svg_create_code( items, svg, obj )
395
459
  return code
396
460
  end
@@ -406,10 +470,11 @@ module MarkdownTitlesToSvg
406
470
  doc = Nokogiri::HTML( response )
407
471
  doc.xpath( '//img' ).each do | element |
408
472
  begin
409
- if !element.attribute( 'banner' ).nil?
473
+ if !element.attribute( 'alt' ).nil?
410
474
  validation = "#{obj[:github][:source]}#{obj[:github][:profile]}/"
411
475
  cmd = {
412
476
  headline: nil,
477
+ type: nil,
413
478
  src: nil,
414
479
  file: nil,
415
480
  path: nil
@@ -421,8 +486,11 @@ module MarkdownTitlesToSvg
421
486
  if cmd[:src].start_with?( validation )
422
487
  if cmd[:src].end_with?( '.svg' )
423
488
  if url =~ URI::regexp
424
- cmd[:headline] = element.attribute( 'banner' ).value
425
-
489
+ cmd[:headline] = element.attribute( 'alt' ).value
490
+
491
+ cmd[:type] = cmd[:headline].count( '#' ) == 0 ? :default : :h1
492
+ cmd[:headline] = cmd[:headline].gsub( '#', '' ).strip
493
+
426
494
  tmp = cmd[:src][ cmd[:src].index( validation ) + validation.length, cmd[:src].length ]
427
495
  tmp = tmp
428
496
  .split( '/' )
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MarkdownTitlesToSvg
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_titles_to_svg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - a6b8