Skip to content

Instantly share code, notes, and snippets.

@andrewbigger
Last active August 29, 2015 14:04
Show Gist options
  • Save andrewbigger/31efb1b931fcebaf50a6 to your computer and use it in GitHub Desktop.
Save andrewbigger/31efb1b931fcebaf50a6 to your computer and use it in GitHub Desktop.
CXML Node Parent Checker
#!/usr/bin/env ruby
def check doc, options
doc.css(options[:query]).each do |node|
unless node.parent.name == options[:parent]
puts "#{options[:for]} element not wrapped in #{options[:parent]} at line #{node.line}" unless node.parent.name == options[:or]
end
end
end
require 'rubygems'
require 'optparse'
require 'nokogiri'
options = { :log => $stdout,
:verbose => false
}
opt = OptionParser.new do |opt|
opt.banner = "Usage: nested_tag_detect [options] <xml>"
opt.on('--log-file=log file') { |log| options[:log] = log }
opt.on('--v','--verbose') { options[:verbose] = true }
opt.on_tail('-h', '--help') do
puts opt
exit
end
end
opt.parse!(ARGV)
unless ARGV.size == 1
puts opt
exit
end
target_file = ARGV[0]
puts "Checking: #{target_file}"
fpr = File.open(target_file.to_s, "r")
doc = Nokogiri::XML(fpr)
check doc, :for => "Inline element", :query => 'c', :parent => 'p'
check doc, :for => "Space or Tab element", :query => 'anchor', :parent => 'p', :or => 'c'
check doc, :for => "Space or Tab element", :query => 't, s', :parent => 'p', :or => 'c'
check doc, :for => "Image element", :query => 'image', :parent => 'context', :or => 'section'
check doc, :for => "Context", :query => 'context', :parent => 'section'
check doc, :for => "Section", :query => 'section', :parent => 'content'
fpr.close
puts "Done"
puts " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment