victor 0.2.1 → 0.2.2

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
- SHA1:
3
- metadata.gz: 6c9b02320dd3fe0061bf8d703b4a50b9c5ef1928
4
- data.tar.gz: 6ab896856bff9033cb81b0593520c02fc508cd3c
2
+ SHA256:
3
+ metadata.gz: 8084c65a136f58070a350341405ef047bc7b3dfd282fec474223598ee7889f12
4
+ data.tar.gz: 2112d671dadbe7c6e7f1f4a78adad9c6cd24512ba3bdddcd641574e2fd218ad4
5
5
  SHA512:
6
- metadata.gz: bbb2d3dbb6567ccdf49e5aefe3719c44f2ebb347ada557b9ae3ef39a3621a8b6dfffbe533879b4d2556d44e28aad038905c940452b67f507e9f72ed134eb2cde
7
- data.tar.gz: c611c5b2923a98c731c08371358cda94559fbea12e290dc38a2368d864ada2813e3b434df305f0bbd650f0ebfff54edd8f692bfcf16f14b1e4bc8794321ace9c
6
+ metadata.gz: 7c3dc90b9f50060eeb447d1bbe5d54badcf5fff046ee91cea74cc487e0c58f37420e208d1d41b307bc80c4b67a3719f490012417a2deef76d02ba71f144599ab
7
+ data.tar.gz: 6665e0d722a7c4ffb70497ea884cf83223a5327fe3e68b498b1ea086b0fd9145377f41f7584a62c351886a6e2dfd4c1e834b68cc0243e5af71c24fbca7037318
data/README.md CHANGED
@@ -1,16 +1,32 @@
1
1
  Victor - Ruby SVG Image Builder
2
2
  ==================================================
3
3
 
