preadly-bulbasaur 0.6.1 → 0.7.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 +4 -4
- data/lib/bulbasaur/removals/remove_attributes.rb +14 -0
- data/lib/bulbasaur/removals/remove_tags.rb +14 -0
- data/lib/bulbasaur/utils/normalize_image_sources.rb +32 -0
- data/lib/bulbasaur/version.rb +2 -2
- data/lib/bulbasaur.rb +3 -0
- data/spec/bulbasaur/removals/remove_attributes_spec.rb +45 -0
- data/spec/bulbasaur/removals/remove_tags_spec.rb +39 -0
- data/spec/bulbasaur/utils/normalize_image_sources_spec.rb +25 -0
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0dc0a73c1850ae685cf72ca24ef60eb89f72b28e
|
4
|
+
data.tar.gz: 82073826c2a3eae44106a25afdc82c16cf6ec853
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb0a1cf64c3eb6f738b37e9ee54814c544fe69447053145ae6a8320ade4221dd7ac3b535cff41ae881c9eb662f3b200ad7b157a6750d975251eb2d529b679196
|
7
|
+
data.tar.gz: a75600ad921fad3dd30e56c5fa0e47ce2efba244a5c49b8d12523c1696fa42da23d9f6328ad96eff88e43dfe746a1a762f66b202d8e1c0a83d59c01e76da3da0
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Bulbasaur
|
2
|
+
class RemoveAttributes
|
3
|
+
def initialize(html, banned_attrs)
|
4
|
+
@html = html
|
5
|
+
@banned_attrs = banned_attrs
|
6
|
+
end
|
7
|
+
|
8
|
+
def call
|
9
|
+
parsed_html = Nokogiri::HTML::DocumentFragment.parse @html
|
10
|
+
@banned_attrs.each { |attr| parsed_html.xpath(".//@#{attr}").remove }
|
11
|
+
parsed_html.to_s
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Bulbasaur
|
2
|
+
class RemoveTags
|
3
|
+
def initialize(html, banned_tags)
|
4
|
+
@html = html
|
5
|
+
@banned_tags = banned_tags
|
6
|
+
end
|
7
|
+
|
8
|
+
def call
|
9
|
+
parsed_html = Nokogiri::HTML::DocumentFragment.parse @html
|
10
|
+
@banned_tags.each { |tag| parsed_html.css(tag).remove }
|
11
|
+
parsed_html.to_s
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Bulbasaur
|
2
|
+
class NormalizeImageSources
|
3
|
+
def initialize(html, target_attrs)
|
4
|
+
@html = html
|
5
|
+
@target_attrs = target_attrs
|
6
|
+
end
|
7
|
+
|
8
|
+
def call
|
9
|
+
parsed_html = Nokogiri::HTML::DocumentFragment.parse @html
|
10
|
+
parsed_html.css('img').each do |element|
|
11
|
+
check_for_attrs element
|
12
|
+
end
|
13
|
+
parsed_html.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def check_for_attrs(element)
|
19
|
+
@target_attrs.each do |attr|
|
20
|
+
if element.xpath "@#{attr}"
|
21
|
+
adjust element, "@#{attr}"
|
22
|
+
break
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def adjust(element, attr)
|
28
|
+
element.set_attribute 'src', element.xpath(attr).text
|
29
|
+
element.xpath(attr).remove
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/bulbasaur/version.rb
CHANGED
data/lib/bulbasaur.rb
CHANGED
@@ -4,8 +4,11 @@ require "bulbasaur/extracts/extract_images_from_vimeo"
|
|
4
4
|
require "bulbasaur/extracts/extract_images_from_html"
|
5
5
|
require "bulbasaur/extracts/extract_images_from_all_resources"
|
6
6
|
require "bulbasaur/extracts/extract_text_from_html.rb"
|
7
|
+
require "bulbasaur/removals/remove_tags"
|
8
|
+
require "bulbasaur/removals/remove_attributes"
|
7
9
|
require "bulbasaur/replaces/replace_by_tag_image"
|
8
10
|
require "bulbasaur/utils/normalize_url"
|
11
|
+
require "bulbasaur/utils/normalize_image_sources"
|
9
12
|
require "bulbasaur/version"
|
10
13
|
|
11
14
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Bulbasaur::RemoveAttributes do
|
4
|
+
subject { described_class.new(html, banned_attrs).call }
|
5
|
+
|
6
|
+
describe '#call' do
|
7
|
+
let(:html) do
|
8
|
+
%[
|
9
|
+
<style>
|
10
|
+
div { color: green; width: 1024px; }
|
11
|
+
</style>
|
12
|
+
<div style="height: 100px; width: 100px;"></div>
|
13
|
+
<form>
|
14
|
+
<input type="text">
|
15
|
+
</form>
|
16
|
+
<p style="font-size: x-large">hello!</p>
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when there are no banned attributes' do
|
21
|
+
let(:banned_attrs) { [] }
|
22
|
+
|
23
|
+
it 'returns the HTML code as it was before' do
|
24
|
+
expect(subject).to eq html
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when there are banned attributes' do
|
29
|
+
let(:banned_attrs) { %w(style) }
|
30
|
+
|
31
|
+
it 'returns the HTML code without the banned attributes' do
|
32
|
+
expect(subject.strip.gsub(/\n/, '').squeeze ' ').to eq %[
|
33
|
+
<style>
|
34
|
+
div { color: green; width: 1024px; }
|
35
|
+
</style>
|
36
|
+
<div></div>
|
37
|
+
<form>
|
38
|
+
<input type="text">
|
39
|
+
</form>
|
40
|
+
<p>hello!</p>
|
41
|
+
].strip.gsub(/\n/, '').squeeze ' '
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Bulbasaur::RemoveTags do
|
4
|
+
subject { described_class.new(html, banned_tags).call }
|
5
|
+
|
6
|
+
describe '#call' do
|
7
|
+
let(:html) do
|
8
|
+
%[
|
9
|
+
<style>
|
10
|
+
div { color: green; width: 1024px; }
|
11
|
+
</style>
|
12
|
+
<div style="height: 100px; width: 100px;"></div>
|
13
|
+
<form>
|
14
|
+
<input type="text">
|
15
|
+
</form>
|
16
|
+
<p>hello!</p>
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when there are no banned tags' do
|
21
|
+
let(:banned_tags) { [] }
|
22
|
+
|
23
|
+
it 'returns the HTML code as it was before' do
|
24
|
+
expect(subject).to eq html
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when there are banned tags' do
|
29
|
+
let(:banned_tags) { %w(form style) }
|
30
|
+
|
31
|
+
it 'returns the HTML code without the banned tags' do
|
32
|
+
expect(subject.strip.gsub(/\n/, '').squeeze ' ').to eq %[
|
33
|
+
<div style="height: 100px; width: 100px;"></div>
|
34
|
+
<p>hello!</p>
|
35
|
+
].strip.gsub(/\n/, '').squeeze ' '
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Bulbasaur::NormalizeImageSources do
|
4
|
+
subject { described_class.new(html, target_attrs).call }
|
5
|
+
|
6
|
+
describe '#call' do
|
7
|
+
let(:html) { '<img src="http://somewhere.to/get/a-pixel.gif" data-lazy-src="http://somewhere.to/get/the-real-image.jpg" alt="Image" width="800" height="1200">' }
|
8
|
+
|
9
|
+
context 'when there are no target attributes' do
|
10
|
+
let(:target_attrs) { [] }
|
11
|
+
|
12
|
+
it 'returns the HTML code as it was before' do
|
13
|
+
expect(subject).to eq html
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when there are target attributes' do
|
18
|
+
let(:target_attrs) { %w(data-lazy-src) }
|
19
|
+
|
20
|
+
it 'returns the HTML code with the image tags adjusted' do
|
21
|
+
expect(subject).to eq '<img src="http://somewhere.to/get/the-real-image.jpg" alt="Image" width="800" height="1200">'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: preadly-bulbasaur
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Magno Costa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -95,7 +95,10 @@ files:
|
|
95
95
|
- lib/bulbasaur/extracts/extract_images_from_vimeo.rb
|
96
96
|
- lib/bulbasaur/extracts/extract_images_from_youtube.rb
|
97
97
|
- lib/bulbasaur/extracts/extract_text_from_html.rb
|
98
|
+
- lib/bulbasaur/removals/remove_attributes.rb
|
99
|
+
- lib/bulbasaur/removals/remove_tags.rb
|
98
100
|
- lib/bulbasaur/replaces/replace_by_tag_image.rb
|
101
|
+
- lib/bulbasaur/utils/normalize_image_sources.rb
|
99
102
|
- lib/bulbasaur/utils/normalize_url.rb
|
100
103
|
- lib/bulbasaur/version.rb
|
101
104
|
- spec/bulbasaur/extracts/extract_images_from_all_resources_spec.rb
|
@@ -103,7 +106,10 @@ files:
|
|
103
106
|
- spec/bulbasaur/extracts/extract_images_from_vimeo_spec.rb
|
104
107
|
- spec/bulbasaur/extracts/extract_images_from_youtube_spec.rb
|
105
108
|
- spec/bulbasaur/extracts/extract_inner_text_from_html_spec.rb
|
109
|
+
- spec/bulbasaur/removals/remove_attributes_spec.rb
|
110
|
+
- spec/bulbasaur/removals/remove_tags_spec.rb
|
106
111
|
- spec/bulbasaur/replaces/replace_by_tag_image_spec.rb
|
112
|
+
- spec/bulbasaur/utils/normalize_image_sources_spec.rb
|
107
113
|
- spec/bulbasaur/utils/normalize_url_spec.rb
|
108
114
|
- spec/bulbasaur_spec.rb
|
109
115
|
- spec/spec_helper.rb
|
@@ -136,7 +142,10 @@ test_files:
|
|
136
142
|
- spec/bulbasaur/extracts/extract_images_from_vimeo_spec.rb
|
137
143
|
- spec/bulbasaur/extracts/extract_images_from_youtube_spec.rb
|
138
144
|
- spec/bulbasaur/extracts/extract_inner_text_from_html_spec.rb
|
145
|
+
- spec/bulbasaur/removals/remove_attributes_spec.rb
|
146
|
+
- spec/bulbasaur/removals/remove_tags_spec.rb
|
139
147
|
- spec/bulbasaur/replaces/replace_by_tag_image_spec.rb
|
148
|
+
- spec/bulbasaur/utils/normalize_image_sources_spec.rb
|
140
149
|
- spec/bulbasaur/utils/normalize_url_spec.rb
|
141
150
|
- spec/bulbasaur_spec.rb
|
142
151
|
- spec/spec_helper.rb
|