victor 0.3.4 → 0.4.0
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 +8 -39
- data/lib/victor/attributes.rb +5 -7
- data/lib/victor/css.rb +9 -9
- data/lib/victor/marshaling.rb +39 -0
- data/lib/victor/svg.rb +7 -3
- data/lib/victor/svg_base.rb +15 -18
- data/lib/victor/templates/default.svg +1 -4
- data/lib/victor/templates/minimal.svg +1 -1
- data/lib/victor/version.rb +2 -2
- data/lib/victor.rb +1 -2
- metadata +13 -9
- data/lib/victor/templates/html.svg +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cfb9ab7b01c8fe3986d973cf09708e47b05ec61d726f4db2e5c94f5b7697d8b
|
4
|
+
data.tar.gz: 55936a274c2b1f662ad6dfb4aacb8396499fe6f5c7366d33fde7858e082d02d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59e8f7b5c39d6c156b322eecd4719ecb3e036e51484ba915c4642400c3ec3547b79f057a4717188ff67e7b8aee9f999f1d493552e88cbb344ff2fe9e521ca087
|
7
|
+
data.tar.gz: 560fe1f61b66cce12912073349b33b1ad73c656ad47780be3c713deb735d3d71a1d2bdb896c419447a7a84ad73360bfc080b56bb69113c2874641826c8519fa3
|
data/README.md
CHANGED
@@ -15,26 +15,6 @@ directly to SVG elements.
|
|
15
15
|
|
16
16
|
---
|
17
17
|
|
18
|
-
## Table of Contents
|
19
|
-
|
20
|
-
* [Install](#install)
|
21
|
-
* [Examples](#examples)
|
22
|
-
* [Usage](#usage)
|
23
|
-
* [Features](#features)
|
24
|
-
* [Composite SVG](#composite-svg)
|
25
|
-
* [Saving the Output](#saving-the-output)
|
26
|
-
* [SVG Templates](#svg-templates)
|
27
|
-
* [CSS](#css)
|
28
|
-
* [Tagless Elements](#tagless-elements)
|
29
|
-
* [XML Encoding](#xml-encoding)
|
30
|
-
* [XML Newlines](#xml-newlines)
|
31
|
-
* [DSL Syntax](#dsl-syntax)
|
32
|
-
* [Using with Rails](#using-with-rails)
|
33
|
-
* [Related Projects](#related-projects)
|
34
|
-
* [Contributing / Support](#contributing--support)
|
35
|
-
|
36
|
-
---
|
37
|
-
|
38
18
|
## Install
|
39
19
|
|
40
20
|
```
|
@@ -267,18 +247,13 @@ svg.save 'filename'
|
|
267
247
|
|
268
248
|
### SVG Templates
|
269
249
|
|
270
|
-
|
271
|
-
a standalone SVG image). If you wish to use the output as an SVG element
|
272
|
-
inside HTML, you can change the SVG template:
|
273
|
-
|
274
|
-
```ruby
|
275
|
-
svg = Victor::SVG.new template: :html
|
276
|
-
# accepts :html, :minimal, :default or a filename
|
277
|
-
```
|
250
|
+
Victor renders its content using the [`:default` template][default-template].
|
278
251
|
|
279
|
-
|
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:
|
280
254
|
|
281
255
|
```ruby
|
256
|
+
svg = Victor::SVG.new template: :minimal
|
282
257
|
svg = Victor::SVG.new template: 'path/to/template.svg'
|
283
258
|
```
|
284
259
|
|
@@ -295,12 +270,10 @@ svg.render template: :minimal
|
|
295
270
|
|
296
271
|
### CSS
|
297
272
|
|
298
|
-
CSS gets a special treatment in `Victor::SVG`,
|
299
|
-
|
300
|
-
- Hide implementation details (such as the need for a `CDATA` marker)
|
301
|
-
- Provide a DSL-like syntax for CSS rules
|
273
|
+
CSS gets a special treatment in `Victor::SVG`, in order to provide a DSL-like
|
274
|
+
syntax for CSS rules.
|
302
275
|
|
303
|
-
The `Victor::SVG`
|
276
|
+
The `Victor::SVG` object has a `css` property, which can contain either a
|
304
277
|
Hash or a String:
|
305
278
|
|
306
279
|
```ruby
|
@@ -476,11 +449,6 @@ save 'output.svg'
|
|
476
449
|
|
477
450
|
See the [dsl example](https://github.com/DannyBen/victor/tree/master/examples#19-dsl).
|
478
451
|
|
479
|
-
## Using with Rails
|
480
|
-
|
481
|
-
See the [example_rails](example_rails) folder.
|
482
|
-
|
483
|
-
|
484
452
|
## Related Projects
|
485
453
|
|
486
454
|
### [Victor CLI][victor-cli]
|
@@ -516,6 +484,7 @@ to contribute, feel free to [open an issue][issues].
|
|
516
484
|
|
517
485
|
[examples]: https://github.com/DannyBen/victor/tree/master/examples#examples
|
518
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
|
519
488
|
[icodi]: https://github.com/DannyBen/icodi
|
520
489
|
[minichart]: https://github.com/DannyBen/minichart
|
521
490
|
[victor-opal]: https://kuboon.github.io/victor-opal/
|
data/lib/victor/attributes.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
module Victor
|
2
|
-
|
3
2
|
# Handles conversion from a Hash of attributes, to an XML string or
|
4
3
|
# a CSS string.
|
5
4
|
class Attributes
|
6
5
|
attr_reader :attributes
|
7
6
|
|
8
|
-
def initialize(attributes={})
|
7
|
+
def initialize(attributes = {})
|
9
8
|
@attributes = attributes
|
10
9
|
end
|
11
10
|
|
@@ -13,10 +12,11 @@ module Victor
|
|
13
12
|
mapped = attributes.map do |key, value|
|
14
13
|
key = key.to_s.tr '_', '-'
|
15
14
|
|
16
|
-
|
15
|
+
case value
|
16
|
+
when Hash
|
17
17
|
style = Attributes.new(value).to_style
|
18
18
|
"#{key}=\"#{style}\""
|
19
|
-
|
19
|
+
when Array
|
20
20
|
"#{key}=\"#{value.join ' '}\""
|
21
21
|
else
|
22
22
|
"#{key}=#{value.to_s.encode(xml: :attr)}"
|
@@ -42,7 +42,5 @@ module Victor
|
|
42
42
|
def []=(key, value)
|
43
43
|
attributes[key] = value
|
44
44
|
end
|
45
|
-
|
46
45
|
end
|
47
|
-
|
48
|
-
end
|
46
|
+
end
|
data/lib/victor/css.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
module Victor
|
2
|
-
|
3
2
|
class CSS
|
4
3
|
attr_reader :attributes
|
5
4
|
|
@@ -13,12 +12,13 @@ module Victor
|
|
13
12
|
|
14
13
|
def render
|
15
14
|
return '' if attributes.empty?
|
16
|
-
|
15
|
+
|
16
|
+
%[<style>\n#{self}\n</style>\n]
|
17
17
|
end
|
18
18
|
|
19
19
|
protected
|
20
20
|
|
21
|
-
def convert_hash(hash, indent=2)
|
21
|
+
def convert_hash(hash, indent = 2)
|
22
22
|
return hash unless hash.is_a? Hash
|
23
23
|
|
24
24
|
result = []
|
@@ -33,13 +33,14 @@ module Victor
|
|
33
33
|
def css_block(key, value, indent)
|
34
34
|
result = []
|
35
35
|
|
36
|
-
my_indent =
|
36
|
+
my_indent = ' ' * indent
|
37
37
|
|
38
|
-
|
38
|
+
case value
|
39
|
+
when Hash
|
39
40
|
result.push "#{my_indent}#{key} {"
|
40
|
-
result.push convert_hash(value, indent+2)
|
41
|
+
result.push convert_hash(value, indent + 2)
|
41
42
|
result.push "#{my_indent}}"
|
42
|
-
|
43
|
+
when Array
|
43
44
|
value.each do |row|
|
44
45
|
result.push "#{my_indent}#{key} #{row};"
|
45
46
|
end
|
@@ -50,5 +51,4 @@ module Victor
|
|
50
51
|
result
|
51
52
|
end
|
52
53
|
end
|
53
|
-
|
54
|
-
end
|
54
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Victor
|
2
|
+
module Marshaling
|
3
|
+
# YAML serialization methods
|
4
|
+
def encode_with(coder)
|
5
|
+
coder['template'] = @template
|
6
|
+
coder['glue'] = @glue
|
7
|
+
coder['svg_attributes'] = @svg_attributes
|
8
|
+
coder['css'] = @css
|
9
|
+
coder['content'] = @content
|
10
|
+
end
|
11
|
+
|
12
|
+
def init_with(coder)
|
13
|
+
@template = coder['template']
|
14
|
+
@glue = coder['glue']
|
15
|
+
@svg_attributes = coder['svg_attributes']
|
16
|
+
@css = coder['css']
|
17
|
+
@content = coder['content']
|
18
|
+
end
|
19
|
+
|
20
|
+
# Marshal serialization methods
|
21
|
+
def marshal_dump
|
22
|
+
{
|
23
|
+
template: @template,
|
24
|
+
glue: @glue,
|
25
|
+
svg_attributes: @svg_attributes,
|
26
|
+
css: @css,
|
27
|
+
content: @content,
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def marshal_load(data)
|
32
|
+
@template = data[:template]
|
33
|
+
@glue = data[:glue]
|
34
|
+
@svg_attributes = data[:svg_attributes]
|
35
|
+
@css = data[:css]
|
36
|
+
@content = data[:content]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/victor/svg.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
module Victor
|
2
2
|
class SVG < SVGBase
|
3
|
-
def method_missing(method_sym,
|
4
|
-
element
|
3
|
+
def method_missing(method_sym, ...)
|
4
|
+
element(method_sym, ...)
|
5
|
+
end
|
6
|
+
|
7
|
+
def respond_to_missing?(*)
|
8
|
+
true
|
5
9
|
end
|
6
10
|
end
|
7
|
-
end
|
11
|
+
end
|
data/lib/victor/svg_base.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
module Victor
|
2
|
-
|
3
2
|
class SVGBase
|
3
|
+
include Marshaling
|
4
|
+
|
4
5
|
attr_accessor :template, :glue
|
5
6
|
attr_reader :content, :svg_attributes
|
7
|
+
attr_writer :css
|
6
8
|
|
7
9
|
def initialize(attributes = nil, &block)
|
8
10
|
setup attributes
|
9
11
|
@content = []
|
10
|
-
build
|
12
|
+
build(&block) if block
|
11
13
|
end
|
12
14
|
|
13
15
|
def <<(additional_content)
|
@@ -17,8 +19,8 @@ module Victor
|
|
17
19
|
|
18
20
|
def setup(attributes = nil)
|
19
21
|
attributes ||= {}
|
20
|
-
attributes[:width] ||=
|
21
|
-
attributes[:height] ||=
|
22
|
+
attributes[:width] ||= '100%'
|
23
|
+
attributes[:height] ||= '100%'
|
22
24
|
|
23
25
|
@template = attributes[:template] || @template || :default
|
24
26
|
@glue = attributes[:glue] || @glue || "\n"
|
@@ -30,10 +32,10 @@ module Victor
|
|
30
32
|
end
|
31
33
|
|
32
34
|
def build(&block)
|
33
|
-
|
35
|
+
instance_eval(&block)
|
34
36
|
end
|
35
37
|
|
36
|
-
def element(name, value=nil, attributes={}
|
38
|
+
def element(name, value = nil, attributes = {})
|
37
39
|
if value.is_a? Hash
|
38
40
|
attributes = value
|
39
41
|
value = nil
|
@@ -50,14 +52,14 @@ module Victor
|
|
50
52
|
empty_tag = name.to_s == '_'
|
51
53
|
|
52
54
|
if block_given? || value
|
53
|
-
content.push "<#{name} #{attributes}".strip
|
55
|
+
content.push "#{"<#{name} #{attributes}".strip}>" unless empty_tag
|
54
56
|
if value
|
55
57
|
content.push(escape ? value.to_s.encode(xml: :text) : value)
|
56
58
|
else
|
57
59
|
yield
|
58
60
|
end
|
59
61
|
content.push "</#{name}>" unless empty_tag
|
60
|
-
else
|
62
|
+
else
|
61
63
|
content.push "<#{name} #{attributes}/>"
|
62
64
|
end
|
63
65
|
end
|
@@ -68,20 +70,16 @@ module Victor
|
|
68
70
|
@css
|
69
71
|
end
|
70
72
|
|
71
|
-
def css=(defs)
|
72
|
-
@css = defs
|
73
|
-
end
|
74
|
-
|
75
73
|
def render(template: nil, glue: nil)
|
76
74
|
@template = template if template
|
77
75
|
@glue = glue if glue
|
78
76
|
css_handler = CSS.new css
|
79
77
|
|
80
78
|
svg_template % {
|
81
|
-
css:
|
82
|
-
style:
|
79
|
+
css: css_handler,
|
80
|
+
style: css_handler.render,
|
83
81
|
attributes: svg_attributes,
|
84
|
-
content:
|
82
|
+
content: to_s,
|
85
83
|
}
|
86
84
|
end
|
87
85
|
|
@@ -90,7 +88,7 @@ module Victor
|
|
90
88
|
end
|
91
89
|
|
92
90
|
def save(filename, template: nil, glue: nil)
|
93
|
-
filename = "#{filename}.svg" unless
|
91
|
+
filename = "#{filename}.svg" unless /\..{2,4}$/.match?(filename)
|
94
92
|
File.write filename, render(template: template, glue: glue)
|
95
93
|
end
|
96
94
|
|
@@ -108,5 +106,4 @@ module Victor
|
|
108
106
|
end
|
109
107
|
end
|
110
108
|
end
|
111
|
-
|
112
|
-
end
|
109
|
+
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
<?xml version="1.0" standalone="no"?>
|
2
|
-
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
3
|
-
|
4
1
|
<svg %{attributes}
|
5
2
|
xmlns="http://www.w3.org/2000/svg"
|
6
3
|
xmlns:xlink="http://www.w3.org/1999/xlink">
|
@@ -8,4 +5,4 @@
|
|
8
5
|
%{style}
|
9
6
|
%{content}
|
10
7
|
|
11
|
-
</svg>
|
8
|
+
</svg>
|
data/lib/victor/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module Victor
|
2
|
-
VERSION =
|
3
|
-
end
|
2
|
+
VERSION = '0.4.0'
|
3
|
+
end
|
data/lib/victor.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.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Build SVG images with ease
|
14
14
|
email: db@dannyben.com
|
@@ -21,18 +21,22 @@ files:
|
|
21
21
|
- lib/victor/attributes.rb
|
22
22
|
- lib/victor/css.rb
|
23
23
|
- lib/victor/dsl.rb
|
24
|
+
- lib/victor/marshaling.rb
|
24
25
|
- lib/victor/script.rb
|
25
26
|
- lib/victor/svg.rb
|
26
27
|
- lib/victor/svg_base.rb
|
27
28
|
- lib/victor/templates/default.svg
|
28
|
-
- lib/victor/templates/html.svg
|
29
29
|
- lib/victor/templates/minimal.svg
|
30
30
|
- lib/victor/version.rb
|
31
31
|
homepage: https://github.com/DannyBen/victor
|
32
32
|
licenses:
|
33
33
|
- MIT
|
34
|
-
metadata:
|
35
|
-
|
34
|
+
metadata:
|
35
|
+
bug_tracker_uri: https://github.com/DannyBen/victor/issues
|
36
|
+
changelog_uri: https://github.com/DannyBen/victor/blob/master/CHANGELOG.md
|
37
|
+
source_code_uri: https://github.com/DannyBen/victor
|
38
|
+
rubygems_mfa_required: 'true'
|
39
|
+
post_install_message:
|
36
40
|
rdoc_options: []
|
37
41
|
require_paths:
|
38
42
|
- lib
|
@@ -40,15 +44,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
44
|
requirements:
|
41
45
|
- - ">="
|
42
46
|
- !ruby/object:Gem::Version
|
43
|
-
version:
|
47
|
+
version: 3.0.0
|
44
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
49
|
requirements:
|
46
50
|
- - ">="
|
47
51
|
- !ruby/object:Gem::Version
|
48
52
|
version: '0'
|
49
53
|
requirements: []
|
50
|
-
rubygems_version: 3.
|
51
|
-
signing_key:
|
54
|
+
rubygems_version: 3.5.17
|
55
|
+
signing_key:
|
52
56
|
specification_version: 4
|
53
57
|
summary: SVG Builder
|
54
58
|
test_files: []
|