Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Open Graph #359

Merged
merged 33 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
49083eb
Describe the option
Dec 14, 2015
7fb2275
Add the option to the binary
Dec 14, 2015
68e1f3d
Add the option to the configuration
Dec 14, 2015
2cbf33b
Add specs or opengraph url
Dec 14, 2015
5c39a85
Pass the line number to the url list
Dec 14, 2015
845bbee
Add specs or opengraph image
Dec 14, 2015
e119483
Add specs for invalid opengraph image
Dec 15, 2015
7810ace
Add specs for empty opengraph
Dec 15, 2015
ce16965
Make the spec more expressive
Dec 15, 2015
23d5280
Add a fixed version of @penibelst's test
gjtorikian Dec 23, 2015
81d8d42
Merge branch 'master' of https://github.com/gjtorikian/html-proofer i…
peternewman Oct 3, 2016
29937a2
Standardise file location
peternewman Oct 3, 2016
9125b4f
More location standardisation
peternewman Oct 3, 2016
54cef9d
Fix the camel case comments
peternewman Oct 3, 2016
ffe521c
Fix the namespace name
peternewman Oct 3, 2016
2f46326
Try and fix the remaining syntax issues
peternewman Oct 3, 2016
361393c
Fix the tests
peternewman Oct 3, 2016
0a5a7a5
Fix the issue reporting
peternewman Oct 3, 2016
8e0b01c
Actually fix the issue reporting
peternewman Oct 3, 2016
217f566
Fix the typo
peternewman Oct 3, 2016
9d6e9c7
Fix the outstanding tests
peternewman Oct 3, 2016
d2c13a3
Try and fix the tests
peternewman Oct 4, 2016
43cbae3
Fix the incorrect URLs
peternewman Oct 4, 2016
0f3c2b4
Correct the syntax
peternewman Oct 4, 2016
2eb2583
Use our own subclassed element
peternewman Oct 4, 2016
d665d29
Correct the class name
peternewman Oct 4, 2016
4670b47
Don't override the URL method, check if a URL is valid
peternewman Oct 4, 2016
7578a97
Keep trying to fix the tests
peternewman Oct 4, 2016
1633351
Revert back some of the changes
peternewman Oct 4, 2016
abd5051
Correct the number of args
peternewman Oct 4, 2016
456b02a
Add more tests
peternewman Oct 5, 2016
4cb037b
Handle more edge cases
peternewman Oct 5, 2016
ccd1e73
Update the VCR
peternewman Oct 5, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ The `HTMLProofer` constructor takes an optional hash of additional options:
| `assume_extension` | Automatically add extension (e.g. `.html`) to file paths, to allow extensionless URLs (as supported by Jekyll 3 and GitHub Pages) | `false` |
| `check_external_hash` | Checks whether external hashes exist (even if the webpage exists). This slows the checker down. | `false` |
| `check_favicon` | Enables the favicon checker. | `false` |
| `check_opengraph` | Enables the Open Graph checker. | `false` |
| `check_html` | Enables HTML validation errors from Nokogiri | `false` |
|`checks_to_ignore`| An array of Strings indicating which checks you'd like to not perform. | `[]`
| `directory_index_file` | Sets the file to look for when a link refers to a directory. | `index.html` |
Expand Down
1 change: 1 addition & 0 deletions bin/htmlproofer
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Mercenary.program(:htmlproofer) do |p|
p.option 'check_favicon', '--check-favicon', 'Enables the favicon checker (default: `false`).'
p.option 'check_html', '--check-html', 'Enables HTML validation errors from Nokogiri (default: `false`).'
p.option 'check_img_http', '--check-img-http', 'Fails an image if it\'s marked as `http` (default: `false`).'
p.option 'check_opengraph', '--check-opengraph', 'Enables the Open Graph checker (default: `false`).'
p.option 'directory_index_file', '--directory-index-file', String, 'Sets the file to look for when a link refers to a directory. (default: `index.html`)'
p.option 'disable_external', '--disable-external', 'If `true`, does not run the external link checker, which can take a lot of time (default: `false`)'
p.option 'empty_alt_ignore', '--empty-alt-ignore', 'If `true`, ignores images with empty alt tags'
Expand Down
44 changes: 44 additions & 0 deletions lib/html-proofer/check/opengraph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# encoding: utf-8

class OpenGraphElement < ::HTMLProofer::Element
def src
@content
end

def url
src
end
end

class OpenGraphCheck < ::HTMLProofer::Check
def missing_src?
!@opengraph.src
end

def empty_src?
blank?(@opengraph.src)
end

