victor 0.3.1 → 0.3.4

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: ff2d91fe91857b10d35b9762dcbec3ee26ac3028c5522b7bdb33f73570a53422
4
- data.tar.gz: b1fce99903fcf5cc4d1b024b48d8382046e4426c57bb7a621265abb3db187008
3
+ metadata.gz: 4f308143350a9236bbdf1c046de94a94728ecd90051004482e78cf9734484ef6
4
+ data.tar.gz: 36b35af10ea11a1bc39063384c6a23e247c257d0364edb24b5914cfd73029a88
5
5
  SHA512:
6
- metadata.gz: 25eac4f567fa4b492bc9bded59d2194f2ac4c89283f71b748db2c8ccc3245793cdea7535131058282dace0fd366aa15f4e676414c2a2c823ea37d9f7777b01e4
7
- data.tar.gz: 694d7f6ff8b2264aec64d3d2955751b9b375a0e25cc4b1aa35c6328de97ee6ce6996ff8744bc86bb5b50cb9ed712644c25431f2a7213b68a8ba05e5b1fdb9802
6
+ metadata.gz: 353fd957d007ad7dd020f84a8866634a799e39ae94ba2ea65edf4eb09b6e8a354aa09961492a6cac0a530bcee5e904738df13bffc68c3b00f983348844adc074
7
+ data.tar.gz: 3993f70f671cb4097e83279bc4b5b68d510aa74a40ae320960d56b4a6b1aa537270893975c473cbf66a263496cea39398cd742b217af6b327b595edef84ea36b
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![Victor](/examples//15_victor_logo.svg)
1
+ ![Victor](/examples/16_victor_logo.svg)
2
2
 
3
3
  # Victor - Ruby SVG Image Builder
4
4
 
@@ -27,6 +27,7 @@ directly to SVG elements.
27
27
  * [CSS](#css)
28
28
  * [Tagless Elements](#tagless-elements)
29
29
  * [XML Encoding](#xml-encoding)
30
+ * [XML Newlines](#xml-newlines)
30
31
  * [DSL Syntax](#dsl-syntax)
31
32
  * [Using with Rails](#using-with-rails)
32
33
  * [Related Projects](#related-projects)
@@ -71,7 +72,7 @@ svg.save 'pacman'
71
72
 
72
73
  Output:
73
74
 
74
- [![pacman](https://cdn.rawgit.com/DannyBen/victor/master/examples/09_pacman.svg)](https://github.com/DannyBen/victor/blob/master/examples/09_pacman.rb)
75
+ [![pacman](https://cdn.rawgit.com/DannyBen/victor/master/examples/10_pacman.svg)](https://github.com/DannyBen/victor/blob/master/examples/10_pacman.rb)
75
76
 
76
77
 
77
78
  See the [examples] folder for several ruby scripts and their SVG output.
@@ -148,7 +149,7 @@ space delimited string:
148
149
 
149
150
  ```ruby
150
151
  svg.path d: ['M', 150, 0, 'L', 75, 200, 'L', 225, 200, 'Z']
151
- # => <path d="M 159 9 L 75 200 L 225 200 Z"/>
152
+ # => <path d="M 150 0 L 75 200 L 225 200 Z"/>
152
153
  ```
153
154
 
154
155
  For SVG elements that have an inner content - such as text - simply pass it as
@@ -185,7 +186,7 @@ svg.text "Victor", x: 40, y: 50, font_family: 'arial', font_weight: 'bold', font
185
186
 
186
187
  ### Composite SVG
187
188
 
188
- Victor also supports the abiliy to combine several smaller SVG objects into
189
+ Victor also supports the ability to combine several smaller SVG objects into
189
190
  one using the `<<` operator or the `#append` method.
190
191
 
191
192
  This operator expects to receive any object that responds to `#to_s` (can be another `SVG` object).
@@ -215,7 +216,7 @@ svg.save 'framed-troll'
215
216
 
216
217
  Output:
217
218
 
218
- [![troll](https://cdn.rawgit.com/DannyBen/victor/master/examples/13_composite_svg.svg)](https://cdn.rawgit.com/DannyBen/victor/master/examples/13_composite_svg.svg)
219
+ [![troll](https://cdn.rawgit.com/DannyBen/victor/master/examples/14_composite_svg.svg)](https://cdn.rawgit.com/DannyBen/victor/master/examples/14_composite_svg.svg)
219
220
 
220
221
  These two calls are identical:
221
222
 
@@ -244,8 +245,8 @@ end
244
245
  Another approach to a more modular SVG composition, would be to subclass
245
246
  `Victor::SVG`.
246
247
 
247
- See the [composite svg example](https://github.com/DannyBen/victor/tree/master/examples#13-composite-svg)
248
- or the [subclassing example](https://github.com/DannyBen/victor/tree/master/examples#14-subclassing)
248
+ See the [composite svg example](https://github.com/DannyBen/victor/tree/master/examples#14-composite-svg)
249
+ or the [subclassing example](https://github.com/DannyBen/victor/tree/master/examples#15-subclassing)
249
250
  for more details.
250
251
 
251
252
 
@@ -284,11 +285,40 @@ svg = Victor::SVG.new template: 'path/to/template.svg'
284
285
  See the [templates] folder for an understanding of how templates are
285
286
  structured.
286
287
 
288
+ Templates can also be provided when rendering or saving the output:
289
+
290
+ ```ruby
291
+ svg.save 'filename', template: :minimal
292
+ svg.render template: :minimal
293
+ ```
294
+
287
295
 
288
296
  ### CSS
289
297
 
290
- To add a CSS to your SVG, simply use the `css` command inside your `build`
291
- block, like this:
298
+ CSS gets a special treatment in `Victor::SVG`, with these objectives in mind:
299
+
300
+ - Hide implementation details (such as the need for a `CDATA` marker)
301
+ - Provide a DSL-like syntax for CSS rules
302
+
303
+ The `Victor::SVG` objects has a `css` property, which can contain either a
304
+ Hash or a String:
305
+
306
+ ```ruby
307
+ svg = Victor::SVG.new
308
+
309
+ svg.css = css_hash_or_string
310
+ # or without the equal sign:
311
+ svg.css css_hash_or_string
312
+
313
+ svg.build do
314
+ # ...
315
+ end
316
+ ```
317
+
318
+ This flexibility allows you to apply CSS in multiple ways. Below are some
319
+ examples.
320
+
321
+ #### Assigning CSS rules using the hash syntax
292
322
 
293
323
  ```ruby
294
324
  svg = Victor::SVG.new
@@ -304,7 +334,7 @@ svg.build do
304
334
  end
305
335
  ```
306
336
 
307
- You can also set CSS by providing a hash:
337
+ #### Assigning a full hash to the CSS property
308
338
 
309
339
  ```ruby
310
340
  svg.css = {
@@ -325,6 +355,13 @@ svg.css = {
325
355
  Underscore characters will be converted to dashes (`stroke_width` becomes
326
356
  `stroke-width`).
327
357
 
358
+ #### Importing CSS from an external file
359
+
360
+ ```ruby
361
+ svg.css = File.read 'styles.css'
362
+ ```
363
+
364
+ #### CSS `@import` directives
328
365
 
329
366
  If you need to add CSS statements , like `@import`, use the following syntax:
330
367
 
@@ -335,11 +372,13 @@ css['@import'] = [
335
372
  ]
336
373
  ```
337
374
 
338
- When the value of the CSS attribute is an array, Victor will simply use
339
- the values of the array and prefix each of them with the key, so the above
340
- will result in two `@import url(...)` rows.
375
+ This is achieved thanks to the fact that when Victor encounters an array
376
+ in the CSS hash, it will prefix each of the array elements with the hash
377
+ key, so the above will result in two `@import url(...)` rows.
341
378
 
342
- See the [custom fonts example](https://github.com/DannyBen/victor/tree/master/examples#12-custom-fonts).
379
+ See the [css example](https://github.com/DannyBen/victor/tree/master/examples#08-css),
380
+ [css string example](https://github.com/DannyBen/victor/tree/master/examples#09-css-string),
381
+ or the [custom fonts example](https://github.com/DannyBen/victor/tree/master/examples#13-custom-fonts).
343
382
 
344
383
 
345
384
  ### Tagless Elements
@@ -368,7 +407,7 @@ svg.build do
368
407
  end
369
408
  ```
370
409
 
371
- See the [targless elements example](https://github.com/DannyBen/victor/tree/master/examples#16-tagless-elements).
410
+ See the [tagless elements example](https://github.com/DannyBen/victor/tree/master/examples#17-tagless-elements).
372
411
 
373
412
 
374
413
  ### XML Encoding
@@ -391,7 +430,25 @@ end
391
430
  # <text>Ben & Jerry's</text>
392
431
  ```
393
432
 
394
- See the [xml encoding example](https://github.com/DannyBen/victor/tree/master/examples#17-xml-encoding).
433
+ See the [xml encoding example](https://github.com/DannyBen/victor/tree/master/examples#18-xml-encoding).
434
+
435
+
436
+ ### XML Newlines
437
+
438
+ By default, the generated SVGs will have a newline glue between the elements.
439
+ You can change this (for example, to an empty string) if the default newlines
440
+ are not appropriate for your use case.
441
+
442
+ ```ruby
443
+ svg = Victor::SVG.new glue: ''
444
+ ```
445
+
446
+ The glue can also be provided when rendering or saving the output:
447
+
448
+ ```ruby
449
+ svg.save 'filename', glue: ''
450
+ svg.render glue: ''
451
+ ```
395
452
 
396
453
  ### DSL Syntax
397
454
 
@@ -417,7 +474,7 @@ puts render
417
474
  save 'output.svg'
418
475
  ```
419
476
 
420
- See the [dsl example](https://github.com/DannyBen/victor/tree/master/examples#18-dsl).
477
+ See the [dsl example](https://github.com/DannyBen/victor/tree/master/examples#19-dsl).
421
478
 
422
479
  ## Using with Rails
423
480
 
@@ -435,6 +492,13 @@ Victor Ruby scripts.
435
492
 
436
493
  A Victor playground that works in the browser.
437
494
 
495
+ ### [Minichart][minichart]
496
+
497
+ A Ruby gem that uses Victor to generate SVG charts
498
+
499
+ [![Minichart](assets/minichart.svg)][minichart]
500
+
501
+
438
502
  ### [Icodi][icodi]
439
503
 
440
504
  A Ruby gem that uses Victor to generate consistent random icon
@@ -453,6 +517,7 @@ to contribute, feel free to [open an issue][issues].
453
517
  [examples]: https://github.com/DannyBen/victor/tree/master/examples#examples
454
518
  [templates]: https://github.com/DannyBen/victor/tree/master/lib/victor/templates
455
519
  [icodi]: https://github.com/DannyBen/icodi
520
+ [minichart]: https://github.com/DannyBen/minichart
456
521
  [victor-opal]: https://kuboon.github.io/victor-opal/
457
522
  [victor-cli]: https://github.com/DannyBen/victor-cli
458
523
  [issues]: https://github.com/DannyBen/victor/issues
data/lib/victor/dsl.rb CHANGED
@@ -3,7 +3,7 @@ require 'forwardable'
3
3
  module Victor
4
4
  module DSL
5
5
  extend Forwardable
6
- def_delegators :svg, :setup, :build, :save, :render, :append, :element
6
+ def_delegators :svg, :setup, :build, :save, :render, :append, :element, :css
7
7
 
8
8
  def svg
9
9
  @svg ||= Victor::SVG.new
@@ -1,13 +1,12 @@
1
1
  module Victor
2
2
 
3
3
  class SVGBase
4
- attr_accessor :template, :css
4
+ attr_accessor :template, :glue
5
5
  attr_reader :content, :svg_attributes
6
6
 
7
7
  def initialize(attributes = nil, &block)
8
8
  setup attributes
9
9
  @content = []
10
- @css = {}
11
10
  build &block if block_given?
12
11
  end
13
12
 
@@ -21,11 +20,11 @@ module Victor
21
20
  attributes[:width] ||= "100%"
22
21
  attributes[:height] ||= "100%"
23
22
 
24
- if attributes[:template]
25
- @template = attributes.delete :template
26
- elsif !@template
27
- @template = :default
28
- end
23
+ @template = attributes[:template] || @template || :default
24
+ @glue = attributes[:glue] || @glue || "\n"
25
+
26
+ attributes.delete :template
27
+ attributes.delete :glue
29
28
 
30
29
  @svg_attributes = Attributes.new attributes
31
30
  end
@@ -63,24 +62,36 @@ module Victor
63
62
  end
64
63
  end
65
64
 
66
- def render
67
- css_string = CSS.new css
65
+ def css(defs = nil)
66
+ @css ||= {}
67
+ @css = defs if defs
68
+ @css
69
+ end
70
+
71
+ def css=(defs)
72
+ @css = defs
73
+ end
74
+
75
+ def render(template: nil, glue: nil)
76
+ @template = template if template
77
+ @glue = glue if glue
78
+ css_handler = CSS.new css
68
79
 
69
80
  svg_template % {
70
- css: css_string,
71
- style: css_string.render,
81
+ css: css_handler,
82
+ style: css_handler.render,
72
83
  attributes: svg_attributes,
73
- content: content.join("\n")
84
+ content: to_s
74
85
  }
75
86
  end
76
87
 
77
88
  def to_s
78
- content.join "\n"
89
+ content.join glue
79
90
  end
80
91
 
81
- def save(filename)
92
+ def save(filename, template: nil, glue: nil)
82
93
  filename = "#{filename}.svg" unless filename =~ /\..{2,4}$/
83
- File.write filename, render
94
+ File.write filename, render(template: template, glue: glue)
84
95
  end
85
96
 
86
97
  protected
@@ -1,3 +1,3 @@
1
1
  module Victor
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: victor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-05 00:00:00.000000000 Z
11
+ date: 2022-07-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Build SVG images with ease
14
14
  email: db@dannyben.com
@@ -40,14 +40,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
- version: 2.3.0
43
+ version: 2.6.0
44
44
  required_rubygems_version: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ">="
47
47
  - !ruby/object:Gem::Version
48
48
  version: '0'
49
49
  requirements: []
50
- rubygems_version: 3.0.3
50
+ rubygems_version: 3.3.14
51
51
  signing_key:
52
52
  specification_version: 4
53
53
  summary: SVG Builder