Ejemplo n.º 1
0
def track_pa11y_stats(pa11y_results, spider):
    """
    Keep track of the number of pa11y errors, warnings, and notices that
    we've seen so far, using the Scrapy stats collector:
    http://doc.scrapy.org/en/1.1/topics/stats.html
    """
    num_err, num_warn, num_notice = pa11y_counts(pa11y_results)
    stats = spider.crawler.stats
    stats.inc_value("pa11y/error", count=num_err, spider=spider)
    stats.inc_value("pa11y/warning", count=num_warn, spider=spider)
    stats.inc_value("pa11y/notice", count=num_notice, spider=spider)
Ejemplo n.º 2
0
def render_html(data_dir, output_dir):
    """
    The main workhorse of this script. Finds all the JSON data files
    from pa11ycrawler, and transforms them into HTML files via Jinja2 templating.
    """
    env = Environment(loader=PackageLoader('pa11ycrawler', 'templates'))
    env.globals["wcag_refs"] = wcag_refs
    pages = []
    counter = collections.Counter()

    copy_assets(output_dir)

    # render detail templates
    template = env.get_template("detail.html")
    for data_file in data_dir.files('*.json'):
        data = json.load(data_file.open())
        num_error, num_warning, num_notice = pa11y_counts(data['pa11y'])
        data["num_error"] = num_error
        data["num_warning"] = num_warning
        data["num_notice"] = num_notice
        rendered_html = template.render(**data)  # pylint: disable=no-member
        # replace `.json` with `.html`
        fname = data_file.namebase + ".html"
        html_path = output_dir / fname
        html_path.write_text(rendered_html, encoding='utf-8')

        data["filename"] = fname
        pages.append(data)

        counter["error"] += num_error
        counter["warning"] += num_warning
        counter["notice"] += num_notice

    def extract_nums(page):
        "Used for sorting"
        return (
            page["num_error"],
            page["num_warning"],
            page["num_notice"],
        )

    # render index template
    index_template = env.get_template("index.html")
    context = {
        "pages": sorted(pages, key=extract_nums, reverse=True),
        "num_error": counter["error"],
        "num_warning": counter["warning"],
        "num_notice": counter["notice"],
    }
    rendered_html = index_template.render(**context)  # pylint: disable=no-member
    html_path = output_dir / "index.html"
    html_path.write_text(rendered_html, encoding='utf-8')
Ejemplo n.º 3
0
def render_html(data_dir, output_dir):
    """
    The main workhorse of this script. Finds all the JSON data files
    from pa11ycrawler, and transforms them into HTML files via Jinja2 templating.
    """
    env = Environment(loader=PackageLoader('pa11ycrawler', 'templates'))
    env.globals["wcag_refs"] = wcag_refs
    pages = []
    counter = collections.Counter()
    grouped_violations = collections.defaultdict(dict)

    # render detail templates
    for data_file in data_dir.files('*.json'):
        data = json.load(data_file.open())
        num_error, num_warning, num_notice = pa11y_counts(data['pa11y'])

        data["num_error"] = num_error
        data["num_warning"] = num_warning
        data["num_notice"] = num_notice
        fname = data_file.namebase + ".html"
        html_path = output_dir / fname
        render_template(env, html_path, 'detail.html', data)

        data["filename"] = fname
        pages.append(data)

        for violation in data['pa11y']:
            violation_id = hashlib.md5(
                (violation['selector'] +
                 violation['code']).encode('utf-8')).hexdigest()

            if violation_id not in grouped_violations[violation['type']]:
                violation['pages'] = []
                grouped_violations[violation['type']][violation_id] = violation
                counter[violation['type']] += 1

            grouped_violations[
                violation['type']][violation_id]['pages'].append({
                    'url':
                    data['url'],
                    'page_title':
                    data['page_title']
                })

    def extract_nums(page):
        "Used to sort pages by violation counts"
        return (
            page["num_error"],
            page["num_warning"],
            page["num_notice"],
        )

    index_path = output_dir / INDEX_TEMPLATE
    render_template(
        env, index_path, INDEX_TEMPLATE, {
            "pages": sorted(pages, key=extract_nums, reverse=True),
            "num_error": counter["error"],
            "num_warning": counter["warning"],
            "num_notice": counter["notice"]
        })

    for violation_type in grouped_violations:
        unique_path = output_dir / u'{}s.html'.format(violation_type)
        render_template(
            env, unique_path, UNIQUE_TEMPLATE, {
                "grouped_violations":
                sorted(grouped_violations[violation_type].values(),
                       key=lambda item: len(item['pages']),
                       reverse=True),
                "current_type":
                violation_type,
                "violation_counts":
                counter
            })
Ejemplo n.º 4
0
def render_html(data_dir, output_dir):
    """
    The main workhorse of this script. Finds all the JSON data files
    from pa11ycrawler, and transforms them into HTML files via Jinja2 templating.
    """
    env = Environment(loader=PackageLoader('pa11ycrawler', 'templates'))
    env.globals["wcag_refs"] = wcag_refs
    pages = []
    counter = collections.Counter()
    grouped_violations = collections.defaultdict(dict)

    # render detail templates
    for data_file in data_dir.files('*.json'):
        data = json.load(data_file.open())
        num_error, num_warning, num_notice = pa11y_counts(data['pa11y'])

        data["num_error"] = num_error
        data["num_warning"] = num_warning
        data["num_notice"] = num_notice
        fname = data_file.namebase + ".html"
        html_path = output_dir / fname
        render_template(env, html_path, 'detail.html', data)

        data["filename"] = fname
        pages.append(data)

        for violation in data['pa11y']:
            violation_id = hashlib.md5(
                (violation['selector'] + violation['code']).encode('utf-8')
            ).hexdigest()

            if violation_id not in grouped_violations[violation['type']]:
                violation['pages'] = []
                grouped_violations[violation['type']][violation_id] = violation
                counter[violation['type']] += 1

            grouped_violations[violation['type']][violation_id]['pages'].append({
                'url': data['url'],
                'page_title': data['page_title']
            })

    def extract_nums(page):
        "Used to sort pages by violation counts"
        return (
            page["num_error"],
            page["num_warning"],
            page["num_notice"],
        )

    index_path = output_dir / INDEX_TEMPLATE
    render_template(env, index_path, INDEX_TEMPLATE, {
        "pages": sorted(pages, key=extract_nums, reverse=True),
        "num_error": counter["error"],
        "num_warning": counter["warning"],
        "num_notice": counter["notice"]
    })

    for violation_type in grouped_violations:
        unique_path = output_dir / u'{}s.html'.format(violation_type)
        render_template(env, unique_path, UNIQUE_TEMPLATE, {
            "grouped_violations": sorted(
                grouped_violations[violation_type].values(),
                key=lambda item: len(item['pages']),
                reverse=True
            ),
            "current_type": violation_type,
            "violation_counts": counter
        })