Skip to content

Instantly share code, notes, and snippets.

@ebot
Last active August 29, 2015 14:16
Show Gist options
  • Save ebot/dbdd69bc1a305103af78 to your computer and use it in GitHub Desktop.
Save ebot/dbdd69bc1a305103af78 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wKU
# Convert XML to HTML
#
# Created By Ed Botzum
#
# This script uses the EDM XSL Stylesheet to convert XMLs to HTMLs.
require 'nokogiri'
require 'csv'
xsl_template = Nokogiri::XSLT(File.open('Ikm837Tran.xsl'))
if ARGV.count < 2
puts 'Please specify the data directory with --data_dir'
exit
end
puts 'Converting XML to HTML'
index_path = ARGV[1]
header_created = false
new = CSV.open( File.join( index_path, 'index_fixed.txt' ), 'w', { :col_sep => ';', :force_quotes => true } )
CSV.foreach( File.join( index_path, 'index.txt' ), { :headers => true, :col_sep => ';' } ) do |row|
unless header_created
new << row.headers
header_created = true
end
file = File.join index_path, row['file_name']
if file.downcase.include? 'xml'
puts " Converting #{file} to html"
document = Nokogiri::XML File.read(file)
html = xsl_template.transform document
html_file = File.new file.downcase.gsub( 'xml', 'html' ), 'w'
html_file << html
html_file.close
row['file_name'] = row['file_name'].downcase.gsub( 'xml', 'html' )
end
new << row
end
new.close
puts 'Done Converting XML to HTML'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment