middleman-svg 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +79 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/middleman-svg.rb +4 -0
  13. data/lib/middleman/svg.rb +13 -0
  14. data/lib/middleman/svg/asset_file.rb +14 -0
  15. data/lib/middleman/svg/extension.rb +66 -0
  16. data/lib/middleman/svg/id_generator.rb +17 -0
  17. data/lib/middleman/svg/io_resource.rb +23 -0
  18. data/lib/middleman/svg/transform_pipeline.rb +12 -0
  19. data/lib/middleman/svg/transform_pipeline/transformations.rb +77 -0
  20. data/lib/middleman/svg/transform_pipeline/transformations/aria_attributes.rb +32 -0
  21. data/lib/middleman/svg/transform_pipeline/transformations/aria_hidden.rb +9 -0
  22. data/lib/middleman/svg/transform_pipeline/transformations/aria_hidden_attribute.rb +9 -0
  23. data/lib/middleman/svg/transform_pipeline/transformations/class_attribute.rb +11 -0
  24. data/lib/middleman/svg/transform_pipeline/transformations/data_attributes.rb +21 -0
  25. data/lib/middleman/svg/transform_pipeline/transformations/description.rb +13 -0
  26. data/lib/middleman/svg/transform_pipeline/transformations/height.rb +9 -0
  27. data/lib/middleman/svg/transform_pipeline/transformations/id_attribute.rb +9 -0
  28. data/lib/middleman/svg/transform_pipeline/transformations/no_comment.rb +13 -0
  29. data/lib/middleman/svg/transform_pipeline/transformations/no_defs.rb +11 -0
  30. data/lib/middleman/svg/transform_pipeline/transformations/preserve_aspect_ratio.rb +9 -0
  31. data/lib/middleman/svg/transform_pipeline/transformations/size.rb +18 -0
  32. data/lib/middleman/svg/transform_pipeline/transformations/style_attribute.rb +11 -0
  33. data/lib/middleman/svg/transform_pipeline/transformations/title.rb +13 -0
  34. data/lib/middleman/svg/transform_pipeline/transformations/transformation.rb +42 -0
  35. data/lib/middleman/svg/transform_pipeline/transformations/width.rb +9 -0
  36. data/lib/middleman/svg/version.rb +5 -0
  37. data/middleman-svg.gemspec +32 -0
  38. data/source/images/example.svg +3 -0
  39. data/source/images/known-document-two.svg +1 -0
  40. data/source/images/known-document.svg +1 -0
  41. data/source/images/other-document.svg +3 -0
  42. data/source/images/some-document.svg +1 -0
  43. data/source/images/some-file.txt +1 -0
  44. metadata +170 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f75c7508eeb8091388a6d0a5750f3fbc99f4bcb9
