inline_svg 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of inline_svg might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76c4c056778276bacd69cc27ec4c982566f99f4b
4
- data.tar.gz: e3678d7bf16c59dd3922d358265721e2fa05151a
3
+ metadata.gz: fee50facd0f1019648bd0f7ab4d716c26919c098
4
+ data.tar.gz: 4987318412fe0646742ceaf185df7a33bf2f1835
5
5
  SHA512:
6
- metadata.gz: a883fe5008e571a78008f94e8196ba84feb846bd25086bc127573c142a2e5232c90278e7002adbd0609dbf3be9dec25758743acd1f86e98280abec3a8df23f64
7
- data.tar.gz: 3fa071d1f1e307cb3bffab9e7144dcf681aa32e5549fb950b350fe55a2eb4bc16438db2eebe508d3fd79b0b7022ade27bcb1b499a47498b9a42da389ff2be9ec
6
+ metadata.gz: 440f34b97188f0e0733ecc60d31bd436b2b911a708339b759b4729469029a01755498c4ea105eead195867749751049b57b2b6aa064dbc6ded2d23df65f0d79f
7
+ data.tar.gz: 316936a7b5e273da6ba81e38e6f1f70df6aca4ac73ec8c80fbd3a7658a0e6f13be4ec5e0308c899fbc6b1336a46e3409d5efec7851ceb4b7f0860bd12d172b48
data/CHANGELOG.md CHANGED
@@ -5,6 +5,17 @@ This project adheres to [Semantic Versioning](http://semver.org/).
5
5
  ## [Unreleased][unreleased]
6
6
  Nothing.
7
7
 
8
+ ## [0.4.0] - 2015-03-22
9
+ ### Added
10
+ - A new option: `size` adds width and height attributes to an SVG. Thanks, @2metres.
11
+
12
+ ### Changed
13
+ - Dramatically simplified the TransformPipeline and Transformations code.
14
+ - Added tests for the pipeline and new size transformations.
15
+
16
+ ### Fixed
17
+ - Transformations can no longer be created with a nil value.
18
+
8
19
  ## [0.3.0] - 2015-03-20
9
20
  ### Added
10
21
  - Use Sprockets to find canonical asset paths (fingerprinted, post asset-pipeline).
@@ -22,6 +33,8 @@ Nothing.
22
33
  ### Added
23
34
  - Basic Railtie and view helper to inline SVG documents to Rails views.
24
35
 
25
- [unreleased]: https://github.com/jamesmartin/inline_svg/compare/v0.2.0...HEAD
36
+ [unreleased]: https://github.com/jamesmartin/inline_svg/compare/v0.4.0...HEAD
37
+ [0.4.0]: https://github.com/jamesmartin/inline_svg/compare/v0.3.0...v0.4.0
38
+ [0.3.0]: https://github.com/jamesmartin/inline_svg/compare/v0.2.0...v0.3.0
26
39
  [0.2.0]: https://github.com/jamesmartin/inline_svg/compare/v0.1.0...v0.2.0
27
40
  [0.1.0]: https://github.com/jamesmartin/inline_svg/compare/v0.0.1...v0.1.0
data/README.md CHANGED
@@ -32,9 +32,12 @@ Or install it yourself as:
32
32
  ```
33
33
  inline_svg(file_name, options={})
34
34
  ```
35
+ The `file_name` can be a full path to a file, or just the file's basename. The
36
+ actual path of the file on disk is resolved using
37
+ [Sprockets](://github.com/sstephenson/sprockets), which means you can pre-process
38
+ and fingerprint your SVG files like any other Rails asset.
35
39
 
36
- Currently, this little helper only works in the context of a Rails app. Here's
37
- an example of embedding an SVG document and applying a 'class' attribute in
40
+ Here's an example of embedding an SVG document and applying a 'class' attribute in
38
41
  HAML:
39
42
 
40
43
  ```
@@ -63,6 +66,9 @@ blue:
63
66
 
64
67
  ## Options
65
68
  * `class`: set a CSS class attribute on the SVG
69
+ * `size`: set width and height attributes on the SVG
70
+ * Supplied as "{Width} * {Height}" or "{Number}", so "30px*45px" becomes `width="30px"`
71
+ and `height="45px"`, and "50%" becomes `width="50%"` and `height="50%"`
66
72
  * `title`: add a \<title\> node inside the top level of the SVG document
67
73
  * `desc`: add a \<desc\> node inside the top level of the SVG document
68
74
  * `nocomment`: remove comment tags (and other unsafe/unknown tags) from svg
@@ -71,7 +77,7 @@ blue:
71
77
  Example:
72
78
 
73
79
  ```
74
- inline_svg("some-document.svg", class: 'some-class', title: 'Some Title', desc:
80
+ inline_svg("some-document.svg", class: 'some-class', size: '30% * 20%', title: 'Some Title', desc:
75
81
  'Some interesting description', nocomment: true)
76
82
  ```
77
83
 
@@ -82,3 +88,8 @@ inline_svg("some-document.svg", class: 'some-class', title: 'Some Title', desc:
82
88
  3. Commit your changes (`git commit -am 'Add some feature'`)
83
89
  4. Push to the branch (`git push origin my-new-feature`)
84
90
  5. Create new Pull Request
91
+
92
+ Please write tests for anything you change, add or fix.
93
+ There is a [basic Rails
94
+ app](http://github.com/jamesmartin/inline_svg_test_app) that demonstrates the
95
+ gem's functionality in use.
@@ -1,36 +1,17 @@
1
1
  require 'action_view/helpers' if defined?(Rails)
2
2
  require 'action_view/context' if defined?(Rails)
3
- require 'nokogiri'
4
- require 'loofah'
5
3
 
6
4
  module InlineSvg
7
5
  module ActionView
8
6
  module Helpers
9
- def inline_svg(filename, options={})
7
+ def inline_svg(filename, transform_params={})
10
8
  begin
11
- doc = Loofah::HTML::DocumentFragment.parse(AssetFile.named(filename))
9
+ svg_file = AssetFile.named(filename)
12
10
  rescue InlineSvg::AssetFile::FileNotFound
13
11
  return "<svg><!-- SVG file not found: '#{filename}' --></svg>".html_safe
14
12
  end
15
13
 
16
- if options[:nocomment].present?
17
- doc.scrub!(:strip)
18
- end
19
-
20
- svg = doc.at_css 'svg'
21
- if options[:class]
22
- svg['class'] = options[:class]
23
- end
24
-
25
- %i(title desc).each do |child|
26
- if options[child].present?
27
- node = Nokogiri::XML::Node.new(child.to_s, doc)
28
- node.content = options[child]
29
- svg.add_child node
30
- end
31
- end
32
-
33
- doc.to_html.html_safe
14
+ InlineSvg::TransformPipeline.generate_html_from(svg_file, transform_params).html_safe
34
15
  end
35
16
  end
36
17
  end
@@ -0,0 +1,10 @@
1
+ module InlineSvg::TransformPipeline::Transformations
2
+ class ClassAttribute < Transformation
3
+ def transform(doc)
4
+ doc = Nokogiri::XML::Document.parse(doc.to_html)
5
+ svg = doc.at_css 'svg'
6
+ svg['class'] = value
7
+ doc
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module InlineSvg::TransformPipeline::Transformations
2
+ class Description < Transformation
3
+ def transform(doc)
4
+ doc = Nokogiri::XML::Document.parse(doc.to_html)
5
+ node = Nokogiri::XML::Node.new('desc', doc)
6
+ node.content = value
7
+ doc.at_css('svg').add_child(node)
8
+ doc
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module InlineSvg::TransformPipeline
2
+ module Transformations
3
+ class NoComment < Transformation
4
+ def transform(doc)
5
+ doc = Loofah::HTML::DocumentFragment.parse(doc.to_html)
6
+ doc.scrub!(:strip)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module InlineSvg::TransformPipeline::Transformations
2
+ class Size < Transformation
3
+ def transform(doc)
4
+ doc = Nokogiri::XML::Document.parse(doc.to_html)
5
+ svg = doc.at_css 'svg'
6
+ svg['width'] = width_of(self.value)
7
+ svg['height'] = height_of(self.value)
8
+ doc
9
+ end
10
+
11
+ def width_of(value)
12
+ value.split(/\*/).map(&:strip)[0]
13
+ end
14
+
15
+ def height_of(value)
16
+ value.split(/\*/).map(&:strip)[1] || width_of(value)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ module InlineSvg::TransformPipeline::Transformations
2
+ class Title < Transformation
3
+ def transform(doc)
4
+ doc = Nokogiri::XML::Document.parse(doc.to_html)
5
+ node = Nokogiri::XML::Node.new('title', doc)
6
+ node.content = value
7
+ doc.at_css('svg').add_child(node)
8
+ doc
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module InlineSvg::TransformPipeline::Transformations
2
+ class Transformation
3
+ def self.create_with_value(value)
4
+ self.new(value)
5
+ end
6
+
7
+ attr_reader :value
8
+
9
+ def initialize(value)
10
+ @value = value
11
+ end
12
+
13
+ def transform(*)
14
+ raise "#transform should be implemented by subclasses of Transformation"
15
+ end
16
+ end
17
+
18
+ class NullTransformation < Transformation
19
+ def transform(doc)
20
+ doc
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module InlineSvg::TransformPipeline::Transformations
2
+ def self.all_transformations
3
+ {nocomment: NoComment, class: ClassAttribute, title: Title, desc: Description, size: Size}
4
+ end
5
+
6
+ def self.lookup(transform_params)
7
+ without_empty_values(transform_params).map do |key, value|
8
+ all_transformations.fetch(key, NullTransformation).create_with_value(value)
9
+ end
10
+ end
11
+
12
+ def self.without_empty_values(params)
13
+ params.reject {|key, value| value.nil?}
14
+ end
15
+ end
16
+
17
+ require 'inline_svg/transform_pipeline/transformations/transformation'
18
+ require 'inline_svg/transform_pipeline/transformations/no_comment'
19
+ require 'inline_svg/transform_pipeline/transformations/class_attribute'
20
+ require 'inline_svg/transform_pipeline/transformations/title'
21
+ require 'inline_svg/transform_pipeline/transformations/description'
22
+ require 'inline_svg/transform_pipeline/transformations/size'
@@ -0,0 +1,14 @@
1
+ module InlineSvg
2
+ module TransformPipeline
3
+ def self.generate_html_from(svg_file, transform_params)
4
+ document = Nokogiri::XML::Document.parse(svg_file)
5
+ Transformations.lookup(transform_params).reduce(document) do |doc, transformer|
6
+ transformer.transform(doc)
7
+ end.to_html
8
+ end
9
+ end
10
+ end
11
+
12
+ require 'nokogiri'
13
+ require 'loofah'
14
+ require 'inline_svg/transform_pipeline/transformations'
@@ -1,3 +1,3 @@
1
1
  module InlineSvg
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/inline_svg.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require "inline_svg/version"
2
2
  require "inline_svg/action_view/helpers"
3
- require "inline_svg/finds_asset_paths"
4
3
  require "inline_svg/asset_file"
4
+ require "inline_svg/finds_asset_paths"
5
+ require "inline_svg/transform_pipeline"
6
+
5
7
  require "inline_svg/railtie" if defined?(Rails)
6
8
  require 'active_support/core_ext'
7
9
  require 'nokogiri'
@@ -19,9 +19,9 @@ describe InlineSvg::ActionView::Helpers do
19
19
 
20
20
  context "and no options" do
21
21
  it "returns a html safe version of the file's contents" do
22
- example_file = <<-SVG.sub(/\n$/, '')
23
- <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><!-- This is a comment --></svg>
24
- SVG
22
+ example_file = <<-SVG
23
+ <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><!-- This is a comment --></svg>
24
+ SVG
25
25
  allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(example_file)
26
26
  expect(helper.inline_svg('some-file')).to eq example_file
27
27
  end
@@ -29,10 +29,10 @@ describe InlineSvg::ActionView::Helpers do
29
29
 
30
30
  context "and the 'title' option" do
31
31
  it "adds the title node to the SVG output" do
32
- input_svg = <<-SVG.sub(/\n$/, '')
32
+ input_svg = <<-SVG
33
33
  <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"></svg>
34
34
  SVG
35
- expected_output = <<-SVG.sub(/\n$/, '')
35
+ expected_output = <<-SVG
36
36
  <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><title>A title</title></svg>
37
37
  SVG
38
38
  allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
@@ -42,10 +42,10 @@ SVG
42
42
 
43
43
  context "and the 'desc' option" do
44
44
  it "adds the description node to the SVG output" do
45
- input_svg = <<-SVG.sub(/\n$/, '')
45
+ input_svg = <<-SVG
46
46
  <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"></svg>
47
47
  SVG
48
- expected_output = <<-SVG.sub(/\n$/, '')
48
+ expected_output = <<-SVG
49
49
  <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><desc>A description</desc></svg>
50
50
  SVG
51
51
  allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
@@ -55,10 +55,10 @@ SVG
55
55
 
56
56
  context "and the 'nocomment' option" do
57
57
  it "strips comments and other unknown/unsafe nodes from the output" do
58
- input_svg = <<-SVG.sub(/\n$/, '')
58
+ input_svg = <<-SVG
59
59
  <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><!-- This is a comment --></svg>
60
60
  SVG
61
- expected_output = <<-SVG.sub(/\n$/, '')
61
+ expected_output = <<-SVG
62
62
  <svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"></svg>
63
63
  SVG
64
64
  allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
@@ -68,10 +68,10 @@ SVG
68
68
 
69
69
  context "and all options" do
70
70
  it "applies all expected transformations to the output" do
71
- input_svg = <<-SVG.sub(/\n$/, '')
71
+ input_svg = <<-SVG
72
72
  <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><!-- This is a comment --></svg>
73
73
  SVG
74
- expected_output = <<-SVG.sub(/\n$/, '')
74
+ expected_output = <<-SVG
75
75
  <svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><title>A title</title>
76
76
  <desc>A description</desc></svg>
77
77
  SVG
@@ -0,0 +1,21 @@
1
+ require 'inline_svg/transform_pipeline'
2
+
3
+ describe InlineSvg::TransformPipeline::Transformations::Size do
4
+ it "adds width and height attributes to a SVG document" do
5
+ document = Nokogiri::XML::Document.parse('<svg>Some document</svg>')
6
+ transformation = InlineSvg::TransformPipeline::Transformations::Size.create_with_value("5% * 5%")
7
+
8
+ expect(transformation.transform(document).to_html).to eq(
9
+ "<svg width=\"5%\" height=\"5%\">Some document</svg>\n"
10
+ )
11
+ end
12
+
13
+ it "adds the same width and height value when only passed one attribute" do
14
+ document = Nokogiri::XML::Document.parse('<svg>Some document</svg>')
15
+ transformation = InlineSvg::TransformPipeline::Transformations::Size.create_with_value("5%")
16
+
17
+ expect(transformation.transform(document).to_html).to eq(
18
+ "<svg width=\"5%\" height=\"5%\">Some document</svg>\n"
19
+ )
20
+ end
21
+ end
@@ -0,0 +1,41 @@
1
+ require 'inline_svg/transform_pipeline'
2
+
3
+ describe InlineSvg::TransformPipeline::Transformations do
4
+ context "looking up transformations" do
5
+ it "returns configured transformations when parameters are supplied" do
6
+ transformations = InlineSvg::TransformPipeline::Transformations.lookup(
7
+ nocomment: 'irrelevant',
8
+ class: 'irrelevant',
9
+ title: 'irrelevant',
10
+ desc: 'irrelevant',
11
+ size: 'irrelevant'
12
+ )
13
+
14
+ expect(transformations.map(&:class)).to match_array([
15
+ InlineSvg::TransformPipeline::Transformations::NoComment,
16
+ InlineSvg::TransformPipeline::Transformations::ClassAttribute,
17
+ InlineSvg::TransformPipeline::Transformations::Title,
18
+ InlineSvg::TransformPipeline::Transformations::Description,
19
+ InlineSvg::TransformPipeline::Transformations::Size
20
+ ])
21
+ end
22
+
23
+ it "returns a benign transformation when asked for an unknown transform" do
24
+ transformations = InlineSvg::TransformPipeline::Transformations.lookup(
25
+ not_a_real_transform: 'irrelevant'
26
+ )
27
+
28
+ expect(transformations.map(&:class)).to match_array([
29
+ InlineSvg::TransformPipeline::Transformations::NullTransformation
30
+ ])
31
+ end
32
+
33
+ it "does not return a transformation when a value is not supplied" do
34
+ transformations = InlineSvg::TransformPipeline::Transformations.lookup(
35
+ title: nil
36
+ )
37
+
38
+ expect(transformations.map(&:class)).to match_array([])
39
+ end
40
+ end
41
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inline_svg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Martin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-20 00:00:00.000000000 Z
11
+ date: 2015-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,12 +113,22 @@ files:
113
113
  - lib/inline_svg/asset_file.rb
114
114
  - lib/inline_svg/finds_asset_paths.rb
115
115
  - lib/inline_svg/railtie.rb
116
+ - lib/inline_svg/transform_pipeline.rb
117
+ - lib/inline_svg/transform_pipeline/transformations.rb
118
+ - lib/inline_svg/transform_pipeline/transformations/class_attribute.rb
119
+ - lib/inline_svg/transform_pipeline/transformations/description.rb
120
+ - lib/inline_svg/transform_pipeline/transformations/no_comment.rb
121
+ - lib/inline_svg/transform_pipeline/transformations/size.rb
122
+ - lib/inline_svg/transform_pipeline/transformations/title.rb
123
+ - lib/inline_svg/transform_pipeline/transformations/transformation.rb
116
124
  - lib/inline_svg/version.rb
117
125
  - spec/asset_file_spec.rb
118
126
  - spec/files/example.svg
119
127
  - spec/finds_asset_paths_spec.rb
120
128
  - spec/helpers/inline_svg_spec.rb
121
129
  - spec/inline_svg_spec.rb
130
+ - spec/transformation_pipeline/transformations/size_spec.rb
131
+ - spec/transformation_pipeline/transformations_spec.rb
122
132
  homepage: ''
123
133
  licenses:
124
134
  - MIT
@@ -149,3 +159,5 @@ test_files:
149
159
  - spec/finds_asset_paths_spec.rb
150
160
  - spec/helpers/inline_svg_spec.rb
151
161
  - spec/inline_svg_spec.rb
162
+ - spec/transformation_pipeline/transformations/size_spec.rb
163
+ - spec/transformation_pipeline/transformations_spec.rb