Skip to content

Using HTMLProofer From Ruby and Travis

James Smith edited this page Sep 28, 2017 · 2 revisions

Most popular HTMLProofer options can be set using the command line. But for a truly customizable experience you will need to use Ruby.

This document shows you all the ways your can customize HTMLProofer and its modules. And we give you copy/pastable snippets you can use for your Travis scripts.

Let's review, here is the stupid, easy way to use HTMLProofer on Travis for an HTML project. We won't be using this.

language: ruby
before_install:
 - export NOKOGIRI_USE_SYSTEM_LIBRARIES=true
addons:
  apt:
    packages:
    - libcurl4-openssl-dev # required to avoid SSL errors
script:
 - gem install html-proof && htmlproof .

Here is the way Ruby people will use HTMLProofer:

Gemfile:

source "https://rubygems.org"

group :test do
  gem "html-proofer"
end

# Add other build tools, like Jekyll, if you need them

Rakefile:

abort('Please run this using `bundle exec rake`') unless ENV["BUNDLE_BIN_PATH"]
require 'html-proofer'

desc "Test the website"
task :test => [:build, 'html:check'] do
  options = {
    :check_sri => true,
    :check_external_hash => true,
    :check_html => true,
    :check_img_http => true,
    :check_opengraph => true,
    :enforce_https => true,
    :cache => {
      :timeframe => '6w'
    }
  }
  begin
    HTMLProofer.check_directory(".", options).run
  rescue => msg
    puts "#{msg}"
  end
end

task :default => [:test]

.travis.yml

language: ruby

script:
 - bundle exec rake test

cache:
  directories:
  - $TRAVIS_BUILD_DIR/tmp/.htmlproofer #https://github.com/gjtorikian/html-proofer/issues/381
  
env:
  global:
  - NOKOGIRI_USE_SYSTEM_LIBRARIES=true # speeds up installation of html-proofer

addons:
  apt:
    packages:
    - libcurl4-openssl-dev # required to avoid SSL errors

sudo: false # route your build to the container-based infrastructure for a faster build

Further sections in this file will show you how to customize the Rakefile part to set up HTMLProofer in new and interesting ways.


Configuring parallel

The parallel module lets you run multiple fetches at the same time.

Use something like:

HTMLProofer.check_directories(["out/"], {:extension => ".htm", :parallel => { :in_processes => 3} })