Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@brandonmwest
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonmwest/6f8a497c0f3dedfe601e to your computer and use it in GitHub Desktop.
Save brandonmwest/6f8a497c0f3dedfe601e to your computer and use it in GitHub Desktop.
Convert tables to {% parameters %} liquid blocks
require 'rubygems'
require 'json'
require 'nokogiri'
require 'nokogiri-pretty'
require 'iconv'
require 'fileutils'
html_files = File.join("/Users/brandonwest/SendGrid/docs/source/API_Reference", "**", "*.{md,html}")
Dir.glob html_files do |html_file|
next if html_file == '.' or html_file == '..' or html_file.include?('v1')
puts "Looking for tables in #{html_file}"
file = File.open(html_file, "r:UTF-8")
html = file.read
file.close
contents = html.clone
contents.gsub!(/<table id="parameters-(.*?)" class=".*?(.*?)<\/table>/m) do |match|
identifier = Regexp.last_match[1]
match.gsub!(/<\/table>/,"")
table_html = '<table id="parameters-' + identifier + '" class="table table-bordered table-striped">' + match.to_s.gsub!("\n","") + '</table>'
table_html.gsub!(/<table class="table table-bordered table-striped">/,"")
xml = Nokogiri.XML(table_html, nil, "UTF-8")
pretty_html = Iconv.conv 'UTF-8', 'iso8859-1', xml.human
params = "\n{% parameters #{identifier} %}\n"
pretty_html.gsub!(/<tr>(.*?)<\/tr>/m) do |row|
parameter = " {% parameter "
row.scan(/<td>(.*?)<\/td>/m) do
parameter += "'" + Regexp.last_match[1].to_s.strip + "' "
end
parameter.gsub!(/Can't/,'Can not')
parameter.gsub!(/It's/,'It is')
parameter.gsub!(/can't/,'can not')
parameter.gsub!(/it's/,'it is')
parameter.gsub!(/\n/,'')
parameter.gsub!(/\s{2,}/,' ')
parameter += "%}\n"
if parameter.strip != "{% parameter %}"
params += parameter
end
end
params += "{% endparameters %}"
params
end
contents.gsub!(/<\?xml version="1\.0" encoding="ISO-8859-1"\?>\s*/,"")
output_path = html_file
dirname = File.dirname(output_path)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end
FileUtils.rm_f(output_path)
file = File.new(output_path,"w:UTF-8")
file.write(contents)
file.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment