Created
August 10, 2017 03:11
-
-
Save kivikakk/f166e359ad5bed29ed2a4a1365f3f7b6 to your computer and use it in GitHub Desktop.
sourcepos verifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'nokogiri' | |
| doc = File.read(ARGV[0]).split("\n") | |
| xml = Nokogiri::XML(`build/src/cmark-gfm -t xml --sourcepos #{ARGV[0]}`) | |
| xml.css('text, code').each do |el| | |
| from, to = el['sourcepos'].split('-').map {|r| r.split(':').map(&:to_i)} | |
| if from[0] != to[0] | |
| puts "Cannot verify #{[from, to].inspect}" | |
| next | |
| end | |
| comp = doc[from[0] - 1].b[from[1] - 1..to[1] - 1].force_encoding('utf-8') | |
| if comp.nil? | |
| puts "#{[from, to].inspect}: got nil" | |
| next | |
| end | |
| if el.name == 'text' | |
| comp = comp.gsub(/\\(.)/, '\1') | |
| else | |
| comp = comp.strip | |
| end | |
| if el.inner_text != comp | |
| puts "#{[from, to].inspect}: Expecting #{el.inner_text.inspect}, actual doc has #{comp.inspect}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment