Using WCAG-Zoo in other languages

Below are a small number of example scripts that show how to call the WCAG-Zoo scripts from a number of target languages to provide runtime support for accessibility checking.

All of the following snippets will either:

  • Store a specified string my_html as the temporary file accessed by the variable tmp_file or
  • Pass a specified string my_html into the command via stdin

Then:

  1. Execute the WCAG command wcag_zoo.validators.tarsier using Python and store the result as results
  2. Capture the results string and parse it from JSON into the variable json_results
  3. Prints the number of failures for the file

All of the following scripts are public domain samples and not guaranteed to work in production in any way. All scripts should output something similar to /tmp/wcag117015-32930-onps7o 1 failures

Node.JS

File based:

Assuming you have temp installed using npm install temp:

#!/usr/bin/env node
var temp = require('temp'),
    fs   = require('fs'),
    exec = require('child_process').exec;

temp.open('wcag', function(err, info) {
  if (!err) {
    fs.write(info.fd, "<html><head><body><h2>This is wrong, it should be h1");
    fs.close(info.fd, function(err) {
      exec("zookeeper tarsier '" + info.path + "' -J",
        function(err, stdout) {
          results = stdout;
          json_results = JSON.parse(results);
          console.log(
            json_results[0][0],
            json_results[0][1].failures.length,
            "failures"
          );
        }
      );
    });
  }
});

Perl

File based:

#!/usr/bin/env perl
require File::Temp;
use File::Temp ();
use File::Temp qw/ :seekable /;
use JSON;

$my_html = "<html><head><body><h2>This is wrong, it should be h1";

$tmp = File::Temp->new();
print $tmp $my_html;
$tmp->seek( 0, SEEK_END );

$fn = $tmp->filename;

$results = `zookeeper tarsier $fn -J`;
@json_results = decode_json($results);
$filename = @{@{@json_results[0]}[0]}[0];
$len = scalar @{@{@{@{@json_results[0]}[0]}[1]}{'failures'}};
print "$filename $len failures\n";

Python

Included for reference, but WCAG-Zoo can be used in Python by importing validators directly.

File based:

#!/usr/bin/env python2
from __future__ import print_function
import json
import tempfile
import subprocess

my_html = "<html><head><body><h2>This is wrong, it should be h1"

tmp_file = tempfile.NamedTemporaryFile()
tmp_file.write(my_html)
tmp_file.seek(0)

process = subprocess.Popen(
    ["zookeeper", "tarsier", tmp_file.name, "-J"],
    stdout=subprocess.PIPE
)

results = process.communicate()[0]
json_results = json.loads(results)

print(json_results[0][0],
    len(json_results[0][1]['failures']),
    "failures"
)

Ruby

Assuming you have installed json like so: gem install json

File based:

#!/usr/bin/env ruby
require 'json'
require 'tempfile'

my_html = "<html><head><body><h2>This is wrong, it should be h1"

tmp_file = Tempfile.new('foo')
tmp_file.write(my_html)
tmp_file.close

results = `zookeeper tarsier #{tmp_file.path} -J`
json_results = JSON.parse(results)
print json_results[0][0], " ", json_results[0][1]['failures'].size, " failures\n"