middleman-inline_svg 0.1.0

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: b49a12d26d8348e8b53221b507f314679359a4df62168b4b684fba31ee1a20a7
4
+ data.tar.gz: 802be13e0118f08a60ded737f93855e00d886dd7200cd27f4e6dd6083dd2a113
5
+ SHA512:
6
+ metadata.gz: 625460b52c1b49dbb1faabbc3d35944b4adac21beed1af786fe3f47ba34460c1e86fbec6bd4c495c0c076533d242138590027a4c0e6bdbc794c818c4d61a003d
7
+ data.tar.gz: 0e81268e23410e0e761816be3ba4983c23452434af31bb267628f3cfb6610e670ba40f99db6b7b003c0324a72b7f2c360ed5a3607e65da0e61630376c434d5dd
@@ -0,0 +1,36 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: circleci/ruby:2.5.1-node
6
+
7
+ working_directory: ~/repo
8
+
9
+ steps:
10
+ - checkout
11
+
12
+ - restore_cache:
13
+ keys:
14
+ - v1-dependencies-{{ checksum "Gemfile" }}
15
+ - v1-dependencies-
16
+
17
+ - run:
18
+ name: install dependencies
19
+ command: |
20
+ bundle install --path vendor/bundle
21
+
22
+ - save_cache:
23
+ paths:
24
+ - ./vendor/bundle
25
+ key: v1-dependencies-{{ checksum "Gemfile" }}
26
+
27
+ - run:
28
+ name: run tests
29
+ command: |
30
+ bundle exec rake
31
+
32
+ - store_test_results:
33
+ path: /tmp/test-results
34
+ - store_artifacts:
35
+ path: /tmp/test-results
36
+ destination: test-results
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ # Ignore bundler lock file
2
+ /Gemfile.lock
3
+
4
+ # Ignore pkg folder
5
+ /pkg
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # If you do not have OpenSSL installed, update
2
+ # the following line to use "http://" instead
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in middleman-inline_svg.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem "rake"
10
+ gem "rdoc"
11
+ gem "yard"
12
+ end
13
+
14
+ group :test do
15
+ gem "minitest"
16
+ end
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # Middleman Inline SVG
2
+
3
+ [![CircleCI](https://circleci.com/gh/thoughtbot/middleman-inline_svg.svg?style=svg)](https://circleci.com/gh/thoughtbot/middleman-inline_svg)
4
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rake/clean"
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.pattern = "test/**/*_test.rb"
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,4 @@
1
+ PROJECT_ROOT_PATH = File.dirname(File.dirname(File.dirname(__FILE__)))
2
+ require "middleman-core"
3
+ require "middleman-core/step_definitions"
4
+ require File.join(PROJECT_ROOT_PATH, "lib", "middleman-inline_svg")
@@ -0,0 +1,5 @@
1
+ require "middleman-core"
2
+ require "middleman-inline_svg/inline_svg"
3
+ require "middleman-inline_svg/middleman-inline_svg"
4
+
5
+ ::Middleman::Extensions.register(:inline_svg, MiddlemanInlineSVG)
@@ -0,0 +1,42 @@
1
+ require "nokogiri"
2
+
3
+ class InlineSVG
4
+ attr_reader :file_name, :options, :title
5
+
6
+ def initialize(file_name, options = {})
7
+ @file_name = file_name
8
+ @title = options.delete(:title)
9
+ @options = options
10
+ end
11
+
12
+ def to_html
13
+ doc = asset_doc(file_name)
14
+ svg = doc.at_css("svg")
15
+
16
+ if title
17
+ add_title(doc, svg, title)
18
+ end
19
+
20
+ options.each do |key, value|
21
+ svg[key.to_s.tr("_", "-")] = value
22
+ end
23
+
24
+ doc.to_html
25
+ end
26
+
27
+ private
28
+
29
+ def add_title(doc, svg, title)
30
+ title_node = ::Nokogiri::XML::Node.new "title", doc
31
+ title_node.content = title
32
+
33
+ svg.search("title").each(&:remove)
34
+ svg.prepend_child(title_node)
35
+ end
36
+
37
+ def asset_doc(file_name)
38
+ File.open(file_name) do |f|
39
+ ::Nokogiri::XML(f)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ require "middleman-inline_svg/inline_svg"
2
+
3
+ class MiddlemanInlineSVG < ::Middleman::Extension
4
+ expose_to_template :inline_svg
5
+
6
+ def initialize(app, options_hash = {}, &block)
7
+ super
8
+ end
9
+
10
+ def inline_svg(file_name, options = {})
11
+ InlineSVG.new(asset_file(file_name), options).to_html
12
+ end
13
+
14
+ private
15
+
16
+ def asset_file(file_name)
17
+ File.join(app.config[:source], app.config[:images_dir], file_name)
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path("lib", __dir__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "middleman-inline_svg"
7
+ s.version = "0.1.0"
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Daniel Barber", "Tyson Gach"]
10
+ s.email = ["github@danbarber.me", "tyson@thoughtbot.com"]
11
+ # s.homepage = "http://example.com"
12
+ s.summary = "Inline your SVG's"
13
+ s.description = "Inline your SVG's and style them with CSS."
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map do |f|
18
+ File.basename(f)
19
+ end
20
+ s.require_paths = ["lib"]
21
+
22
+ # The version of middleman-core your extension depends on
23
+ s.add_runtime_dependency("middleman-core", [">= 3.4.1"])
24
+
25
+ # Additional dependencies
26
+ s.add_runtime_dependency("nokogiri", [">= 1.8"])
27
+ end
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <svg width="64px" height="64px" viewBox="0 0 64 64" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
3
+ <circle id="Oval" fill="#000000" cx="32" cy="32" r="30"></circle>
4
+ </svg>
@@ -0,0 +1,67 @@
1
+ require "minitest/autorun"
2
+
3
+ require_relative "../../lib/middleman-inline_svg/inline_svg"
4
+
5
+ class TestInlineSVG < Minitest::Test
6
+ def test_it_adds_a_title
7
+ new_svg = InlineSVG.new(
8
+ "test/fixtures/circle.svg",
9
+ title: "Circle",
10
+ ).to_html
11
+
12
+ expected_svg = <<~SVG
13
+ <svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"64px\" height=\"64px\" viewBox=\"0 0 64 64\" version=\"1.1\"><title>Circle</title>
14
+ <circle id="Oval" fill="#000000" cx="32" cy="32" r="30"></circle>
15
+ </svg>
16
+ SVG
17
+
18
+ assert_equal new_svg, expected_svg
19
+ end
20
+
21
+ def test_it_adds_a_class
22
+ new_svg = InlineSVG.new(
23
+ "test/fixtures/circle.svg",
24
+ class: "circle",
25
+ ).to_html
26
+
27
+ expected_svg = <<~SVG
28
+ <svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"64px\" height=\"64px\" viewBox=\"0 0 64 64\" version=\"1.1\" class="circle">
29
+ <circle id="Oval" fill="#000000" cx="32" cy="32" r="30"></circle>
30
+ </svg>
31
+ SVG
32
+
33
+ assert_equal new_svg, expected_svg
34
+ end
35
+
36
+ def test_it_adds_multiple_attributes
37
+ new_svg = InlineSVG.new(
38
+ "test/fixtures/circle.svg",
39
+ class: "circle",
40
+ role: "img",
41
+ id: "circle",
42
+ ).to_html
43
+
44
+ expected_svg = <<~SVG
45
+ <svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"64px\" height=\"64px\" viewBox=\"0 0 64 64\" version=\"1.1\" class="circle" role="img" id="circle">
46
+ <circle id="Oval" fill="#000000" cx="32" cy="32" r="30"></circle>
47
+ </svg>
48
+ SVG
49
+
50
+ assert_equal new_svg, expected_svg
51
+ end
52
+
53
+ def test_it_transforms_attribute_names
54
+ new_svg = InlineSVG.new(
55
+ "test/fixtures/circle.svg",
56
+ aria_hidden: true,
57
+ ).to_html
58
+
59
+ expected_svg = <<~SVG
60
+ <svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"64px\" height=\"64px\" viewBox=\"0 0 64 64\" version=\"1.1\" aria-hidden="true">
61
+ <circle id="Oval" fill="#000000" cx="32" cy="32" r="30"></circle>
62
+ </svg>
63
+ SVG
64
+
65
+ assert_equal new_svg, expected_svg
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-inline_svg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Barber
8
+ - Tyson Gach
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-04-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: middleman-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 3.4.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.4.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: nokogiri
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '1.8'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '1.8'
42
+ description: Inline your SVG's and style them with CSS.
43
+ email:
44
+ - github@danbarber.me
45
+ - tyson@thoughtbot.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".circleci/config.yml"
51
+ - ".gitignore"
52
+ - Gemfile
53
+ - README.md
54
+ - Rakefile
55
+ - features/support/env.rb
56
+ - lib/middleman-inline_svg.rb
57
+ - lib/middleman-inline_svg/inline_svg.rb
58
+ - lib/middleman-inline_svg/middleman-inline_svg.rb
59
+ - middleman-inline_svg.gemspec
60
+ - test/fixtures/circle.svg
61
+ - test/middleman-inline_svg/inline_svg_test.rb
62
+ homepage:
63
+ licenses: []
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.7.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Inline your SVG's
85
+ test_files:
86
+ - features/support/env.rb
87
+ - test/fixtures/circle.svg
88
+ - test/middleman-inline_svg/inline_svg_test.rb