victor 0.4.0 → 0.5.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 +30 -453
- data/lib/victor/component.rb +61 -0
- data/lib/victor/dsl.rb +1 -1
- data/lib/victor/marshaling.rb +16 -22
- data/lib/victor/svg.rb +1 -1
- data/lib/victor/svg_base.rb +7 -1
- data/lib/victor/templates/default.svg +1 -5
- data/lib/victor/version.rb +1 -1
- data/lib/victor.rb +10 -6
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86eac1cab671c40d60409cf11e0e06800add519f2a11e80f33adbb8a516b7906
|
4
|
+
data.tar.gz: ff574def00c021acf82c0071ce6d9467576a74e0651663ecd7291ca2e5884ea7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fa1657d2e98c4bf98468f9f1e9a69a07c0c049eac6033d0bb3d2bbe603627cb448034923f94e6aef31db755ca66916a484ad521c86534c32274ef764a31ed79
|
7
|
+
data.tar.gz: 1a0662ce919d6abed8bea839c4fef03f7314960dbeaa5602b1627b44e4f69082e3ce933de57170c17973a58ab8f61ee7bb214796c99e641b35a3dec59a54d945
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
<div align='center'>
|
2
|
+
<img src='assets/logo.svg' width=500>
|
2
3
|
|
3
4
|
# Victor - Ruby SVG Image Builder
|
4
5
|
|
@@ -6,12 +7,14 @@
|
|
6
7
|
[![Build Status](https://github.com/DannyBen/victor/workflows/Test/badge.svg)](https://github.com/DannyBen/victor/actions?query=workflow%3ATest)
|
7
8
|
[![Maintainability](https://api.codeclimate.com/v1/badges/85cc05c219d6d233ab78/maintainability)](https://codeclimate.com/github/DannyBen/victor/maintainability)
|
8
9
|
|
9
|
-
|
10
|
+
## [victor.dannyb.co](https://victor.dannyb.co)
|
11
|
+
|
12
|
+
</div>
|
10
13
|
|
11
|
-
|
12
|
-
directly to SVG elements.
|
14
|
+
---
|
13
15
|
|
14
|
-
|
16
|
+
**Victor** is a lightweight, zero-dependencies Ruby library that lets you build
|
17
|
+
SVG images using Ruby code.
|
15
18
|
|
16
19
|
---
|
17
20
|
|
@@ -21,472 +24,46 @@ directly to SVG elements.
|
|
21
24
|
$ gem install victor
|
22
25
|
```
|
23
26
|
|
24
|
-
|
25
|
-
|
26
|
-
```ruby
|
27
|
-
gem 'victor'
|
28
|
-
```
|
29
|
-
|
30
|
-
## Examples
|
31
|
-
|
32
|
-
```ruby
|
33
|
-
require 'victor'
|
34
|
-
|
35
|
-
svg = Victor::SVG.new width: 140, height: 100, style: { background: '#ddd' }
|
36
|
-
|
37
|
-
svg.build do
|
38
|
-
rect x: 10, y: 10, width: 120, height: 80, rx: 10, fill: '#666'
|
39
|
-
|
40
|
-
circle cx: 50, cy: 50, r: 30, fill: 'yellow'
|
41
|
-
circle cx: 58, cy: 32, r: 4, fill: 'black'
|
42
|
-
polygon points: %w[45,50 80,30 80,70], fill: '#666'
|
43
|
-
|
44
|
-
3.times do |i|
|
45
|
-
x = 80 + i*18
|
46
|
-
circle cx: x, cy: 50, r: 4, fill: 'yellow'
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
svg.save 'pacman'
|
51
|
-
```
|
52
|
-
|
53
|
-
Output:
|
54
|
-
|
55
|
-
[![pacman](https://cdn.rawgit.com/DannyBen/victor/master/examples/10_pacman.svg)](https://github.com/DannyBen/victor/blob/master/examples/10_pacman.rb)
|
56
|
-
|
57
|
-
|
58
|
-
See the [examples] folder for several ruby scripts and their SVG output.
|
59
|
-
|
60
|
-
|
61
|
-
## Usage
|
62
|
-
|
63
|
-
Initialize your SVG image:
|
64
|
-
|
65
|
-
```ruby
|
66
|
-
require 'victor'
|
67
|
-
svg = Victor::SVG.new
|
68
|
-
```
|
69
|
-
|
70
|
-
Any option you provide to `SVG.new` will be added as an attribute to the
|
71
|
-
main `<svg>` element. By default, `height` and `width` are set to 100%.
|
72
|
-
|
73
|
-
```ruby
|
74
|
-
svg = Victor::SVG.new width: '100%', height: '100%'
|
75
|
-
# same as just Victor::SVG.new
|
76
|
-
|
77
|
-
svg = Victor::SVG.new width: '100%', height: '100%', viewBox: "0 0 200 100"
|
78
|
-
```
|
79
|
-
|
80
|
-
As an alternative, you can set the root SVG attributes using the `setup` method:
|
81
|
-
|
82
|
-
```ruby
|
83
|
-
require 'victor'
|
84
|
-
svg = Victor::SVG.new
|
85
|
-
svg.setup width: 200, height: 150
|
86
|
-
```
|
87
|
-
|
88
|
-
Victor uses a single method (`element`) to generate all SVG elements:
|
89
|
-
|
90
|
-
```ruby
|
91
|
-
svg.element :rect, x: 2, y: 2, width: 200, height: 200
|
92
|
-
# => <rect x="2" y="2" width="200" height="200"/>
|
93
|
-
```
|
94
|
-
|
95
|
-
But you can omit it. Calls to any other method, will be delegated to the
|
96
|
-
`element` method, so normal usage looks more like this:
|
97
|
-
|
98
|
-
```ruby
|
99
|
-
svg.rect x: 2, y: 2, width: 200, height: 200
|
100
|
-
# => <rect x="2" y="2" width="200" height="200"/>
|
101
|
-
```
|
102
|
-
|
103
|
-
In other words, these are the same:
|
104
|
-
|
105
|
-
```ruby
|
106
|
-
svg.element :anything, option: 'value'
|
107
|
-
svg.anything option: 'value'
|
108
|
-
```
|
109
|
-
|
110
|
-
You can use the `build` method, to generate the SVG with a block
|
111
|
-
|
112
|
-
```ruby
|
113
|
-
svg.build do
|
114
|
-
rect x: 0, y: 0, width: 100, height: 100, fill: '#ccc'
|
115
|
-
rect x: 20, y: 20, width: 60, height: 60, fill: '#f99'
|
116
|
-
end
|
117
|
-
```
|
118
|
-
|
119
|
-
If the value of an attribute is a hash, it will be converted to a
|
120
|
-
style-compatible string:
|
121
|
-
|
122
|
-
```ruby
|
123
|
-
svg.rect x: 0, y: 0, width: 100, height: 100, style: { stroke: '#ccc', fill: 'red' }
|
124
|
-
# => <rect x=0 y=0 width=100 height=100 style="stroke:#ccc; fill:red"/>
|
125
|
-
```
|
126
|
-
|
127
|
-
If the value of an attribute is an array, it will be converted to a
|
128
|
-
space delimited string:
|
129
|
-
|
130
|
-
```ruby
|
131
|
-
svg.path d: ['M', 150, 0, 'L', 75, 200, 'L', 225, 200, 'Z']
|
132
|
-
# => <path d="M 150 0 L 75 200 L 225 200 Z"/>
|
133
|
-
```
|
134
|
-
|
135
|
-
For SVG elements that have an inner content - such as text - simply pass it as
|
136
|
-
the first argument:
|
137
|
-
|
138
|
-
```ruby
|
139
|
-
svg.text "Victor", x: 40, y: 50
|
140
|
-
# => <text x="40" y="50">Victor</text>
|
141
|
-
```
|
142
|
-
|
143
|
-
You can also nest elements with blocks:
|
144
|
-
|
145
|
-
```ruby
|
146
|
-
svg.build do
|
147
|
-
g font_size: 30, font_family: 'arial', fill: 'white' do
|
148
|
-
text "Scalable Victor Graphics", x: 40, y: 50
|
149
|
-
end
|
150
|
-
end
|
151
|
-
# => <g font-size="30" font-family="arial" fill="white">
|
152
|
-
# <text x="40" y="50">Scalable Victor Graphics</text>
|
153
|
-
# </g>
|
154
|
-
```
|
155
|
-
|
156
|
-
Underscores in attribute names are converted to dashes:
|
157
|
-
|
158
|
-
```ruby
|
159
|
-
svg.text "Victor", x: 40, y: 50, font_family: 'arial', font_weight: 'bold', font_size: 40
|
160
|
-
# => <text x="40" y="50" font-family="arial" font-weight="bold" font-size="40">
|
161
|
-
# Victor
|
162
|
-
# </text>
|
163
|
-
```
|
164
|
-
|
165
|
-
## Features
|
166
|
-
|
167
|
-
### Composite SVG
|
168
|
-
|
169
|
-
Victor also supports the ability to combine several smaller SVG objects into
|
170
|
-
one using the `<<` operator or the `#append` method.
|
171
|
-
|
172
|
-
This operator expects to receive any object that responds to `#to_s` (can be another `SVG` object).
|
173
|
-
|
174
|
-
```ruby
|
175
|
-
require 'victor'
|
176
|
-
include Victor
|
177
|
-
|
178
|
-
# Create a reusable SVG object
|
179
|
-
frame = SVG.new
|
180
|
-
frame.rect x: 0, y: 0, width: 100, height: 100, fill: '#336'
|
181
|
-
frame.rect x: 10, y: 10, width: 80, height: 80, fill: '#fff'
|
182
|
-
|
183
|
-
# ... and another
|
184
|
-
troll = SVG.new
|
185
|
-
troll.circle cx: 50, cy: 60, r: 24, fill: 'yellow'
|
186
|
-
troll.polygon points: %w[24,50 50,14 76,54], fill: 'red'
|
187
|
-
|
188
|
-
# Combine objects into a single image
|
189
|
-
svg = SVG.new viewBox: '0 0 100 100'
|
190
|
-
svg << frame
|
191
|
-
svg << troll
|
192
|
-
|
193
|
-
# ... and save it
|
194
|
-
svg.save 'framed-troll'
|
195
|
-
```
|
196
|
-
|
197
|
-
Output:
|
198
|
-
|
199
|
-
[![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)
|
200
|
-
|
201
|
-
These two calls are identical:
|
202
|
-
|
203
|
-
```ruby
|
204
|
-
svg << other
|
205
|
-
svg.append other
|
206
|
-
```
|
207
|
-
|
208
|
-
To make this common use case a little easier to use, you can use a block when instantiating a new `SVG` object:
|
209
|
-
|
210
|
-
```ruby
|
211
|
-
troll = SVG.new do
|
212
|
-
circle cx: 50, cy: 60, r: 24, fill: 'yellow'
|
213
|
-
end
|
214
|
-
```
|
215
|
-
|
216
|
-
Which is the same as:
|
217
|
-
|
218
|
-
```ruby
|
219
|
-
troll = SVG.new
|
220
|
-
troll.build do
|
221
|
-
circle cx: 50, cy: 60, r: 24, fill: 'yellow'
|
222
|
-
end
|
223
|
-
```
|
224
|
-
|
225
|
-
Another approach to a more modular SVG composition, would be to subclass
|
226
|
-
`Victor::SVG`.
|
227
|
-
|
228
|
-
See the [composite svg example](https://github.com/DannyBen/victor/tree/master/examples#14-composite-svg)
|
229
|
-
or the [subclassing example](https://github.com/DannyBen/victor/tree/master/examples#15-subclassing)
|
230
|
-
for more details.
|
231
|
-
|
232
|
-
|
233
|
-
### Saving the Output
|
234
|
-
|
235
|
-
Generate the full SVG to a string with `render`:
|
236
|
-
|
237
|
-
```ruby
|
238
|
-
result = svg.render
|
239
|
-
```
|
240
|
-
|
241
|
-
Or, save it to a file with `save`:
|
242
|
-
|
243
|
-
```ruby
|
244
|
-
svg.save 'filename'
|
245
|
-
# the '.svg' extension is optional
|
246
|
-
```
|
247
|
-
|
248
|
-
### SVG Templates
|
249
|
-
|
250
|
-
Victor renders its content using the [`:default` template][default-template].
|
251
|
-
|
252
|
-
If you wish to use another template, you can either use one of the built-in
|
253
|
-
templates (`:default` or `:minimal`), or provide your own:
|
254
|
-
|
255
|
-
```ruby
|
256
|
-
svg = Victor::SVG.new template: :minimal
|
257
|
-
svg = Victor::SVG.new template: 'path/to/template.svg'
|
258
|
-
```
|
259
|
-
|
260
|
-
See the [templates] folder for an understanding of how templates are
|
261
|
-
structured.
|
262
|
-
|
263
|
-
Templates can also be provided when rendering or saving the output:
|
264
|
-
|
265
|
-
```ruby
|
266
|
-
svg.save 'filename', template: :minimal
|
267
|
-
svg.render template: :minimal
|
268
|
-
```
|
269
|
-
|
270
|
-
|
271
|
-
### CSS
|
272
|
-
|
273
|
-
CSS gets a special treatment in `Victor::SVG`, in order to provide a DSL-like
|
274
|
-
syntax for CSS rules.
|
275
|
-
|
276
|
-
The `Victor::SVG` object has a `css` property, which can contain either a
|
277
|
-
Hash or a String:
|
278
|
-
|
279
|
-
```ruby
|
280
|
-
svg = Victor::SVG.new
|
281
|
-
|
282
|
-
svg.css = css_hash_or_string
|
283
|
-
# or without the equal sign:
|
284
|
-
svg.css css_hash_or_string
|
285
|
-
|
286
|
-
svg.build do
|
287
|
-
# ...
|
288
|
-
end
|
289
|
-
```
|
290
|
-
|
291
|
-
This flexibility allows you to apply CSS in multiple ways. Below are some
|
292
|
-
examples.
|
293
|
-
|
294
|
-
#### Assigning CSS rules using the hash syntax
|
295
|
-
|
296
|
-
```ruby
|
297
|
-
svg = Victor::SVG.new
|
298
|
-
|
299
|
-
svg.build do
|
300
|
-
css['.main'] = {
|
301
|
-
stroke: "green",
|
302
|
-
stroke_width: 2,
|
303
|
-
fill: "yellow"
|
304
|
-
}
|
305
|
-
|
306
|
-
circle cx: 35, cy: 35, r: 20, class: 'main'
|
307
|
-
end
|
308
|
-
```
|
309
|
-
|
310
|
-
#### Assigning a full hash to the CSS property
|
27
|
+
## Example
|
311
28
|
|
312
|
-
|
313
|
-
svg.css = {
|
314
|
-
'.bar': {
|
315
|
-
fill: '#666',
|
316
|
-
stroke: '#fff',
|
317
|
-
stroke_width: 1
|
318
|
-
},
|
319
|
-
'.negative': {
|
320
|
-
fill: '#f66'
|
321
|
-
},
|
322
|
-
'.positive': {
|
323
|
-
fill: '#6f6'
|
324
|
-
}
|
325
|
-
}
|
326
|
-
```
|
327
|
-
|
328
|
-
Underscore characters will be converted to dashes (`stroke_width` becomes
|
329
|
-
`stroke-width`).
|
330
|
-
|
331
|
-
#### Importing CSS from an external file
|
332
|
-
|
333
|
-
```ruby
|
334
|
-
svg.css = File.read 'styles.css'
|
335
|
-
```
|
29
|
+
<table><tr><td width="250">
|
336
30
|
|
337
|
-
|
31
|
+
<img src='assets/ghost.svg' width=250>
|
338
32
|
|
339
|
-
|
33
|
+
</td><td>
|
340
34
|
|
341
35
|
```ruby
|
342
|
-
|
343
|
-
"url('https://fonts.googleapis.com/css?family=Audiowide')",
|
344
|
-
"url('https://fonts.googleapis.com/css?family=Pacifico')"
|
345
|
-
]
|
346
|
-
```
|
347
|
-
|
348
|
-
This is achieved thanks to the fact that when Victor encounters an array
|
349
|
-
in the CSS hash, it will prefix each of the array elements with the hash
|
350
|
-
key, so the above will result in two `@import url(...)` rows.
|
351
|
-
|
352
|
-
See the [css example](https://github.com/DannyBen/victor/tree/master/examples#08-css),
|
353
|
-
[css string example](https://github.com/DannyBen/victor/tree/master/examples#09-css-string),
|
354
|
-
or the [custom fonts example](https://github.com/DannyBen/victor/tree/master/examples#13-custom-fonts).
|
355
|
-
|
356
|
-
|
357
|
-
### Tagless Elements
|
358
|
-
|
359
|
-
Using underscore (`_`) as the element name will simply add the value to the
|
360
|
-
generated SVG, without any surrounding element. This is designed to allow
|
361
|
-
generating something like this:
|
36
|
+
setup viewBox: '0 0 100 100'
|
362
37
|
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
a banana
|
368
|
-
</text>
|
369
|
-
```
|
370
|
-
|
371
|
-
using this Ruby code:
|
38
|
+
build do
|
39
|
+
rect x: 0, y: 0, width: 100, height: 100, fill: :white
|
40
|
+
circle cx: 50, cy: 50, r: 40, fill: 'yellow'
|
41
|
+
rect x: 10, y: 50, width: 80, height: 50, fill: :yellow
|
372
42
|
|
373
|
-
|
374
|
-
|
375
|
-
text do
|
376
|
-
_ 'You are'
|
377
|
-
tspan 'not', font_weight: "bold"
|
378
|
-
_ 'a banana'
|
43
|
+
[25, 50].each do |x|
|
44
|
+
circle cx: x, cy: 40, r: 8, fill: :white
|
379
45
|
end
|
380
|
-
end
|
381
|
-
```
|
382
|
-
|
383
|
-
See the [tagless elements example](https://github.com/DannyBen/victor/tree/master/examples#17-tagless-elements).
|
384
|
-
|
385
|
-
|
386
|
-
### XML Encoding
|
387
|
-
|
388
|
-
Plain text values are encoded automatically:
|
389
|
-
|
390
|
-
```ruby
|
391
|
-
svg.build do
|
392
|
-
text "Ben & Jerry's"
|
393
|
-
end
|
394
|
-
# <text>Ben & Jerry's</text>
|
395
|
-
```
|
396
|
-
|
397
|
-
If you need to use the raw, unencoded string, add `!` to the element's name:
|
398
|
-
|
399
|
-
```ruby
|
400
|
-
svg.build do
|
401
|
-
text! "Ben & Jerry's"
|
402
|
-
end
|
403
|
-
# <text>Ben & Jerry's</text>
|
404
|
-
```
|
405
|
-
|
406
|
-
See the [xml encoding example](https://github.com/DannyBen/victor/tree/master/examples#18-xml-encoding).
|
407
|
-
|
408
|
-
|
409
|
-
### XML Newlines
|
410
|
-
|
411
|
-
By default, the generated SVGs will have a newline glue between the elements.
|
412
|
-
You can change this (for example, to an empty string) if the default newlines
|
413
|
-
are not appropriate for your use case.
|
414
|
-
|
415
|
-
```ruby
|
416
|
-
svg = Victor::SVG.new glue: ''
|
417
|
-
```
|
418
46
|
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
svg.render glue: ''
|
424
|
-
```
|
425
|
-
|
426
|
-
### DSL Syntax
|
427
|
-
|
428
|
-
Victor also supports a DSL-like syntax. To use it, simply `require 'victor/script'`.
|
429
|
-
|
430
|
-
Once required, you have access to:
|
431
|
-
|
432
|
-
- `svg` - returns an instance of `Victor::SVG`
|
433
|
-
- All the methods that are available on the `SVG` object, are included at the root level.
|
434
|
-
|
435
|
-
For example:
|
436
|
-
|
437
|
-
```ruby
|
438
|
-
require 'victor/script'
|
439
|
-
|
440
|
-
setup width: 100, height: 100
|
441
|
-
|
442
|
-
build do
|
443
|
-
circle cx: 50, cy: 50, r: 30, fill: "yellow"
|
47
|
+
path fill: 'white', d: %w[
|
48
|
+
M11 100 l13 -15 l13 15 l13 -15
|
49
|
+
l13 15 l13 -15 l13 15 Z
|
50
|
+
]
|
444
51
|
end
|
445
|
-
|
446
|
-
puts render
|
447
|
-
save 'output.svg'
|
448
52
|
```
|
449
53
|
|
450
|
-
|
451
|
-
|
452
|
-
## Related Projects
|
453
|
-
|
454
|
-
### [Victor CLI][victor-cli]
|
455
|
-
|
456
|
-
A command line utility that allows converting Ruby to SVG as well as SVG to
|
457
|
-
Victor Ruby scripts.
|
458
|
-
|
459
|
-
### [Victor Opal][victor-opal]
|
460
|
-
|
461
|
-
A Victor playground that works in the browser.
|
462
|
-
|
463
|
-
### [Minichart][minichart]
|
54
|
+
</td></tr></table>
|
464
55
|
|
465
|
-
A Ruby gem that uses Victor to generate SVG charts
|
466
56
|
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
### [Icodi][icodi]
|
471
|
-
|
472
|
-
A Ruby gem that uses Victor to generate consistent random icon
|
473
|
-
images, similar to GitHub's identicon.
|
474
|
-
|
475
|
-
[![Icodi](assets/icodi.svg)][icodi]
|
57
|
+
## Documentation
|
476
58
|
|
59
|
+
- [Victor Homepage][docs]
|
477
60
|
|
478
61
|
## Contributing / Support
|
479
62
|
|
480
63
|
If you experience any issue, have a question or a suggestion, or if you wish
|
481
|
-
to contribute, feel free to [open an issue][issues]
|
482
|
-
|
483
|
-
---
|
64
|
+
to contribute, feel free to [open an issue][issues] or
|
65
|
+
[start a discussion][discussions].
|
484
66
|
|
485
|
-
[examples]: https://github.com/DannyBen/victor/tree/master/examples#examples
|
486
|
-
[templates]: https://github.com/DannyBen/victor/tree/master/lib/victor/templates
|
487
|
-
[default-template]: https://github.com/DannyBen/victor/blob/master/lib/victor/templates/default.svg?short_path=c28efa4
|
488
|
-
[icodi]: https://github.com/DannyBen/icodi
|
489
|
-
[minichart]: https://github.com/DannyBen/minichart
|
490
|
-
[victor-opal]: https://kuboon.github.io/victor-opal/
|
491
|
-
[victor-cli]: https://github.com/DannyBen/victor-cli
|
492
67
|
[issues]: https://github.com/DannyBen/victor/issues
|
68
|
+
[discussions]: https://github.com/DannyBen/victor/discussions
|
69
|
+
[docs]: https://victor.dannyb.co/
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module Victor
|
4
|
+
class Component
|
5
|
+
extend Forwardable
|
6
|
+
include Marshaling
|
7
|
+
|
8
|
+
def_delegators :svg, :save, :render, :content, :element, :tag, :css, :to_s
|
9
|
+
|
10
|
+
# Marshaling data
|
11
|
+
def marshaling = %i[width height x y svg merged_css]
|
12
|
+
|
13
|
+
# Subclasses MUST implement this
|
14
|
+
def body
|
15
|
+
raise(NotImplementedError, "#{self.class.name} must implement `body'")
|
16
|
+
end
|
17
|
+
|
18
|
+
# Subclasses MUST override these methods, OR assign instance vars
|
19
|
+
def height
|
20
|
+
@height || raise(NotImplementedError,
|
21
|
+
"#{self.class.name} must implement `height' or `@height'")
|
22
|
+
end
|
23
|
+
|
24
|
+
def width
|
25
|
+
@width || raise(NotImplementedError,
|
26
|
+
"#{self.class.name} must implement `width' or `@width'")
|
27
|
+
end
|
28
|
+
|
29
|
+
# Subclasses MAY override these methods, OR assign instance vars
|
30
|
+
def style = @style ||= {}
|
31
|
+
def x = @x ||= 0
|
32
|
+
def y = @y ||= 0
|
33
|
+
|
34
|
+
# Appending/Embedding - DSL for the `#body` implementation
|
35
|
+
def append(component)
|
36
|
+
svg_instance.append component.svg
|
37
|
+
merged_css.merge! component.merged_css
|
38
|
+
end
|
39
|
+
alias embed append
|
40
|
+
|
41
|
+
# SVG / CSS
|
42
|
+
def svg
|
43
|
+
@svg ||= begin
|
44
|
+
body
|
45
|
+
svg_instance.css = merged_css
|
46
|
+
svg_instance
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
# Start with an ordinary SVG instance
|
53
|
+
def svg_instance = @svg_instance ||= SVG.new(viewBox: "#{x} #{y} #{width} #{height}")
|
54
|
+
|
55
|
+
# Internal DSL to enable `add.anything` in the `#body` implementation
|
56
|
+
alias add svg_instance
|
57
|
+
|
58
|
+
# Start with a copy of our own style
|
59
|
+
def merged_css = @merged_css ||= style.dup
|
60
|
+
end
|
61
|
+
end
|
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, :css
|
6
|
+
def_delegators :svg, :setup, :build, :save, :render, :append, :element, :tag, :css
|
7
7
|
|
8
8
|
def svg
|
9
9
|
@svg ||= Victor::SVG.new
|
data/lib/victor/marshaling.rb
CHANGED
@@ -1,39 +1,33 @@
|
|
1
1
|
module Victor
|
2
2
|
module Marshaling
|
3
|
+
def marshaling
|
4
|
+
raise NotImplementedError, "#{self.class.name} must implement `marshaling'"
|
5
|
+
end
|
6
|
+
|
3
7
|
# YAML serialization methods
|
4
8
|
def encode_with(coder)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
coder['css'] = @css
|
9
|
-
coder['content'] = @content
|
9
|
+
marshaling.each do |attr|
|
10
|
+
coder[attr.to_s] = send(attr)
|
11
|
+
end
|
10
12
|
end
|
11
13
|
|
12
14
|
def init_with(coder)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
@css = coder['css']
|
17
|
-
@content = coder['content']
|
15
|
+
marshaling.each do |attr|
|
16
|
+
instance_variable_set(:"@#{attr}", coder[attr.to_s])
|
17
|
+
end
|
18
18
|
end
|
19
19
|
|
20
20
|
# Marshal serialization methods
|
21
21
|
def marshal_dump
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
svg_attributes: @svg_attributes,
|
26
|
-
css: @css,
|
27
|
-
content: @content,
|
28
|
-
}
|
22
|
+
marshaling.to_h do |attr|
|
23
|
+
[attr, send(attr)]
|
24
|
+
end
|
29
25
|
end
|
30
26
|
|
31
27
|
def marshal_load(data)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
@css = data[:css]
|
36
|
-
@content = data[:content]
|
28
|
+
marshaling.each do |attr|
|
29
|
+
instance_variable_set(:"@#{attr}", data[attr])
|
30
|
+
end
|
37
31
|
end
|
38
32
|
end
|
39
33
|
end
|
data/lib/victor/svg.rb
CHANGED
data/lib/victor/svg_base.rb
CHANGED
@@ -12,10 +12,15 @@ module Victor
|
|
12
12
|
build(&block) if block
|
13
13
|
end
|
14
14
|
|
15
|
+
def marshaling
|
16
|
+
%i[template glue svg_attributes css content]
|
17
|
+
end
|
18
|
+
|
15
19
|
def <<(additional_content)
|
16
20
|
content.push additional_content.to_s
|
17
21
|
end
|
18
22
|
alias append <<
|
23
|
+
alias embed <<
|
19
24
|
|
20
25
|
def setup(attributes = nil)
|
21
26
|
attributes ||= {}
|
@@ -35,7 +40,7 @@ module Victor
|
|
35
40
|
instance_eval(&block)
|
36
41
|
end
|
37
42
|
|
38
|
-
def
|
43
|
+
def tag(name, value = nil, attributes = {})
|
39
44
|
if value.is_a? Hash
|
40
45
|
attributes = value
|
41
46
|
value = nil
|
@@ -63,6 +68,7 @@ module Victor
|
|
63
68
|
content.push "<#{name} #{attributes}/>"
|
64
69
|
end
|
65
70
|
end
|
71
|
+
alias element tag
|
66
72
|
|
67
73
|
def css(defs = nil)
|
68
74
|
@css ||= {}
|
data/lib/victor/version.rb
CHANGED
data/lib/victor.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
require 'victor/version'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
|
3
|
+
module Victor
|
4
|
+
autoload :Attributes, 'victor/attributes'
|
5
|
+
autoload :Component, 'victor/component'
|
6
|
+
autoload :CSS, 'victor/css'
|
7
|
+
autoload :DSL, 'victor/dsl'
|
8
|
+
autoload :Marshaling, 'victor/marshaling'
|
9
|
+
autoload :SVG, 'victor/svg'
|
10
|
+
autoload :SVGBase, 'victor/svg_base'
|
11
|
+
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.
|
4
|
+
version: 0.5.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: 2024-08-
|
11
|
+
date: 2024-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Build SVG images with ease
|
14
14
|
email: db@dannyben.com
|
@@ -19,6 +19,7 @@ files:
|
|
19
19
|
- README.md
|
20
20
|
- lib/victor.rb
|
21
21
|
- lib/victor/attributes.rb
|
22
|
+
- lib/victor/component.rb
|
22
23
|
- lib/victor/css.rb
|
23
24
|
- lib/victor/dsl.rb
|
24
25
|
- lib/victor/marshaling.rb
|
@@ -34,6 +35,7 @@ licenses:
|
|
34
35
|
metadata:
|
35
36
|
bug_tracker_uri: https://github.com/DannyBen/victor/issues
|
36
37
|
changelog_uri: https://github.com/DannyBen/victor/blob/master/CHANGELOG.md
|
38
|
+
homepage_uri: https://victor.dannyb.co/
|
37
39
|
source_code_uri: https://github.com/DannyBen/victor
|
38
40
|
rubygems_mfa_required: 'true'
|
39
41
|
post_install_message:
|