victor 0.3.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4fb744c40ffdf04d89198bbe192150e73fc6776f2fc92f26bfd246c1f107c98
4
- data.tar.gz: 1360dae8be0520d6357ec7300c19cb7a7769cc656081fa1f66dd5bfdbe2895b0
3
+ metadata.gz: 6cfb9ab7b01c8fe3986d973cf09708e47b05ec61d726f4db2e5c94f5b7697d8b
4
+ data.tar.gz: 55936a274c2b1f662ad6dfb4aacb8396499fe6f5c7366d33fde7858e082d02d1
5
5
  SHA512:
6
- metadata.gz: ed68e9abbe74e3abe5c910f24c6dc988365da107317836649c0f9aef22a7876e7b27c7ea7f18c77d07e436951bb092123f9432a1faa3189e4900b8f3fcb217fe
7
- data.tar.gz: 313a4bd4034c8ea18d5e111fb22e7f4073d6f9dff7cf3f57ddabd980074ffd113afcfb9313d3d33db39f9fd81ba1be493dae0b2f972fc4197bdfa57c74dfe5af
6
+ metadata.gz: 59e8f7b5c39d6c156b322eecd4719ecb3e036e51484ba915c4642400c3ec3547b79f057a4717188ff67e7b8aee9f999f1d493552e88cbb344ff2fe9e521ca087
7
+ data.tar.gz: 560fe1f61b66cce12912073349b33b1ad73c656ad47780be3c713deb735d3d71a1d2bdb896c419447a7a84ad73360bfc080b56bb69113c2874641826c8519fa3
data/README.md CHANGED
@@ -15,25 +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
- * [DSL Syntax](#dsl-syntax)
31
- * [Using with Rails](#using-with-rails)
32
- * [Related Projects](#related-projects)
33
- * [Contributing / Support](#contributing--support)
34
-
35
- ---
36
-
37
18
  ## Install
38
19
 
39
20
  ```
@@ -266,18 +247,13 @@ svg.save 'filename'
266
247
 
267
248
  ### SVG Templates
268
249
 
269
- The `:default` SVG template is designed to be a full XML document (i.e.,
270
- a standalone SVG image). If you wish to use the output as an SVG element
271
- inside HTML, you can change the SVG template:
272
-
273
- ```ruby
274
- svg = Victor::SVG.new template: :html
275
- # accepts :html, :minimal, :default or a filename
276
- ```
250
+ Victor renders its content using the [`:default` template][default-template].
277
251
 
278
- You can also point it to any other template file:
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:
279
254
 
280
255
  ```ruby
256
+ svg = Victor::SVG.new template: :minimal
281
257
  svg = Victor::SVG.new template: 'path/to/template.svg'
282
258
  ```
283
259
 
@@ -294,12 +270,10 @@ svg.render template: :minimal
294
270
 
295
271
  ### CSS
296
272
 
297
- CSS gets a special treatment in `Victor::SVG`, with these objectives in mind:
298
-
299
- - Hide implementation details (such as the need for a `CDATA` marker)
300
- - 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.
301
275
 
302
- The `Victor::SVG` objects has a `css` property, which can contain either a
276
+ The `Victor::SVG` object has a `css` property, which can contain either a
303
277
  Hash or a String:
304
278
 
305
279
  ```ruby
@@ -431,6 +405,24 @@ end
431
405
 
432
406
  See the [xml encoding example](https://github.com/DannyBen/victor/tree/master/examples#18-xml-encoding).
433
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
+
419
+ The glue can also be provided when rendering or saving the output:
420
+
421
+ ```ruby
422
+ svg.save 'filename', glue: ''
423
+ svg.render glue: ''
424
+ ```
425
+
434
426
  ### DSL Syntax
435
427
 
436
428
  Victor also supports a DSL-like syntax. To use it, simply `require 'victor/script'`.
@@ -457,11 +449,6 @@ save 'output.svg'
457
449
 
458
450
  See the [dsl example](https://github.com/DannyBen/victor/tree/master/examples#19-dsl).
459
451
 
460
- ## Using with Rails
461
-
462
- See the [example_rails](example_rails) folder.
463
-
464
-
465
452
  ## Related Projects
466
453
 
467
454
  ### [Victor CLI][victor-cli]
@@ -497,6 +484,7 @@ to contribute, feel free to [open an issue][issues].
497
484
 
498
485
  [examples]: https://github.com/DannyBen/victor/tree/master/examples#examples
499
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
500
488
  [icodi]: https://github.com/DannyBen/icodi
501
489
  [minichart]: https://github.com/DannyBen/minichart
502
490
  [victor-opal]: https://kuboon.github.io/victor-opal/
@@ -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
- if value.is_a? Hash
15
+ case value
16
+ when Hash
17
17
  style = Attributes.new(value).to_style
18
18
  "#{key}=\"#{style}\""
19
- elsif value.is_a? Array
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
- %Q{<style type="text/css">\n<![CDATA[\n#{self}\n]]>\n</style>\n}
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 = " " * indent
36
+ my_indent = ' ' * indent
37
37
 
38
- if value.is_a? Hash
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
- elsif value.is_a? Array
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, *arguments, &block)
4
- element method_sym, *arguments, &block
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
@@ -1,13 +1,15 @@
1
1
  module Victor
2
-
3
2
  class SVGBase
4
- attr_accessor :template
3
+ include Marshaling
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 &block if block_given?
12
+ build(&block) if block
11
13
  end
12
14
 
13
15
  def <<(additional_content)
@@ -17,23 +19,23 @@ module Victor
17
19
 
18
20
  def setup(attributes = nil)
19
21
  attributes ||= {}
20
- attributes[:width] ||= "100%"
21
- attributes[:height] ||= "100%"
22
+ attributes[:width] ||= '100%'
23
+ attributes[:height] ||= '100%'
22
24
 
23
- if attributes[:template]
24
- @template = attributes.delete :template
25
- elsif !@template
26
- @template = :default
27
- end
25
+ @template = attributes[:template] || @template || :default
26
+ @glue = attributes[:glue] || @glue || "\n"
27
+
28
+ attributes.delete :template
29
+ attributes.delete :glue
28
30
 
29
31
  @svg_attributes = Attributes.new attributes
30
32
  end
31
33
 
32
34
  def build(&block)
33
- self.instance_eval(&block)
35
+ instance_eval(&block)
34
36
  end
35
37
 
36
- def element(name, value=nil, attributes={}, &_block)
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 + ">" unless empty_tag
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,29 +70,26 @@ module Victor
68
70
  @css
69
71
  end
70
72
 
71
- def css=(defs)
72
- @css = defs
73
- end
74
-
75
- def render(template: nil)
73
+ def render(template: nil, glue: nil)
76
74
  @template = template if template
75
+ @glue = glue if glue
77
76
  css_handler = CSS.new css
78
77
 
79
78
  svg_template % {
80
- css: css_handler,
81
- style: css_handler.render,
79
+ css: css_handler,
80
+ style: css_handler.render,
82
81
  attributes: svg_attributes,
83
- content: content.join("\n")
82
+ content: to_s,
84
83
  }
85
84
  end
86
85
 
87
86
  def to_s
88
- content.join "\n"
87
+ content.join glue
89
88
  end
90
89
 
91
- def save(filename, template: nil)
92
- filename = "#{filename}.svg" unless filename =~ /\..{2,4}$/
93
- File.write filename, render(template: template)
90
+ def save(filename, template: nil, glue: nil)
91
+ filename = "#{filename}.svg" unless /\..{2,4}$/.match?(filename)
92
+ File.write filename, render(template: template, glue: glue)
94
93
  end
95
94
 
96
95
  protected
@@ -107,5 +106,4 @@ module Victor
107
106
  end
108
107
  end
109
108
  end
110
-
111
- 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>
@@ -1,4 +1,4 @@
1
1
  <svg %{attributes}>
2
2
  %{style}
3
3
  %{content}
4
- </svg>
4
+ </svg>
@@ -1,3 +1,3 @@
1
1
  module Victor
2
- VERSION = "0.3.3"
3
- end
2
+ VERSION = '0.4.0'
3
+ end
data/lib/victor.rb CHANGED
@@ -1,8 +1,7 @@
1
1
  require 'victor/version'
2
+ require 'victor/marshaling'
2
3
  require 'victor/svg_base'
3
4
  require 'victor/svg'
4
5
  require 'victor/attributes'
5
6
  require 'victor/css'
6
7
  require 'victor/dsl'
7
-
8
- require 'byebug' if ENV['BYEBUG']
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.3.3
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: 2021-05-15 00:00:00.000000000 Z
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
- post_install_message:
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: 2.3.0
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.2.16
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: []
@@ -1,8 +0,0 @@
1
- <svg %{attributes}
2
- xmlns="http://www.w3.org/2000/svg"
3
- xmlns:xlink="http://www.w3.org/1999/xlink">
4
-
5
- %{style}
6
- %{content}
7
-
8
- </svg>