victor 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +54 -0
- data/lib/victor.rb +2 -0
- data/lib/victor/attributes.rb +47 -0
- data/lib/victor/css.rb +27 -0
- data/lib/victor/svg.rb +21 -32
- data/lib/victor/templates/default.svg +8 -0
- data/lib/victor/templates/html.svg +10 -0
- data/lib/victor/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e578183630315066cfac8073fe7a95df4b200a2
|
4
|
+
data.tar.gz: 12cd33f3ad1dbbf877f7fd84c56bc26c05c15024
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16ff14fb0e35c9d866d3ceb69750aa2220cfc4c3ceb28760c44da377003b8e8ba8667818582a375a2a7c8dbff08527e0db0e71d8f38d39e43b596a6b57e3c535
|
7
|
+
data.tar.gz: 96f4949a8103de5e5f535530b43e033fc8cc29088bc7f967baa8e4e15c45ac1a2a17396122280ebf4e59f3c0ae0c3e3ed9449195bdbc57d37ef0f247418322c9
|
data/README.md
CHANGED
@@ -84,6 +84,15 @@ svg.rect x: 0, y: 0, width: 100, height: 100, style: { stroke: '#ccc', fill: 're
|
|
84
84
|
# => <rect x=0 y=0 width=100 height=100 style="stroke:#ccc; fill:red"/>
|
85
85
|
```
|
86
86
|
|
87
|
+
If the value of an attribute is an array, it will be converted to a
|
88
|
+
space delimited string:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
svg.path ['M', 150, 0, 'L', 75, 200, 'L', 225, 200, 'Z']
|
92
|
+
# => <rect x=0 y=0 width=100 height=100 style="stroke:#ccc; fill:red"/>
|
93
|
+
```
|
94
|
+
|
95
|
+
|
87
96
|
For SVG elements that have an inner content - such as text - simply pass it as
|
88
97
|
the first argument:
|
89
98
|
|
@@ -117,7 +126,52 @@ svg.save 'filename'
|
|
117
126
|
# the '.svg' extension is optional
|
118
127
|
```
|
119
128
|
|
129
|
+
SVG Templates
|
130
|
+
--------------------------------------------------
|
131
|
+
|
132
|
+
The `:default` SVG template is designed to be a full XML document (i.e.,
|
133
|
+
a standalone SVG image). If you wish to use the output as an SVG element
|
134
|
+
inside HTML, you can change the SVG template:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
svg = SVG.new template: :html
|
138
|
+
# accepts :html, :default or a filename
|
139
|
+
```
|
140
|
+
|
141
|
+
You can also point it to any other template file:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
svg = SVG.new template: 'path/to/template.svg'
|
145
|
+
```
|
146
|
+
|
147
|
+
See the [templates] folder for an understanding of how templates are
|
148
|
+
structured.
|
149
|
+
|
150
|
+
|
151
|
+
CSS
|
152
|
+
--------------------------------------------------
|
153
|
+
|
154
|
+
To add a CSS to your SVG, simply use the `css` command inside your `build`
|
155
|
+
block, like this:
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
svg = SVG.new
|
159
|
+
|
160
|
+
svg.build do
|
161
|
+
css['.main'] = {
|
162
|
+
stroke: "green",
|
163
|
+
stroke_width: 2,
|
164
|
+
fill: "yellow"
|
165
|
+
}
|
166
|
+
|
167
|
+
circle cx: 35, cy: 35, r: 20, class: 'main'
|
168
|
+
end
|
169
|
+
```
|
170
|
+
|
171
|
+
Underscore characters will be converted to dashes (`stroke_width` becomes
|
172
|
+
`stroke-width`).
|
120
173
|
|
121
174
|
---
|
122
175
|
|
123
176
|
[examples]: https://github.com/DannyBen/victor/tree/master/examples#examples
|
177
|
+
[templates]: https://github.com/DannyBen/victor/tree/master/lib/victor/templates
|
data/lib/victor.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
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}:#{value}"
|
32
|
+
end
|
33
|
+
|
34
|
+
mapped.join '; '
|
35
|
+
end
|
36
|
+
|
37
|
+
def [](key)
|
38
|
+
attributes[key]
|
39
|
+
end
|
40
|
+
|
41
|
+
def []=(key, value)
|
42
|
+
attributes[key] = value
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/lib/victor/css.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Victor
|
4
|
+
|
5
|
+
class CSS
|
6
|
+
attr_reader :attributes
|
7
|
+
|
8
|
+
def initialize(attributes={})
|
9
|
+
@attributes = attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
result = []
|
14
|
+
attributes.each do |selector, styles|
|
15
|
+
result.push " #{selector} {"
|
16
|
+
styles.each do |key, value|
|
17
|
+
key = key.to_s.tr '_', '-'
|
18
|
+
result.push " #{key}: #{value};"
|
19
|
+
end
|
20
|
+
result.push " }"
|
21
|
+
end
|
22
|
+
|
23
|
+
result.join "\n"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/victor/svg.rb
CHANGED
@@ -3,14 +3,16 @@
|
|
3
3
|
module Victor
|
4
4
|
|
5
5
|
class SVG
|
6
|
-
attr_accessor :
|
7
|
-
attr_reader :content
|
6
|
+
attr_accessor :template, :css
|
7
|
+
attr_reader :content, :svg_attributes
|
8
8
|
|
9
9
|
def initialize(attributes={})
|
10
|
-
@
|
11
|
-
|
12
|
-
|
10
|
+
@template = attributes.delete(:template) || :default
|
11
|
+
@svg_attributes = Attributes.new attributes
|
12
|
+
svg_attributes[:width] ||= "100%"
|
13
|
+
svg_attributes[:height] ||= "100%"
|
13
14
|
@content = []
|
15
|
+
@css = {}
|
14
16
|
end
|
15
17
|
|
16
18
|
def method_missing(method_sym, *arguments, &block)
|
@@ -27,19 +29,23 @@ module Victor
|
|
27
29
|
value = nil
|
28
30
|
end
|
29
31
|
|
30
|
-
|
32
|
+
attributes = Attributes.new attributes
|
31
33
|
|
32
34
|
if block_given? || value
|
33
|
-
content.push "<#{name} #{
|
35
|
+
content.push "<#{name} #{attributes}".strip + ">"
|
34
36
|
value ? content.push(value) : yield
|
35
37
|
content.push "</#{name}>"
|
36
38
|
else
|
37
|
-
content.push "<#{name} #{
|
39
|
+
content.push "<#{name} #{attributes}/>"
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
41
43
|
def render
|
42
|
-
|
44
|
+
svg_template % {
|
45
|
+
css: CSS.new(css),
|
46
|
+
attributes: svg_attributes,
|
47
|
+
content: content.join("\n")
|
48
|
+
}
|
43
49
|
end
|
44
50
|
|
45
51
|
def save(filename)
|
@@ -49,33 +55,16 @@ module Victor
|
|
49
55
|
|
50
56
|
private
|
51
57
|
|
52
|
-
def
|
53
|
-
mapped = attributes.map do |key, value|
|
54
|
-
key = key.to_s.tr '_', '-'
|
55
|
-
value.is_a?(Hash) ? inner_expand(key, value) : "#{key}=\"#{value}\""
|
56
|
-
end
|
57
|
-
|
58
|
-
mapped.join ' '
|
59
|
-
end
|
60
|
-
|
61
|
-
def inner_expand(name, attributes)
|
62
|
-
mapped = attributes.map do |key, value|
|
63
|
-
"#{key}:#{value}"
|
64
|
-
end
|
65
|
-
|
66
|
-
"#{name}=\"#{mapped.join '; '}\""
|
67
|
-
end
|
68
|
-
|
69
|
-
def template
|
58
|
+
def svg_template
|
70
59
|
File.read template_path
|
71
60
|
end
|
72
61
|
|
73
62
|
def template_path
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
63
|
+
if template.is_a? Symbol
|
64
|
+
File.join File.dirname(__FILE__), 'templates', "#{template}.svg"
|
65
|
+
else
|
66
|
+
template
|
67
|
+
end
|
79
68
|
end
|
80
69
|
end
|
81
70
|
|
data/lib/victor/version.rb
CHANGED
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.0
|
4
|
+
version: 0.1.0
|
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: 2016-05-
|
11
|
+
date: 2016-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: runfile
|
@@ -88,8 +88,11 @@ extra_rdoc_files: []
|
|
88
88
|
files:
|
89
89
|
- README.md
|
90
90
|
- lib/victor.rb
|
91
|
+
- lib/victor/attributes.rb
|
92
|
+
- lib/victor/css.rb
|
91
93
|
- lib/victor/svg.rb
|
92
94
|
- lib/victor/templates/default.svg
|
95
|
+
- lib/victor/templates/html.svg
|
93
96
|
- lib/victor/version.rb
|
94
97
|
homepage: https://github.com/DannyBen/victor
|
95
98
|
licenses:
|