4
- [![Gem](https://img.shields.io/gem/v/victor.svg?style=flat-square)](https://rubygems.org/gems/victor)
5
- [![Travis](https://img.shields.io/travis/DannyBen/victor.svg?style=flat-square)](https://travis-ci.org/DannyBen/victor)
6
- [![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/victor.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/victor)
7
- [![Gemnasium](https://img.shields.io/gemnasium/DannyBen/victor.svg?style=flat-square)](https://gemnasium.com/DannyBen/victor)
4
+ [![Gem Version](https://badge.fury.io/rb/victor.svg)](https://badge.fury.io/rb/victor)
5
+ [![Build Status](https://travis-ci.com/DannyBen/victor.svg?branch=master)](https://travis-ci.com/DannyBen/victor)
6
+ [![Code Quality](https://api.codacy.com/project/badge/Grade/a502c262875643eabb01a43f7f5131ff)](https://www.codacy.com/app/db/victor?utm_source=github.com&utm_medium=referral&utm_content=DannyBen/victor&utm_campaign=Badge_Grade)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/85cc05c219d6d233ab78/maintainability)](https://codeclimate.com/github/DannyBen/victor/maintainability)
8
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/85cc05c219d6d233ab78/test_coverage)](https://codeclimate.com/github/DannyBen/victor/test_coverage)
8
9
 
9
10
  ---
10
11
 
11
12
  Victor is a direct Ruby-to-SVG builder. All method calls are converted
12
13
  directly to SVG elements.
13
14
 
15
+ ![Demo](/animated.gif)
16
+
17
+ ---
18
+
19
+ Table of Contents
20
+ --------------------------------------------------
21
+
22
+ * [Install](#install)
23
+ * [Examples](#examples)
24
+ * [Usage](#usage)
25
+ * [Composite SVG](#composite-svg)
26
+ * [Saving the Output](#saving-the-output)
27
+ * [SVG Templates](#svg-templates)
28
+ * [CSS](#css)
29
+
14
30
  ---
15
31
 
16
32
  Install
@@ -155,6 +171,47 @@ svg.text "Victor", x: 40, y: 50, font_family: 'arial', font_weight: 'bold', font
155
171
  # </text>
156
172
  ```
157
173
 
174
+ Composite SVG
175
+ --------------------------------------------------
176
+ Victor also supports the abiliy to combine several smaller SVG objects into
177
+ one using the `<<` operator. This operator expects to receive any object
178
+ that responds to `#to_s` (can be another `SVG` object).
179
+
180
+ ```ruby
181
+ require 'victor'
182
+ include Victor
183
+
184
+ # Create a reusable SVG object
185
+ frame = SVG.new
186
+ frame.rect x: 0, y: 0, width: 100, height: 100, fill: '#336'
187
+ frame.rect x: 10, y: 10, width: 80, height: 80, fill: '#fff'
188
+
189
+ # ... and another
190
+ troll = SVG.new
191
+ troll.circle cx: 50, cy: 60, r: 24, fill: 'yellow'
192
+ troll.polygon points: %w[24,50 50,14 76,54], fill: 'red'
193
+
194
+ # Combine objects into a single image
195
+ svg = SVG.new viewBox: '0 0 100 100'
196
+ svg << frame
197
+ svg << troll
198
+
199
+ # ... and save it
200
+ svg.save 'framed-troll'
201
+ ```
202
+
203
+ Output:
204
+
205
+ [![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)
206
+
207
+ Another approach to a more modular SVG composition, would be to subclass
208
+ `Victor::SVG`.
209
+
210
+ See the [composite svg example](https://github.com/DannyBen/victor/tree/master/examples#13-composite-svg)
211
+ or the [subclassing example](https://github.com/DannyBen/victor/tree/master/examples#14-subclassing)
212
+ for more details.
213
+
214
+
158
215
  Saving the Output
159
216
  --------------------------------------------------
160
217
 
@@ -248,7 +305,7 @@ When the value of the CSS attribute is an array, Victor will simply use
248
305
  the values of the array and prefix each of them with the key, so the above
249
306
  will result in two `@import url(...)` rows.
250
307
 
251
- See the [custom fonts example](https://github.com/DannyBen/victor/tree/master/examples#12-custom-fonts)
308
+ See the [custom fonts example](https://github.com/DannyBen/victor/tree/master/examples#12-custom-fonts).
252
309
 
253
310
 
254
311
  ---
@@ -1,48 +1,48 @@
1
-
2
-
3
- module Victor
4
-
5
- class Attributes
6
- attr_reader :attributes
7
-
8
- def initialize(attributes={})
9
- @attributes = attributes
10
- end
11
-
12
- def to_s
13
- mapped = attributes.map do |key, value|
14
- key = key.to_s.tr '_', '-'
15
-
16
- if value.is_a? Hash
17
- style = Attributes.new(value).to_style
18
- "#{key}=\"#{style}\""
19
- elsif value.is_a? Array
20
- "#{key}=\"#{value.join ' '}\""
21
- else
22
- "#{key}=\"#{value}\""
23
- end
24
- end
25
-
26
- mapped.join ' '
27
- end
28
-
29
- def to_style
30
- mapped = attributes.map do |key, value|
31
- key = key.to_s.tr '_', '-'
32
- "#{key}:#{value}"
33
- end
34
-
35
- mapped.join '; '
36
- end
37
-
38
- def [](key)
39
- attributes[key]
40
- end
41
-
42
- def []=(key, value)
43
- attributes[key] = value
44
- end
45
-
46
- end
47
-
1
+ module Victor
2
+
3
+ # Handles conversion from a Hash of attributes, to an XML string or
4
+ # a CSS string.
5
+ class Attributes
6
+ attr_reader :attributes
7
+
8
+ def initialize(attributes={})
9
+ @attributes = attributes
10
+ end
11
+
12
+ def to_s
13
+ mapped = attributes.map do |key, value|
14
+ key = key.to_s.tr '_', '-'
15
+
16
+ if value.is_a? Hash
17
+ style = Attributes.new(value).to_style
18
+ "#{key}=\"#{style}\""
19
+ elsif value.is_a? Array
20
+ "#{key}=\"#{value.join ' '}\""
21
+ else
22
+ "#{key}=\"#{value}\""
23
+ end
24
+ end
25
+
26
+ mapped.join ' '
27
+ end
28
+
29
+ def to_style
30
+ mapped = attributes.map do |key, value|
31
+ key = key.to_s.tr '_', '-'
32
+ "#{key}:#{value}"
33
+ end
34
+
35
+ mapped.join '; '
36
+ end
37
+
38
+ def [](key)
39
+ attributes[key]
40
+ end
41
+
42
+ def []=(key, value)
43
+ attributes[key] = value
44
+ end
45
+
46
+ end
47
+
48
48
  end
data/lib/victor/css.rb CHANGED
@@ -1,39 +1,50 @@
1
- module Victor
2
-
3
- class CSS
4
- attr_reader :attributes
5
-
6
- def initialize(attributes={})
7
- @attributes = attributes
8
- end
9
-
10
- def to_s
11
- convert_hash attributes
12
- end
13
-
14
- private
15
-
16
- def convert_hash(hash, indent=2)
17
- return hash unless hash.is_a? Hash
18
-
19
- result = []
20
- hash.each do |key, value|
21
- key = key.to_s.tr '_', '-'
22
- if value.is_a? Hash
23
- result.push " " * indent + "#{key} {"
24
- result.push convert_hash(value, indent+2)
25
- result.push " " * indent + "}"
26
- elsif value.is_a? Array
27
- value.each do |row|
28
- result.push " " * indent + "#{key} #{row};"
29
- end
30
- else
31
- result.push " " * indent + "#{key}: #{value};"
32
- end
33
- end
34
-
35
- result.join "\n"
36
- end
37
- end
38
-
1
+ module Victor
2
+
3
+ # Converts a Hash to a CSS string
4
+ class CSS
5
+ attr_reader :attributes
6
+
7
+ def initialize(attributes={})
8
+ @attributes = attributes
9
+ end
10
+
11
+ def to_s
12
+ convert_hash attributes
13
+ end
14
+
15
+ private
16
+
17
+ def convert_hash(hash, indent=2)
18
+ return hash unless hash.is_a? Hash
19
+
20
+ result = []
21
+ hash.each do |key, value|
22
+ key = key.to_s.tr '_', '-'
23
+ result += css_block(key, value, indent)
24
+ end
25
+
26
+ result.join "\n"
27
+ end
28
+
29
+ def css_block(key, value, indent)
30
+ result = []
31
+
32
+ my_indent = " " * indent
33
+
34
+ if value.is_a? Hash
35
+ result.push "#{my_indent}#{key} {"
36
+ result.push convert_hash(value, indent+2)
37
+ result.push "#{my_indent}}"
38
+ elsif value.is_a? Array
39
+ value.each do |row|
40
+ result.push "#{my_indent}#{key} #{row};"
41
+ end
42
+ else
43
+ result.push "#{my_indent}#{key}: #{value};"
44
+ end
45
+
46
+ result
47
+ end
48
+ end
49
+
39
50
  end
data/lib/victor/svg.rb CHANGED
@@ -1,73 +1,79 @@
1
- module Victor
2
-
3
- class SVG
4
- attr_accessor :template, :css
5
- attr_reader :content, :svg_attributes
6
-
7
- def initialize(attributes={})
8
- @template = attributes.delete(:template) || :default
9
- @svg_attributes = Attributes.new attributes
10
- svg_attributes[:width] ||= "100%"
11
- svg_attributes[:height] ||= "100%"
12
- @content = []
13
- @css = {}
14
- end
15
-
16
- def method_missing(method_sym, *arguments, &block)
17
- element method_sym, *arguments, &block
18
- end
19
-
20
- def build(&block)
21
- self.instance_eval(&block)
22
- end
23
-
24
- def element(name, value=nil, attributes={}, &_block)
25
- if value.is_a? Hash
26
- attributes = value
27
- value = nil
28
- end
29
-
30
- attributes = Attributes.new attributes
31
-
32
- if block_given? || value
33
- content.push "<#{name} #{attributes}".strip + ">"
34
- value ? content.push(value) : yield
35
- content.push "</#{name}>"
36
- else
37
- content.push "<#{name} #{attributes}/>"
38
- end
39
- end
40
-
41
- def render
42
- svg_template % {
43
- css: CSS.new(css),
44
- attributes: svg_attributes,
45
- content: content.join("\n")
46
- }
47
- end
48
-
49
- def to_s
50
- content.join "\n"
51
- end
52
-
53
- def save(filename)
54
- filename = "#{filename}.svg" unless filename =~ /\..{2,4}$/
55
- File.write filename, render
56
- end
57
-
58
- private
59
-
60
- def svg_template
61
- File.read template_path
62
- end
63
-
64
- def template_path
65
- if template.is_a? Symbol
66
- File.join File.dirname(__FILE__), 'templates', "#{template}.svg"
67
- else
68
- template
69
- end
70
- end
71
- end
72
-
1
+ module Victor
2
+
3
+ # This is the primary Victor class. It handles all the conversion from
4
+ # ruby to SVG using {#method_missing}
5
+ class SVG
6
+ attr_accessor :template, :css
7
+ attr_reader :content, :svg_attributes
8
+
9
+ def initialize(attributes={})
10
+ @template = attributes.delete(:template) || :default
11
+ @svg_attributes = Attributes.new attributes
12
+ svg_attributes[:width] ||= "100%"
13
+ svg_attributes[:height] ||= "100%"
14
+ @content = []
15
+ @css = {}
16
+ end
17
+
18
+ def method_missing(method_sym, *arguments, &block)
19
+ element method_sym, *arguments, &block
20
+ end
21
+
22
+ def <<(additional_content)
23
+ content.push additional_content.to_s
24
+ end
25
+
26
+ def build(&block)
27
+ self.instance_eval(&block)
28
+ end
29
+
30
+ def element(name, value=nil, attributes={}, &_block)
31
+ if value.is_a? Hash
32
+ attributes = value
33
+ value = nil
34
+ end
35
+
36
+ attributes = Attributes.new attributes
37
+
38
+ if block_given? || value
39
+ content.push "<#{name} #{attributes}".strip + ">"
40
+ value ? content.push(value) : yield
41
+ content.push "</#{name}>"
42
+ else
43
+ content.push "<#{name} #{attributes}/>"
44
+ end
45
+ end
46
+
47
+ def render
48
+ svg_template % {
49
+ css: CSS.new(css),
50
+ attributes: svg_attributes,
51
+ content: content.join("\n")
52
+ }
53
+ end
54
+
55
+ def to_s
56
+ content.join "\n"
57
+ end
58
+
59
+ def save(filename)
60
+ filename = "#{filename}.svg" unless filename =~ /\..{2,4}$/
61
+ File.write filename, render
62
+ end
63
+
64
+ private
65
+
66
+ def svg_template
67
+ File.read template_path
68
+ end
69
+
70
+ def template_path
71
+ if template.is_a? Symbol
72
+ File.join File.dirname(__FILE__), 'templates', "#{template}.svg"
73
+ else
74
+ template
75
+ end
76
+ end
77
+ end
78
+
73
79
  end
@@ -1,3 +1,3 @@
1
1
  module Victor
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
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.2.1
4
+ version: 0.2.2
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: 2017-02-17 00:00:00.000000000 Z
11
+ date: 2018-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: runfile
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.7'
19
+ version: '0.10'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.7'
26
+ version: '0.10'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: runfile-tasks
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,42 +44,42 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.4'
47
+ version: '3.6'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.4'
54
+ version: '3.6'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: simplecov
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.11'
61
+ version: '0.16'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.11'
68
+ version: '0.16'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: filewatcher
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0.5'
75
+ version: '1.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0.5'
82
+ version: '1.0'
83
83
  description: Build SVG images with ease
84
84
  email: db@dannyben.com
85
85
  executables: []
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  version: '0'
115
115
  requirements: []
116
116
  rubyforge_project:
117
- rubygems_version: 2.6.6
117
+ rubygems_version: 2.7.6
118
118
  signing_key:
119
119
  specification_version: 4
120
120
  summary: SVG Builder