ruby-svg 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6ffb6a71e63ff7edc59c3c980647026b1b473e7e331f476912d43b2fe6b5f9db
4
+ data.tar.gz: e2426c43ad0aa901a46c2328d1502d0e9d38e860cea4f5919d1a064cedbbbc6e
5
+ SHA512:
6
+ metadata.gz: 7706fcd0bce7e321a4a04214336b919b838f0f022bb836aa3f998f986ee9a2c80e097a44ab4a17f1c9b1d58e6fd4865a396a651b419876073febd571c04309eb
7
+ data.tar.gz: 6ddf24dcc0bb2c30936dd273c17fcb7accfb464e62a65fe13cc1e7dae3c452ec3fde319df7b37fee2be501999708dd7da9f36f983f8baece5c0ab4a8d858a148
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-06-06
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at rocco11nicholls@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ruby-svg.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Rocco Nicholls
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # SVG
2
+
3
+ An SVG generation wrapper for pure Ruby!
4
+
5
+ This gem simply allows your to build SVGs with Ruby syntax. The objects accessible are largely straight from SVGs and should be fairly intuitive, allowing you to build an SVG with Ruby syntax and logic in no time!
6
+
7
+ You can even extend the SVG class to register your own svg types to give you shortcuts to generate your own components!
8
+
9
+ Basic example:
10
+ ```ruby
11
+ SVG.new(:svg) do |svg|
12
+ svg.minx = -5
13
+ svg.miny = -105
14
+ svg.width = 110
15
+ svg.height = 110
16
+
17
+ svg.circle(5, -5, 5)
18
+ end
19
+ ```
20
+
21
+
22
+ ## Installation
23
+
24
+ Install the gem and add to the application's Gemfile by executing:
25
+
26
+ $ bundle add ruby-svg
27
+
28
+ If bundler is not being used to manage dependencies, install the gem by executing:
29
+
30
+ $ gem install ruby-svg
31
+
32
+ ## Usage
33
+
34
+
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Rockster160/ruby-svg. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Rockster160/ruby-svg/blob/master/CODE_OF_CONDUCT.md).
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
49
+
50
+ ## Code of Conduct
51
+
52
+ Everyone interacting in the SVG project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Rockster160/ruby-svg/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SVG
4
+ VERSION = "0.1.1"
5
+ end
data/lib/ruby-svg.rb ADDED
@@ -0,0 +1,215 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ruby-svg/version"
4
+
5
+ class SVG
6
+ class Error < StandardError; end
7
+
8
+ attr_accessor(
9
+ :filename, :tag, :attrs,
10
+ :minx, :miny, :width, :height,
11
+ :items, :stroke, :fill
12
+ )
13
+
14
+ def self.open(tag=:svg, opts={}, &block)
15
+ new(tag, opts, &block).open
16
+ end
17
+
18
+ def self.write(tag=:svg, opts={}, &block)
19
+ new(tag, opts, &block).write
20
+ end
21
+
22
+ def self.register(method_name, &block)
23
+ define_method(method_name) do |*attrs|
24
+ block.call(self, *attrs)
25
+ end
26
+ end
27
+
28
+ # Allows setting any attrs directly from the instance
29
+ def method_missing(method, *args, &block)
30
+ super unless method.to_s.end_with?("=")
31
+
32
+ @attrs[method.to_s[0..-2].to_sym] = args.first
33
+ end
34
+
35
+ def initialize(tag, opts={}, &block)
36
+ @attrs = opts[:attrs] || {}
37
+ if tag == :svg
38
+ # Use `self.` since it will auto set the `attr` as an ivar
39
+ self.fill = :transparent
40
+ self.stroke = :black
41
+ end
42
+ @tag = tag
43
+ @filename = (opts[:filename] || :svg).to_s.gsub(/\.svg$/, "")
44
+ @content = opts[:content]
45
+ @minx, @miny, @width, @height = opts[:minx] || 0, opts[:miny] || 0, opts[:width] || 100, opts[:height] || 100
46
+ @items = []
47
+
48
+ block&.call(self)
49
+ end
50
+
51
+ def g(**attrs, &block)
52
+ @items << SVG.new(:g, attrs: attrs, &block)
53
+ end
54
+ def path(d, **attrs, &block)
55
+ @items << SVG.new(:path, attrs: attrs.merge(d: d), &block)
56
+ end
57
+ def text(str, x, y, **attrs, &block) # https://developer.mozilla.org/en-US/docs/Web/SVG/Element/text
58
+ escaped = str.to_s.gsub("<", "&lt;").gsub(">", "&gt;")
59
+ @items << SVG.new(:text, attrs: attrs.merge(x: x, y: y), content: escaped, &block)
60
+ end
61
+ def rect(x, y, width, height, **attrs, &block)
62
+ @items << SVG.new(:rect, attrs: attrs.merge(x: x, y: y, width: width, height: height), &block)
63
+ end
64
+ def circle(x, y, r, **attrs, &block)
65
+ @items << SVG.new(:circle, attrs: attrs.merge(cx: x, cy: y, r: r), &block)
66
+ end
67
+ def line(x1, y1, x2, y2, **attrs, &block)
68
+ @items << SVG.new(:line, attrs: attrs.merge(x1: x1, y1: y1, x2: x2, y2: y2), &block)
69
+ end
70
+ def ellipse(cx, cy, rx, ry, **attrs, &block)
71
+ @items << SVG.new(:ellipse, attrs: attrs.merge(cx: cx, cy: cy, rx: rx, ry: ry), &block)
72
+ end
73
+ def polyline(*points, **attrs, &block)
74
+ block_called = false
75
+ points = points.presence || block.call.then { |block_points|
76
+ next block_points if block_points.is_a?(String)
77
+ next unless block_points.is_a?(Array)
78
+
79
+ block_points.filter_map { |block_point|
80
+ case block_point
81
+ when String then block_point
82
+ when Array then block_point.join(",")
83
+ when Hash then "#{block_point[:x]},#{block_point[:y]}"
84
+ end
85
+ }.join(" ")
86
+ }&.tap { block_called = true }
87
+ block = nil if block_called
88
+ @items << SVG.new(:polyline, attrs: attrs.merge(points: points), &block)
89
+ end
90
+ def polygon(*points, **attrs, &block)
91
+ @items << SVG.new(:polygon, attrs: attrs.merge(points: points), &block)
92
+ end
93
+ def item(tag, **attrs, &block)
94
+ @items << SVG.new(tag, attrs: attr, &block)
95
+ end
96
+ # <animate attributeName="|" attributeType="" begin="0" end="" from="" to="" dur="" repeatCount="" fill=""/>
97
+
98
+ def html_tag(tag, n, **attrs, &block)
99
+ # TODO: Need to escape attr vals, in case they have quotes in them
100
+ attr_str = attrs.map { |k,v| "#{k}=\"#{v}\"" }.join(" ")
101
+ content = block&.call
102
+ no_content = content.gsub(/\s/, "").length == 0
103
+ # content.gsub(/\s/, "").length == 0 ? "" : "\n #{content}\n"
104
+ if no_content
105
+ "<#{tag} #{attr_str} />"
106
+ else
107
+ "<#{tag} #{attr_str}>\n#{content}\n#{" "*n}</#{tag}>"
108
+ end
109
+ end
110
+
111
+ def to_svg(n=0)
112
+ if @tag == :svg
113
+ @attrs = {
114
+ xmlns: "http://www.w3.org/2000/svg",
115
+ viewBox: [@minx, @miny, @width, @height].join(" "),
116
+ }.merge(@attrs)
117
+ end
118
+ @attrs = @attrs.transform_keys { |sym| sym.to_s.split("_").join("-") }
119
+
120
+ html_tag(@tag, n, **@attrs) {
121
+ [@content, *items.map { |i| i.to_svg(n+1) }].filter_map { |i|
122
+ next if i.to_s.gsub(/\s/, "").length == 0
123
+ "#{" "*(n+1)}#{i}"
124
+ }.join("\n")
125
+ }
126
+ end
127
+
128
+ def save_as(filename)
129
+ write(filename)
130
+ end
131
+
132
+ def write(filename=@filename)
133
+ File.open("#{@filename}.svg", "w") { |file| file.write(to_svg) }
134
+ end
135
+
136
+ def open
137
+ write
138
+ default_browser = `defaults read com.apple.LaunchServices/com.apple.launchservices.secure LSHandlers | sed -n -e "/LSHandlerURLScheme = https;/{x;p;d;}" -e 's/.*=[^"]"\\(.*\\)";/\\1/g' -e x`.strip
139
+ `open -b #{default_browser} '#{@filename}.svg' && sleep 2 && rm '#{@filename}.svg'`
140
+ end
141
+ end
142
+
143
+ # SVG.open(:svg) do |svg| - alternative syntax. Automatically opens the SVG after creation.
144
+ # SVG.new(:svg) do |svg|
145
+ # svg.minx = -5
146
+ # svg.miny = -105
147
+ # svg.width = 110
148
+ # svg.height = 110
149
+ #
150
+ # svg.circle(5, -5, 5)
151
+ # svg.grid(svg.minx, svg.miny, svg.width, svg.height)
152
+ # end
153
+
154
+ # SVG.new(:svg, filename: "my svg") do |svg|
155
+ # svg.stroke = "black"
156
+ # svg.fill = "transparent"
157
+ # svg.g(id: "sup") do |g|
158
+ # g.rect(5, 5, 90, 90)
159
+ # g.text("Words", 10, 5)
160
+ # g.rect(0, 0, 50, 50)
161
+ # g.g do |g|
162
+ # g.text("More Words", 5, 20)
163
+ # end
164
+ # end
165
+ # end
166
+
167
+ # SVG.register(:point) do |svg, attrs|
168
+ # attrs[:fill] ||= svg.stroke || :black
169
+ # attrs[:stroke] ||= svg.fill || :none
170
+ #
171
+ # svg.circle(**attrs.merge(cx: x, cy: y, r: r))
172
+ # end
173
+ #
174
+ # SVG.register(:bezier) do |svg, attrs|
175
+ # p1, b1, b2, p2 = attrs.slice(:p1, :b1, :b2, :p2).values
176
+ # path = [:M, p1.join(","), :C, *[b1, b2, p2].map { |c| c.join(",") }]
177
+ # puts "\e[36m#{path.join(" ")}\e[0m"
178
+ # svg.path(path.join(" "), **attrs)
179
+ #
180
+ # svg.line(*p1, *b1, stroke: :cyan, opacity: 0.6)
181
+ # svg.point(*b1, fill: :cyan, opacity: 0.4)
182
+ # svg.point(*p1, fill: :blue, opacity: 0.4)
183
+ #
184
+ # svg.line(*b2, *p2, stroke: :lime, opacity: 0.6)
185
+ # svg.point(*b2, fill: :lime, opacity: 0.4)
186
+ # svg.point(*p2, fill: :green, opacity: 0.4)
187
+ # end
188
+ # SVG.register(:spiral) do |svg, attrs|
189
+ # center_x = svg.width/2
190
+ # center_y = svg.height/2
191
+ # max_rev = attrs.delete(:max_rev) || 10
192
+ # progress = attrs.delete(:progress) || 1
193
+ # outer_radius = attrs.delete(:size) || [center_x, center_y].min
194
+ #
195
+ # # line_width = (max_radius/(max_rev+1).to_f)-1
196
+ # # padding = max_line_width
197
+ # # outer_radius = max_radius - padding
198
+ # # line_width = (outer_radius/max_rev.to_f)-1
199
+ #
200
+ # path = ["M#{center_x},#{center_y-outer_radius}"] # Starting at top-center
201
+ # (0..max_rev * 360).step(1).each do |angle|
202
+ # rotation = 90
203
+ # radians = (angle - rotation) * (Math::PI / 180)
204
+ # radius = outer_radius * (1 - (angle.to_f / (max_rev*360)))
205
+ #
206
+ # x = center_x + radius * Math.cos(radians)
207
+ # y = center_y + radius * Math.sin(radians)
208
+ # path << "L#{x},#{y} "
209
+ #
210
+ # break if angle.to_f > (progress*360)
211
+ # end
212
+ # path.join(" ")
213
+ #
214
+ # svg.path(path.join(" "), **attrs)
215
+ # end
data/sig/ruby-svg.rbs ADDED
@@ -0,0 +1,4 @@
1
+ class SVG
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-svg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Rocco Nicholls
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - rocco11nicholls@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/ruby-svg.rb
28
+ - lib/ruby-svg/version.rb
29
+ - sig/ruby-svg.rbs
30
+ homepage: https://github.com/Rockster160/ruby-svg
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://github.com/Rockster160/ruby-svg
35
+ source_code_uri: https://github.com/Rockster160/ruby-svg
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.4.1
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Simply Ruby wrapper to generate SVG files
55
+ test_files: []