a11yist/lib/tasks/import.rake
2024-10-28 22:39:01 +01:00

124 lines
5 KiB
Ruby

# frozen_string_literal: true
URL = "https://outline-rocks.github.io/wcag/translations/WCAG21-de/"
WCAG_22_EN_URL ="https://www.w3.org/TR/WCAG22/"
def import_wcag22en
doc = Nokogiri::HTML5(URI.open(WCAG_22_EN_URL))
standards = [Standard.find_by(name_de: "WCAG 2.2"), Standard.find_by(name_de: "EN 301 549")]
doc.css("section.principle").each do |principle_node|
_principle_id = principle_node.attributes["id"].value
principle_title = principle_node.css("h2").first.content.scan(/([a-zA-Z]+)/)
principle = Principle.find_by!(name_en: principle_title)
principle_node.css("section.guideline").each do |guideline_node|
guideline_node.css("section.guideline").each do |sc|
sc_number, sc_title = sc.css("h4")
.first
.content
.scan(/Success Criterion (\d+\.\d+\.\d+) (.*)/)
.first
sc_level = sc.css("p.conformance-level").first&.content&.scan(/\(Level (A+)\)/)&.first&.last
sc_url = sc.css("a.self-link").first.attr("href")
sc_conformity_notice = sc.css("div.note p").to_a.map(&:to_s).join("\n")
full_text = sc.children
.select { _1.class == Nokogiri::XML::Element }
.select { _1.attr("class").nil? || _1.attr("class") == "note" }
.reduce("") { |str, node| str + node.to_s }
check = Check.find_or_initialize_by(external_number: sc_number)
new_standards = Set.new(check.standards)
new_standards += standards
check.name_de = sc_title
check.name_en = sc_title
check.principle = principle
check.standards = new_standards
check.applicable_to_app = check.applicable_to_web = true
check.external_number = sc_number
check.external_url = "#{WCAG_22_EN_URL}#{sc_url}"
check.conformity_level = sc_level&.to_sym
check.conformity_notice_de = sc_conformity_notice
check.criterion_de = "<div>#{full_text}</div>".gsub('href="#', %(href="#{WCAG_22_EN_URL}#))
check.save!
end
end
end
@output
end
def import_wcag21de
doc = Nokogiri::HTML5(URI.open(URL))
standard = Standard.find_by(name_de: "WCAG 2.1")
doc.css("section.principle").each do |principle_node|
_principle_id = principle_node.attributes["id"].value
principle_title = principle_node.css("h2").first.content.scan(/[\d\.]+ ([a-zA-ZöäüÖÄÜ]+)/)
principle = Principle.find_by!(name_de: principle_title)
principle_node.css("section.guideline").each do |guideline_node|
# puts guideline_node.attr("id")
# puts guideline_node.css("h3").size
# next unless guideline_node.css("h3").first
guideline_title = guideline_node.css("h3").first.content
guideline_text = guideline_node.css("p").first
guideline_number = guideline_title.scan(/Richtlinie \d+\.(\d+).*/).first.last
# guideline = principle.guidelines.find_or_create_by(number: guideline_number)
# guideline.title = guideline_title.scan(/Richtlinie \d+\.\d+(.*)/).first.last
# guideline.text = guideline_text
# guideline.save!
guideline_node.css("section.sc").each do |sc|
sc_title = sc.css("h4").first.content.scan(/Erfolgskriterium \d+\.\d+\.\d+ (.*)/).first.last
sc_number = sc.css("h4").first.content.scan(/Erfolgskriterium (\d+\.\d+\.\d+).*/).first.last
sc_level = sc.css("p").first.content.scan(/\(Stufe (A+)\)/).first.last
sc_url = sc.css("h4 a.self-link").first.attr("href")
sc_conformity_notice = sc.css("div.note p").first&.content
text = "<div>#{guideline_text}<br>#{sc.css('p')[1]}"
text += "<p>#{sc.css('dl').first&.to_s}</p>" if sc.css("dl").first
# _sc_number = sc_title.scan(/Erfolgskriterium \d+\.\d+\.(\d+).*/).first.last
check = Check.find_or_initialize_by(external_number: sc_number)
check.name_de = sc_title
check.principle = principle
check.standards << standard unless check.standards.include?(standard)
check.applicable_to_app = check.applicable_to_web = true
check.external_number = sc_number
check.external_url = "#{URL}#{sc_url}"
check.conformity_level = sc_level.to_sym
check.conformity_notice_de = sc_conformity_notice
check.criterion_de = "#{text}</div>".gsub('href="#', %(href="#{URL}#))
check.save!
end
end
end
@output
end
namespace :import do
desc "Import WCAG2.1 in german"
task wcag21de: :environment do
Check.transaction do
import_wcag21de
# raise ActiveRecord::Rollback
end
end
desc "Import WCAG2.2 in english"
task wcag22en: :environment do
Check.transaction do
import_wcag22en
# raise ActiveRecord::Rollback
end
end
desc "Import EN 301 549"
task en301549de: :environment do
Check.transaction do
import_en301549de
end
end
end