svg_spriter 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 76990472a6115293f4a6ed339304131c522d598c
4
+ data.tar.gz: 5dcf85e41e3546d8519dda6913fe37561d0ffc5d
5
+ SHA512:
6
+ metadata.gz: 84c19d3c70f7e892b5dad993cb0cbcf06925137e2981799b02bfd4f39b7a2396610f8d5b6ea5aaf66a10284dfbf27ef632b48a185dbda1788b436fdf9e48d02f
7
+ data.tar.gz: c567b2d24c615d8ba37ab1a268f5082f5ccfc66793955ed740de2708ec71fdcb0d491a8a15ce78e9f9cc10dafa45bfeb61d99ea4a2a8eec620fb87dfdc5bd365
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in svg_spriter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kyle Simmonds
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
+ # SvgSpriter
2
+
3
+ Take a directory of SVG files, optimize them, and compile them into a single sprite file using `<symbol>` elements.
4
+
5
+ Use case for SVG sprites: https://css-tricks.com/svg-sprites-use-better-icon-fonts
6
+
7
+ This gem is based off [svg-sprite](https://www.npmjs.com/package/svg-sprite) for npm. It will allow you to use SVG sprites while only maintaining one instance of an SVG. Originally built for Nanoc to build a sprite when compiling site, but can be used in any Ruby application that can compile a site.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application’s Gemfile:
12
+
13
+ ```ruby
14
+ gem 'svg_spriter'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install svg_spriter
24
+
25
+ ## Usage
26
+
27
+ During compiling, run:
28
+
29
+ ```ruby
30
+ sprite_svg(source: 'source', output: 'output')
31
+ ```
32
+
33
+ where source is something like /static/svg. Output is optional, and will use the source if not specified.
34
+
35
+ ## TODO
36
+
37
+ + Allow for more robust SVGs. Currently only simple SVGs compile reliably
38
+ + Allow for subdirectories of source directory
39
+ + Add more tests for checking integrity of compiled sprite
40
+ + Handle SVGs with duplicate ids
41
+
42
+ ## Author’s note
43
+
44
+ This is my first RubyGem, and my experience with Ruby is limited. This was a way for me to learn more about Ruby and RubyGems, and I don't pretend to know the best way to do things in Ruby. So, any contributions or suggestions for this gem are appreciated.
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kylesimmonds/svg_spriter.
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "svg_spriter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,104 @@
1
+ require 'svg_spriter/version'
2
+ require 'svg_optimizer'
3
+
4
+ module SvgSpriter
5
+
6
+ # class SpriteItem
7
+ # def initialize(filename)
8
+ # @filename = filename
9
+ # end
10
+ # end
11
+
12
+
13
+
14
+ def sprite_svg(source: nil, output: nil)
15
+
16
+ # if item.nil? or item.undefined? # Check for item
17
+ # puts "SVG Spriter needs an item to sprite!" # Harass the user
18
+ # return # End
19
+ if source.nil? # Check for source
20
+ puts "SVG Spriter needs a source!" # Harass the user
21
+ return # End
22
+ elsif File.directory?(source) == false # Check if source is a directory
23
+ puts "Source is not a directory, or directory doesn't exit."
24
+ return # End
25
+ end
26
+
27
+ # @item = SpriteItem.new(item) # Define current item
28
+ @source = source.to_s.chomp('/') # Get source and remove trailing slash
29
+ @output = output.nil? ? @source : output.to_s.chomp('/') # Get output and remove trailing slash. Set to source if nil
30
+
31
+ list_items()
32
+ end
33
+
34
+
35
+
36
+ def list_items
37
+ @item_list = nil
38
+ @item_list = Dir[@source + "/*.svg"] # Get the list of all sprites in @source
39
+ @item_list.delete(@source + "/sprite.svg") # Remove sprite.svg from list
40
+ @compile_list = @item_list.uniq # Remove any duplicates from item list
41
+
42
+ @defs_list = ''
43
+ @symbols_list = ''
44
+ @number_of_svgs = 0
45
+
46
+ @compile_list.each_with_index do |v, i|
47
+ # $current_item = SpriteItem.new v
48
+ @number_of_svgs += 1 # Take a count of how many SVGs there are
49
+ compile_item v
50
+ end
51
+
52
+ @number_of_symbols = @symbols_list.scan(/<symbol/).count
53
+ compile_sprite()
54
+ end
55
+
56
+
57
+
58
+ def compile_item(current_item)
59
+ svg = File.read current_item.to_s # Read the current svg
60
+ id = svg[/id=('|")(.*?)('|")/, 2] # Get id
61
+ vb = svg[/viewBox=(\'|")(.*?)('|")/, 2] # Get viewbox
62
+ par = svg[/preserveAspectRatio=(\'|")(.*?)('|")/, 2] # Get preserveAspectRatio
63
+ # Make sure the three attributes exist
64
+ add_id = id ? "id='#{id}'" : nil
65
+ add_vb = vb ? " viewBox='#{vb}'" : nil
66
+ add_par = par ? " preserveAspectRatio='#{par}'" : nil
67
+
68
+ optimized_svg = SvgOptimizer.optimize svg # Optimize the svg
69
+ symbol = optimized_svg.gsub /<svg (.*?)>/m, "<symbol #{add_id}#{add_vb}#{add_par}>" # Replace the optimized svg tag with a symbol, the id, the viewBox and preserveAspectRatio, if there is one
70
+ symbol = symbol.gsub '</svg>', '</symbol>'
71
+
72
+ defs = symbol[/<defs>(.*?)<\/defs>/m, 1] # Get svg's defs, if any
73
+ sans_defs = nil
74
+ if defs
75
+ pre_defs = symbol[/(.*?)<defs>(.*?)<\/defs>(.* ?)/m, 1] # Get symbol before defs
76
+ post_defs = symbol[/(.*?)<defs>(.*?)<\/defs>(.* ?)/m, 3] # Get symbol after defs
77
+ sans_defs = "#{pre_defs}#{post_defs}" # Put them together to remove the defs
78
+ defs = defs.gsub />\s+</,"><" # Remove all extra space
79
+ @defs_list << defs # Add the defs to the insertion list for the sprite
80
+ end
81
+
82
+ symbol = sans_defs ? sans_defs : symbol # Set the variable to the symbol without defs, if applicable
83
+ symbol = symbol.gsub />\s+</,"><" # Remove all extra space
84
+ @symbols_list << symbol # Add the symbol to the insertion list for the sprite
85
+ end
86
+
87
+
88
+
89
+ def compile_sprite
90
+ $f = File.open @output + '/sprite.svg', 'w+' # Open or create sprite.svg
91
+ $f.truncate 0 # Delete contents of sprite.svg
92
+ $f << '<svg version="1.1" id="icon-sprite" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" xml:space="preserve">' # Add svg open tag
93
+
94
+ unless @defs_list.empty? # Don't add the defs if there aren't any
95
+ @defs_list = @defs_list.gsub(/>\s+</,"><").strip # Trim the defs before adding
96
+ $f << "<defs>#{@defs_list}</defs>" # Add the defs to the sprite
97
+ end
98
+ $f << @symbols_list # Add the symbols to the sprite
99
+ $f << '</svg>' # Close the svg tag
100
+
101
+ $f.close # Close the file
102
+ return $f # Send it back to go to the output
103
+ end
104
+ end
@@ -0,0 +1,3 @@
1
+ module SvgSpriter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'svg_spriter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "svg_spriter"
8
+ spec.version = SvgSpriter::VERSION
9
+ spec.authors = ["Kyle Simmonds"]
10
+ spec.email = ["kylesimmonds@users.noreply.github.com"]
11
+
12
+ spec.summary = %q{Take a directory of SVG files, optimize them, and compile them into a single file using <symbol> elements.}
13
+ spec.description = %q{Based off svg-sprite for npm, this basic utility will allow you to use SVG sprites while only maintaining one instance of an SVG.}
14
+ spec.homepage = "https://github.com/kylesimmonds/svg-spriter"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+
34
+ spec.add_dependency 'svg_optimizer'
35
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svg_spriter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Simmonds
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: svg_optimizer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Based off svg-sprite for npm, this basic utility will allow you to use
70
+ SVG sprites while only maintaining one instance of an SVG.
71
+ email:
72
+ - kylesimmonds@users.noreply.github.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - .travis.yml
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/svg_spriter.rb
87
+ - lib/svg_spriter/version.rb
88
+ - svg_spriter.gemspec
89
+ homepage: https://github.com/kylesimmonds/svg-spriter
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Take a directory of SVG files, optimize them, and compile them into a single
113
+ file using <symbol> elements.
114
+ test_files: []