-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
utils.rb
37 lines (31 loc) · 873 Bytes
/
utils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true
require 'nokogiri'
module HTMLProofer
module Utils
def pluralize(count, single, plural)
"#{count} #{(count == 1 ? single : plural)}"
end
def create_nokogiri(path)
content = if File.exist?(path) && !File.directory?(path)
File.open(path).read
else
path
end
Nokogiri::HTML(clean_content(content))
end
def swap(href, replacement)
replacement.each do |link, replace|
href = href.gsub(link, replace)
end
href
end
# address a problem with Nokogiri's parsing URL entities
# problem from http://git.io/vBYU1
# solution from http://git.io/vBYUi
def clean_content(string)
string.gsub(%r{(?:https?:)?//([^>]+)}i) do |url|
url.gsub(/&(?!amp;)/, '&')
end
end
end
end