4
+ data.tar.gz: 99e5f0b678180819ce60c8e61b3f48b9960bf7cb
5
+ SHA512:
6
+ metadata.gz: fb03d634937ca902f894c58e0c4137730ddfd741aaf701a8213907efc6b8b902197d53e210fc258bbe41214e4dd0355df1d2be36f2efed15ba23b66e1db43d25
7
+ data.tar.gz: 9b44f0013e1b41c2702ab6094916a2a8db60b483b58934e053f3e9d98ca1fe89c04541f9e4fcef52506108510a5ba5d5b53a470782987c0fc0d1201e97f3a98d
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at giacomello.damiano@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in middleman-test.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Damiano Giacomello
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.
@@ -0,0 +1,79 @@
1
+ # Middleman::Svg
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'middleman-svg'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Add this line to your config.rb:
16
+
17
+ ```ruby
18
+ activate :inline_svg
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ Styling a SVG document with CSS for use on the web is most reliably achieved by adding classes to the document and embedding it inline in the HTML.
24
+ This gem adds a Middleman helper method (inline_svg) that reads an SVG document, applies a CSS class attribute to the root of the document and then embeds it into a view.
25
+
26
+ ```ruby
27
+ inline_svg "file.svg", class: "some-class"
28
+ ```
29
+
30
+ ## Options
31
+
32
+ key | description
33
+ :---------------------- | :----------
34
+ `id` | set a ID attribute on the SVG
35
+ `class` | set a CSS class attribute on the SVG
36
+ `style` | set a CSS style attribute on the SVG
37
+ `data` | add data attributes to the SVG (supply as a hash)
38
+ `size` | set width and height attributes on the SVG <br/> Can also be set using `height` and/or `width` attributes, which take precedence over `size` <br/> Supplied as "{Width} * {Height}" or "{Number}", so "30px\*45px" becomes `width="30px"` and `height="45px"`, and "50%" becomes `width="50%"` and `height="50%"`
39
+ `title` | add a \<title\> node inside the top level of the SVG document
40
+ `desc` | add a \<desc\> node inside the top level of the SVG document
41
+ `nocomment` | remove comment tags from the SVG document
42
+ `preserve_aspect_ratio` | adds a `preserveAspectRatio` attribute to the SVG
43
+ `aria` | adds common accessibility attributes to the SVG
44
+ `aria_hidden` | adds the `aria-hidden=true` attribute to the SVG
45
+
46
+ Example:
47
+
48
+ ```ruby
49
+ inline_svg(
50
+ "some-document.svg",
51
+ id: 'some-id',
52
+ class: 'some-class',
53
+ data: {some: "value"},
54
+ size: '30% * 20%',
55
+ title: 'Some Title',
56
+ desc: 'Some description',
57
+ nocomment: true,
58
+ no_defs: true,
59
+ preserve_aspect_ratio: 'xMaxYMax meet',
60
+ aria: true,
61
+ aria_hidden: true
62
+ )
63
+ ```
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/middleman-svg. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
74
+
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
79
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "middleman/svg"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ require 'middleman-core'
2
+ require 'middleman/svg'
3
+
4
+ Middleman::Extensions.register(:inline_svg, Middleman::Svg::Extension)
@@ -0,0 +1,13 @@
1
+ require 'middleman/svg/version'
2
+ require 'middleman/svg/extension'
3
+
4
+ require "middleman/svg/asset_file"
5
+ require "middleman/svg/transform_pipeline"
6
+ require "middleman/svg/io_resource"
7
+
8
+ require 'nokogiri'
9
+
10
+ module Middleman
11
+ module Svg
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module Middleman::Svg
2
+ class AssetFile
3
+ class FileNotFound < IOError; end
4
+ UNREADABLE_PATH = ''
5
+
6
+ def self.named(filename)
7
+ root = Middleman::Application.root
8
+ source_path = File.join(root, 'source')
9
+ File.read(File.join(source_path, 'images', source) || File.join(source_path, 'fonts/svg', source) || UNREADABLE_PATH)
10
+ rescue Errno::ENOENT
11
+ raise FileNotFound.new("Asset not found: #{asset_path}")
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,66 @@
1
+ require 'middleman-core'
2
+
3
+ module Middleman
4
+ module Svg
5
+ class Extension < ::Middleman::Extension
6
+ expose_to_template :inline_svg
7
+
8
+ def initialize( app, options_hash={}, &block)
9
+ super
10
+
11
+ require 'active_support/core_ext/string'
12
+ end
13
+
14
+ def inline_svg(filename, transform_params={})
15
+ begin
16
+ svg_file = Middleman::Svg::Extension.read_svg(filename)
17
+ rescue Middleman::Svg::AssetFile::FileNotFound
18
+ return Middleman::Svg::Extension.placeholder(filename) unless transform_params[:fallback].present?
19
+
20
+ if transform_params[:fallback].present?
21
+ begin
22
+ svg_file = Middleman::Svg::Extension.read_svg(transform_params[:fallback])
23
+ rescue Middleman::Svg::AssetFile::FileNotFound
24
+ Middleman::Svg::Extension.placeholder(filename)
25
+ end
26
+ end
27
+ end
28
+
29
+ Middleman::Svg::TransformPipeline.generate_html_from(svg_file, transform_params).html_safe
30
+ end
31
+
32
+ def self.read_svg(filename)
33
+ if Middleman::Svg::IOResource === filename
34
+ Middleman::Svg::IOResource.read filename
35
+ else
36
+ Middleman::Svg::Extension.asset_path(filename)
37
+ end
38
+ end
39
+
40
+ def self.asset_path(source)
41
+ root = Middleman::Application.root
42
+ source_path = File.join(root, 'source')
43
+ if File.exists?(File.join(source_path, 'images', source))
44
+ File.read(File.join(source_path, 'images', source))
45
+ elsif File.exists?(File.join(source_path, 'fonts/svg', source))
46
+ File.read(File.join(source_path, 'fonts/svg', source))
47
+ end
48
+ end
49
+
50
+ def self.placeholder(filename)
51
+ css_class = Middleman::Svg.configuration.svg_not_found_css_class
52
+ not_found_message = "'#{filename}' #{Middleman::Svg::Extension.extension_hint(filename)}"
53
+
54
+ if css_class.nil?
55
+ return "<svg><!-- SVG file not found: #{not_found_message}--></svg>".html_safe
56
+ else
57
+ return "<svg class='#{css_class}'><!-- SVG file not found: #{not_found_message}--></svg>".html_safe
58
+ end
59
+ end
60
+
61
+ def self.extension_hint(filename)
62
+ filename.ends_with?(".svg") ? "" : "(Try adding .svg to your filename) "
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,17 @@
1
+ module Middleman
2
+ module Svg
3
+ class IdGenerator
4
+ class Randomness
5
+ require "securerandom"
6
+ def self.call
7
+ SecureRandom.hex(10)
8
+ end
9
+ end
10
+
11
+ def self.generate(base, salt, randomness: Randomness)
12
+ bytes = Digest::SHA1.digest("#{base}-#{salt}-#{randomness.call}")
13
+ Digest.hexencode(bytes).to_i(16).to_s(36)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ module Middleman
2
+ module Svg
3
+ module IOResource
4
+ def self.===(object)
5
+ object.is_a?(IO) || object.is_a?(StringIO)
6
+ end
7
+
8
+ def self.default_for(object)
9
+ case object
10
+ when StringIO then ''
11
+ when IO then 1
12
+ end
13
+ end
14
+
15
+ def self.read(object)
16
+ start = object.pos
17
+ str = object.read
18
+ object.seek start
19
+ str
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ module Middleman::Svg::TransformPipeline
2
+ def self.generate_html_from(svg_file, transform_params)
3
+ document = Nokogiri::XML::Document.parse(svg_file)
4
+ Transformations.lookup(transform_params).reduce(document) do |doc, transformer|
5
+ transformer.transform(doc)
6
+ end.to_html
7
+ end
8
+ end
9
+
10
+ require 'nokogiri'
11
+ require 'middleman/svg/id_generator'
12
+ require 'middleman/svg/transform_pipeline/transformations'
@@ -0,0 +1,77 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ # Transformations are run in priority order, lowest number first:
3
+ def self.built_in_transformations
4
+ {
5
+ id: { transform: IdAttribute, priority: 1 },
6
+ desc: { transform: Description, priority: 2 },
7
+ title: { transform: Title, priority: 3 },
8
+ aria: { transform: AriaAttributes },
9
+ aria_hidden: { transform: AriaHiddenAttribute },
10
+ class: { transform: ClassAttribute },
11
+ style: { transform: StyleAttribute },
12
+ data: { transform: DataAttributes },
13
+ height: { transform: Height },
14
+ nocomment: { transform: NoComment },
15
+ no_defs: { transform: NoDefs },
16
+ preserve_aspect_ratio: { transform: PreserveAspectRatio },
17
+ size: { transform: Size },
18
+ width: { transform: Width },
19
+ }
20
+ end
21
+
22
+ def self.magnify_priorities(transforms)
23
+ transforms.inject({}) do |output, (name, definition)|
24
+ priority = definition.fetch(:priority, built_in_transformations.size)
25
+
26
+ output[name] = definition.merge( { priority: magnify(priority) } )
27
+ output
28
+ end
29
+ end
30
+
31
+ def self.magnify(priority=0)
32
+ (priority + 1) * built_in_transformations.size
33
+ end
34
+
35
+ def self.all_transformations
36
+ in_priority_order(built_in_transformations)
37
+ end
38
+
39
+ def self.lookup(transform_params)
40
+ all_transformations.map { |name, definition|
41
+ value = params_with_defaults(transform_params)[name]
42
+ definition.fetch(:transform, no_transform).create_with_value(value) if value
43
+ }.compact
44
+ end
45
+
46
+ def self.in_priority_order(transforms)
47
+ transforms.sort_by { |_, options| options.fetch(:priority, transforms.size) }
48
+ end
49
+
50
+ def self.params_with_defaults(params)
51
+ without_empty_values(params)
52
+ end
53
+
54
+ def self.without_empty_values(params)
55
+ params.reject {|key, value| value.nil?}
56
+ end
57
+
58
+ def self.no_transform
59
+ Middleman::Svg::TransformPipeline::Transformations::NullTransformation
60
+ end
61
+ end
62
+
63
+ require 'middleman/svg/transform_pipeline/transformations/transformation'
64
+ require 'middleman/svg/transform_pipeline/transformations/no_defs'
65
+ require 'middleman/svg/transform_pipeline/transformations/no_comment'
66
+ require 'middleman/svg/transform_pipeline/transformations/class_attribute'
67
+ require 'middleman/svg/transform_pipeline/transformations/style_attribute'
68
+ require 'middleman/svg/transform_pipeline/transformations/title'
69
+ require 'middleman/svg/transform_pipeline/transformations/description'
70
+ require 'middleman/svg/transform_pipeline/transformations/size'
71
+ require 'middleman/svg/transform_pipeline/transformations/height'
72
+ require 'middleman/svg/transform_pipeline/transformations/width'
73
+ require 'middleman/svg/transform_pipeline/transformations/id_attribute'
74
+ require 'middleman/svg/transform_pipeline/transformations/data_attributes'
75
+ require 'middleman/svg/transform_pipeline/transformations/preserve_aspect_ratio'
76
+ require 'middleman/svg/transform_pipeline/transformations/aria_attributes'
77
+ require "middleman/svg/transform_pipeline/transformations/aria_hidden_attribute"
@@ -0,0 +1,32 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class AriaAttributes < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ # Add role
6
+ svg["role"] = "img"
7
+
8
+ # Build aria-labelledby string
9
+ aria_elements = []
10
+ svg.search("title").each do |element|
11
+ aria_elements << element["id"] = element_id_for("title", element)
12
+ end
13
+
14
+ svg.search("desc").each do |element|
15
+ aria_elements << element["id"] = element_id_for("desc", element)
16
+ end
17
+
18
+ if aria_elements.any?
19
+ svg["aria-labelledby"] = aria_elements.join(" ")
20
+ end
21
+ end
22
+ end
23
+
24
+ def element_id_for(base, element)
25
+ if element["id"].nil?
26
+ Middleman::Svg::IdGenerator.generate(base, element.text)
27
+ else
28
+ Middleman::Svg::IdGenerator.generate(element["id"], element.text)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class AriaHidden < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ svg["aria-hidden"] = self.value
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class AriaHiddenAttribute < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ svg["aria-hidden"] = self.value
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class ClassAttribute < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ classes = (svg["class"] || "").split(" ")
6
+ classes << value
7
+ svg["class"] = classes.join(" ")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class DataAttributes < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ with_valid_hash_from(self.value).each_pair do |name, data|
6
+ svg["data-#{dasherize(name)}"] = data
7
+ end
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def with_valid_hash_from(hash)
14
+ Hash.try_convert(hash) || {}
15
+ end
16
+
17
+ def dasherize(string)
18
+ string.to_s.gsub(/_/, "-")
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class Description < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ node = Nokogiri::XML::Node.new("desc", doc)
6
+ node.content = value
7
+
8
+ svg.search("desc").each { |node| node.remove }
9
+ svg.prepend_child(node)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class Height < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ svg["height"] = self.value
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class IdAttribute < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ svg["id"] = self.value
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module Middleman::Svg::TransformPipeline
2
+ module Transformations
3
+ class NoComment < Transformation
4
+ def transform(doc)
5
+ with_svg(doc) do |svg|
6
+ svg.xpath("//comment()").each do |comment|
7
+ comment.remove
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Middleman::Svg::TransformPipeline
2
+ module Transformations
3
+ class NoDefs < Transformation
4
+ def transform(doc)
5
+ with_svg(doc) do |svg|
6
+ svg.at_css("defs").remove if !svg.at_css("defs").blank?
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class PreserveAspectRatio < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ svg["preserveAspectRatio"] = self.value
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class Size < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ svg["width"] = width_of(self.value)
6
+ svg["height"] = height_of(self.value)
7
+ end
8
+ end
9
+
10
+ def width_of(value)
11
+ value.split(/\*/).map(&:strip)[0]
12
+ end
13
+
14
+ def height_of(value)
15
+ value.split(/\*/).map(&:strip)[1] || width_of(value)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class StyleAttribute < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ styles = svg["style"].to_s.split(";")
6
+ styles << value
7
+ svg["style"] = styles.join(";")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class Title < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ node = Nokogiri::XML::Node.new("title", doc)
6
+ node.content = value
7
+
8
+ svg.search("title").each { |node| node.remove }
9
+ svg.prepend_child(node)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,42 @@
1
+ module Middleman
2
+ module Svg
3
+ module TransformPipeline
4
+ module Transformations
5
+ class Transformation
6
+ def self.create_with_value(value)
7
+ self.new(value)
8
+ end
9
+
10
+ attr_reader :value
11
+
12
+ def initialize(value)
13
+ @value = value
14
+ end
15
+
16
+ def transform(*)
17
+ raise "#transform should be implemented by subclasses of Transformation"
18
+ end
19
+
20
+ # Parses a document and yields the contained SVG nodeset to the given block
21
+ # if it exists.
22
+ #
23
+ # Returns a Nokogiri::XML::Document.
24
+ def with_svg(doc)
25
+ doc = Nokogiri::XML::Document.parse(
26
+ doc.to_html(encoding: "UTF-8"), nil, "UTF-8"
27
+ )
28
+ svg = doc.at_css("svg")
29
+ yield svg if svg && block_given?
30
+ doc
31
+ end
32
+ end
33
+
34
+ class NullTransformation < Transformation
35
+ def transform(doc)
36
+ doc
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ module Middleman::Svg::TransformPipeline::Transformations
2
+ class Width < Transformation
3
+ def transform(doc)
4
+ with_svg(doc) do |svg|
5
+ svg["width"] = self.value
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Middleman
2
+ module Svg
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'middleman/svg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.name = "middleman-svg"
9
+ spec.version = Middleman::Svg::VERSION
10
+ spec.authors = ["Damiano Giacomello"]
11
+ spec.email = ["giacomello.damiano@gmail.com"]
12
+
13
+ spec.summary = "A simple helper to generate inline SVG with Middleman"
14
+ spec.description = "A simple helper to generate inline SVG content with Middleman"
15
+ spec.homepage = "https://github.com/cantierecreativo/middleman-svg"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+
29
+ spec.add_runtime_dependency "middleman-core", ">= 4.0.0"
30
+ spec.add_runtime_dependency "activesupport", ">= 3.0"
31
+ spec.add_runtime_dependency "nokogiri", ">= 1.6"
32
+ end
@@ -0,0 +1,3 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg"
2
+ xml:lang="en"
3
+ role="presentation"><!-- This is a test comment --></svg>
@@ -0,0 +1 @@
1
+ <svg>other interesting content</svg>
@@ -0,0 +1 @@
1
+ <svg>Another known document</svg>
@@ -0,0 +1,3 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
3
+ <svg>Other document</svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M3.273 7.151l-2.917-2.916c-.23.665-.356 1.361-.356 2.057 0 1.608.624 3.216 1.851 4.442 1.35 1.351 3.163 1.957 4.928 1.821.933-.072 1.851.268 2.513.93l9.646 9.646c.58.579 1.338.869 2.097.869 1.636 0 2.965-1.326 2.965-2.965 0-.759-.29-1.518-.868-2.097l-9.647-9.646c-.661-.662-1.002-1.581-.93-2.514.136-1.766-.47-3.578-1.821-4.928-.372-.372-.778-.686-1.209-.945l-6.252 6.246zm18.727 13.849c0 .552-.448 1-1 1s-1-.448-1-1 .448-1 1-1 1 .447 1 1zm-12.153-13.396l-3.061 3.061-2.566-2.567 3.062-3.061 2.565 2.567zm-.933.096l-.762.761-1.705-1.705.762-.762 1.705 1.706zm-2.991-.42l-.761.762 1.706 1.705.762-.762-1.707-1.705zm2.484-6.903l-2.893 2.893-2.412-2.412c.953-.556 2.044-.858 3.165-.858.707 0 1.425.12 2.128.373l.012.004z"/></svg>
@@ -0,0 +1 @@
1
+ Some file contents.
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-svg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Damiano Giacomello
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-19 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: middleman-core
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: nokogiri
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '1.6'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '1.6'
97
+ description: A simple helper to generate inline SVG content with Middleman
98
+ email:
99
+ - giacomello.damiano@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/middleman-svg.rb
115
+ - lib/middleman/svg.rb
116
+ - lib/middleman/svg/asset_file.rb
117
+ - lib/middleman/svg/extension.rb
118
+ - lib/middleman/svg/id_generator.rb
119
+ - lib/middleman/svg/io_resource.rb
120
+ - lib/middleman/svg/transform_pipeline.rb
121
+ - lib/middleman/svg/transform_pipeline/transformations.rb
122
+ - lib/middleman/svg/transform_pipeline/transformations/aria_attributes.rb
123
+ - lib/middleman/svg/transform_pipeline/transformations/aria_hidden.rb
124
+ - lib/middleman/svg/transform_pipeline/transformations/aria_hidden_attribute.rb
125
+ - lib/middleman/svg/transform_pipeline/transformations/class_attribute.rb
126
+ - lib/middleman/svg/transform_pipeline/transformations/data_attributes.rb
127
+ - lib/middleman/svg/transform_pipeline/transformations/description.rb
128
+ - lib/middleman/svg/transform_pipeline/transformations/height.rb
129
+ - lib/middleman/svg/transform_pipeline/transformations/id_attribute.rb
130
+ - lib/middleman/svg/transform_pipeline/transformations/no_comment.rb
131
+ - lib/middleman/svg/transform_pipeline/transformations/no_defs.rb
132
+ - lib/middleman/svg/transform_pipeline/transformations/preserve_aspect_ratio.rb
133
+ - lib/middleman/svg/transform_pipeline/transformations/size.rb
134
+ - lib/middleman/svg/transform_pipeline/transformations/style_attribute.rb
135
+ - lib/middleman/svg/transform_pipeline/transformations/title.rb
136
+ - lib/middleman/svg/transform_pipeline/transformations/transformation.rb
137
+ - lib/middleman/svg/transform_pipeline/transformations/width.rb
138
+ - lib/middleman/svg/version.rb
139
+ - middleman-svg.gemspec
140
+ - source/images/example.svg
141
+ - source/images/known-document-two.svg
142
+ - source/images/known-document.svg
143
+ - source/images/other-document.svg
144
+ - source/images/some-document.svg
145
+ - source/images/some-file.txt
146
+ homepage: https://github.com/cantierecreativo/middleman-svg
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.6.12
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: A simple helper to generate inline SVG with Middleman
170
+ test_files: []