def run
@html.css('meta[property="og:url"], meta[property="og:image"]').each do |m|
@opengraph = OpenGraphElement.new(m, self)

next if @opengraph.ignore?

# does the opengraph exist?
if missing_src?
add_issue('open graph has no content attribute', line: m.line, content: m.content)
elsif empty_src?
add_issue('open graph content attribute is empty', line: m.line, content: m.content)
elsif !@opengraph.valid?
add_issue("#{@opengraph.src} is an invalid URL", line: m.line)
elsif @opengraph.remote?
add_to_external_urls(@opengraph.url)
else
add_issue("internal open graph #{@opengraph.url} does not exist", line: m.line, content: m.content) unless @opengraph.exists?
end
end

external_urls
end
end
1 change: 1 addition & 0 deletions lib/html-proofer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Configuration
:check_favicon => false,
:check_html => false,
:check_img_http => false,
:check_opengraph => false,
:checks_to_ignore => [],
:directory_index_file => 'index.html',
:disable_external => false,
Expand Down
1 change: 1 addition & 0 deletions lib/html-proofer/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def checks
@checks = HTMLProofer::Check.subchecks.map(&:name)
@checks.delete('FaviconCheck') unless @options[:check_favicon]
@checks.delete('HtmlCheck') unless @options[:check_html]
@checks.delete('OpenGraphCheck') unless @options[:check_opengraph]
@options[:checks_to_ignore].each { |ignored| @checks.delete(ignored) }
@checks
end
Expand Down
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/opengraph/image-broken.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta property="og:image" content="https://github.com/hearticon.ico" />
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/opengraph/image-empty.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta property="og:image" content="" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta property="og:image" content="doesnotexist.png" />
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/opengraph/image-missing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta property="og:image" />
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/opengraph/image-valid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta property="og:image" content="https://github.com/favicon.ico" />
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/opengraph/url-broken.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta property="og:url" content="http://www.asdo3IRJ395295jsingrkrg4.com/">
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/opengraph/url-empty.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta property="og:url" content="">
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/opengraph/url-missing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta property="og:url">
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/opengraph/url-valid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta property="og:url" content="http://www.example.com/">

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions spec/html-proofer/opengraph_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'spec_helper'

describe 'Open Graph test' do
it 'passes for existing external url' do
url_valid = "#{FIXTURES_DIR}/opengraph/url-valid.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => true })
expect(proofer.failed_tests).to eq []
end

it 'fails for missing url content attribute' do
url_valid = "#{FIXTURES_DIR}/opengraph/url-missing.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => true })
expect(proofer.failed_tests.first).to match(/open graph has no content attribute/)
end

it 'fails for empty url' do
url_valid = "#{FIXTURES_DIR}/opengraph/url-empty.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => true })
expect(proofer.failed_tests.first).to match(/open graph content attribute is empty/)
end

it 'fails for missing external url' do
url_valid = "#{FIXTURES_DIR}/opengraph/url-broken.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => true })
expect(proofer.failed_tests.first).to match(/failed: response code 0/)
end

it 'passes for existing external image' do
url_valid = "#{FIXTURES_DIR}/opengraph/image-valid.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => true })
expect(proofer.failed_tests).to eq []
end

it 'fails for missing image content attribute' do
url_valid = "#{FIXTURES_DIR}/opengraph/image-missing.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => true })
expect(proofer.failed_tests.first).to match(/open graph has no content attribute/)
end

it 'fails for empty image' do
url_valid = "#{FIXTURES_DIR}/opengraph/image-empty.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => true })
expect(proofer.failed_tests.first).to match(/open graph content attribute is empty/)
end

it 'fails for missing external image' do
url_valid = "#{FIXTURES_DIR}/opengraph/image-broken.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => true })
expect(proofer.failed_tests.first).to match(/failed: 406/)
end

it 'fails for missing internal images' do
image_internal_invalid = "#{FIXTURES_DIR}/opengraph/image-internal-broken.html"
proofer = run_proofer(image_internal_invalid, :file, { :check_opengraph => true })
expect(proofer.failed_tests.first).to match(/doesnotexist.png does not exist/)
end

it 'passes for missing external url when not asked to check' do
url_valid = "#{FIXTURES_DIR}/opengraph/url-broken.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => false })
expect(proofer.failed_tests).to eq []
end

it 'passes for missing external image when not asked to check' do
url_valid = "#{FIXTURES_DIR}/opengraph/image-broken.html"
proofer = run_proofer(url_valid, :file, { :check_opengraph => false })
expect(proofer.failed_tests).to eq []
end
end