victor 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/README.md +55 -8
- data/lib/victor/svg.rb +27 -15
- data/lib/victor/{template.svg → templates/default.svg} +3 -2
- data/lib/victor/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54f32fa0b60b2a0c70bb9a6d52ad88757d6e4701
|
4
|
+
data.tar.gz: 3c1b19ee5f21bf29966f10ffefc9a13344490956
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2ae86c7705f26d3f1945eae9c263b94aae563de16b4e597f41a9e5a055e27784cfa0806599a68d5778f514cc9d087277ad5db774116b86e98b1b3a5a89c4792
|
7
|
+
data.tar.gz: ed055f13b4aeff0a56db603e067704555eca6f6f1a29f9440571d42cd56f0667bba0c43294edc4876388de3d9a0c5990d31665a635131c2fc285ee2e635fafdf
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ Victor - Ruby SVG Image Builder
|
|
8
8
|
|
9
9
|
---
|
10
10
|
|
11
|
-
Victor is a direct Ruby
|
11
|
+
Victor is a direct Ruby-to-SVG builder. All method calls are converted
|
12
12
|
directly to SVG elements.
|
13
13
|
|
14
14
|
---
|
@@ -29,18 +29,32 @@ gem 'victor'
|
|
29
29
|
Examples
|
30
30
|
--------------------------------------------------
|
31
31
|
|
32
|
-
See the examples folder for several ruby scripts and their SVG output.
|
32
|
+
See the [examples] folder for several ruby scripts and their SVG output.
|
33
33
|
|
34
34
|
|
35
35
|
Usage
|
36
36
|
--------------------------------------------------
|
37
37
|
|
38
|
-
|
38
|
+
Initialize your SVG image:
|
39
39
|
|
40
40
|
```ruby
|
41
41
|
require 'victor'
|
42
42
|
svg = SVG.new
|
43
|
+
```
|
44
|
+
|
45
|
+
Any option you provide to `SVG.new` will be added as an attribute to the
|
46
|
+
main `<svg>` element. By default, `height` and `width` are set to 100%.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
svg = SVG.new width: '100%', height: '100%'
|
50
|
+
# same as just SVG.new
|
51
|
+
|
52
|
+
svg = SVG.new width: '100%', height: '100%', viewBox: "0 0 200 100"
|
53
|
+
```
|
43
54
|
|
55
|
+
Victor uses a single method (`element`) to generate all SVG elements:
|
56
|
+
|
57
|
+
```ruby
|
44
58
|
svg.element :rect, x: 2, y: 2, width: 200, height: 200
|
45
59
|
# => <rect x="2" y="2" width="200" height="200"/>
|
46
60
|
```
|
@@ -49,7 +63,6 @@ But you can omit it. Calls to any other method, will be delegated to the
|
|
49
63
|
`element` method, so normal usage looks more like this:
|
50
64
|
|
51
65
|
```ruby
|
52
|
-
# this will generate the exact same result
|
53
66
|
svg.rect x: 2, y: 2, width: 200, height: 200
|
54
67
|
# => <rect x="2" y="2" width="200" height="200"/>
|
55
68
|
```
|
@@ -58,12 +71,40 @@ You can use the `build` method, to generate the SVG with a block
|
|
58
71
|
|
59
72
|
```ruby
|
60
73
|
svg.build do
|
61
|
-
rect x: 0, y: 0, width: 100, height: 100,
|
62
|
-
rect x: 20, y: 20, width: 60, height: 60,
|
74
|
+
rect x: 0, y: 0, width: 100, height: 100, fill: '#ccc'
|
75
|
+
rect x: 20, y: 20, width: 60, height: 60, fill: '#f99'
|
63
76
|
end
|
64
77
|
```
|
65
78
|
|
66
|
-
|
79
|
+
If the value of an attribute is a hash, it will be converted to a
|
80
|
+
style-compatible string:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
svg.rect x: 0, y: 0, width: 100, height: 100, style: { stroke: '#ccc', fill: 'red' }
|
84
|
+
# => <rect x=0 y=0 width=100 height=100 style="stroke:#ccc; fill:red"/>
|
85
|
+
```
|
86
|
+
|
87
|
+
For SVG elements that have an inner content - such as text - simply pass it as
|
88
|
+
the first argument:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
svg.text "Victor", x: 40, y: 50
|
92
|
+
# => <text x="40" y="50">Victor</text>
|
93
|
+
```
|
94
|
+
|
95
|
+
Underscores in attribute names are converted to dashes:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
svg.text "Victor", x: 40, y: 50, font_family: 'arial', font_weight: 'bold', font_size: 40
|
99
|
+
# => <text x="40" y="50" font-family="arial" font-weight="bold" font-size="40">
|
100
|
+
# Victor
|
101
|
+
# </text>
|
102
|
+
```
|
103
|
+
|
104
|
+
Saving the Output
|
105
|
+
--------------------------------------------------
|
106
|
+
|
107
|
+
Generate the full SVG to a string with `render`:
|
67
108
|
|
68
109
|
```ruby
|
69
110
|
result = svg.render
|
@@ -72,5 +113,11 @@ result = svg.render
|
|
72
113
|
Or, save it to a file with `save`:
|
73
114
|
|
74
115
|
```ruby
|
75
|
-
svg.save 'filename'
|
116
|
+
svg.save 'filename'
|
117
|
+
# the '.svg' extension is optional
|
76
118
|
```
|
119
|
+
|
120
|
+
|
121
|
+
---
|
122
|
+
|
123
|
+
[examples]: https://github.com/DannyBen/victor/tree/master/examples#examples
|
data/lib/victor/svg.rb
CHANGED
@@ -3,32 +3,43 @@
|
|
3
3
|
module Victor
|
4
4
|
|
5
5
|
class SVG
|
6
|
-
attr_accessor :
|
6
|
+
attr_accessor :attributes
|
7
7
|
attr_reader :content
|
8
8
|
|
9
|
-
def initialize(
|
10
|
-
@
|
11
|
-
|
9
|
+
def initialize(attributes={})
|
10
|
+
@attributes = attributes
|
11
|
+
attributes[:width] ||= "100%"
|
12
|
+
attributes[:height] ||= "100%"
|
12
13
|
@content = []
|
13
14
|
end
|
14
15
|
|
15
16
|
def method_missing(method_sym, *arguments, &block)
|
16
|
-
element method_sym, *arguments
|
17
|
+
element method_sym, *arguments, &block
|
17
18
|
end
|
18
19
|
|
19
20
|
def build(&block)
|
20
|
-
self.instance_eval
|
21
|
+
self.instance_eval(&block)
|
21
22
|
end
|
22
23
|
|
23
|
-
def element(name, attributes={}, &
|
24
|
+
def element(name, value=nil, attributes={}, &_block)
|
25
|
+
if value.is_a? Hash
|
26
|
+
attributes = value
|
27
|
+
value = nil
|
28
|
+
end
|
29
|
+
|
24
30
|
xml_attributes = expand attributes
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
|
32
|
+
if block_given? || value
|
33
|
+
content.push "<#{name} #{xml_attributes}".strip + ">"
|
34
|
+
value ? content.push(value) : yield
|
35
|
+
content.push "</#{name}>"
|
36
|
+
else
|
37
|
+
content.push "<#{name} #{xml_attributes}/>"
|
38
|
+
end
|
28
39
|
end
|
29
40
|
|
30
41
|
def render
|
31
|
-
template % {
|
42
|
+
template % { attributes: expand(attributes), content: content.join("\n") }
|
32
43
|
end
|
33
44
|
|
34
45
|
def save(filename)
|
@@ -40,18 +51,19 @@ module Victor
|
|
40
51
|
|
41
52
|
def expand(attributes={})
|
42
53
|
mapped = attributes.map do |key, value|
|
54
|
+
key = key.to_s.tr '_', '-'
|
43
55
|
value.is_a?(Hash) ? inner_expand(key, value) : "#{key}=\"#{value}\""
|
44
56
|
end
|
45
57
|
|
46
58
|
mapped.join ' '
|
47
59
|
end
|
48
60
|
|
49
|
-
def inner_expand(
|
61
|
+
def inner_expand(name, attributes)
|
50
62
|
mapped = attributes.map do |key, value|
|
51
63
|
"#{key}:#{value}"
|
52
64
|
end
|
53
65
|
|
54
|
-
"#{
|
66
|
+
"#{name}=\"#{mapped.join '; '}\""
|
55
67
|
end
|
56
68
|
|
57
69
|
def template
|
@@ -59,11 +71,11 @@ module Victor
|
|
59
71
|
end
|
60
72
|
|
61
73
|
def template_path
|
62
|
-
File.join File.dirname(__FILE__), template_file
|
74
|
+
File.join File.dirname(__FILE__), 'templates', template_file
|
63
75
|
end
|
64
76
|
|
65
77
|
def template_file
|
66
|
-
'
|
78
|
+
'default.svg'
|
67
79
|
end
|
68
80
|
end
|
69
81
|
|
@@ -1,5 +1,6 @@
|
|
1
1
|
<?xml version="1.0" standalone="no"?>
|
2
2
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
3
|
-
|
4
|
-
|
3
|
+
|
4
|
+
<svg %{attributes} xmlns="http://www.w3.org/2000/svg">
|
5
|
+
%{content}
|
5
6
|
</svg>
|
data/lib/victor/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: victor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
@@ -89,7 +89,7 @@ files:
|
|
89
89
|
- README.md
|
90
90
|
- lib/victor.rb
|
91
91
|
- lib/victor/svg.rb
|
92
|
-
- lib/victor/
|
92
|
+
- lib/victor/templates/default.svg
|
93
93
|
- lib/victor/version.rb
|
94
94
|
homepage: https://github.com/DannyBen/victor
|
95
95
|
licenses:
|