Skip to content

Instantly share code, notes, and snippets.

@ayucat
Created December 5, 2011 18: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 ayucat/1434589 to your computer and use it in GitHub Desktop.
Save ayucat/1434589 to your computer and use it in GitHub Desktop.
a simple converter of table markuped as html to wikitable on mediawiki
#!/usr/bin/env ruby
# encoding: utf-8
require 'nokogiri'
@doc = Nokogiri::HTML::DocumentFragment.parse <<-EOHTML
<table></table>
EOHTML
@tables = @doc.xpath("table")
@tables.each do |table|
ret = ""
ret += "{|\n"
table.xpath("*").each do |e|
case e.name
when "caption"
ret += "|+ " + e.content + "\n"
when "tr"
ret += "|-\n"
first = true
e.xpath("*").each do |e2|
case e2.name
when "th"
ret += "\n" unless first
ret += "! " + e2.content + "\n"
first = true
when "td"
ret += "|" if first
style = e2.attributes['style'] ? " style=\"#{e2.attributes['style']}\" " : ""
ret += "#{style}| " + e2.content + " "
first = false
end
end
ret += "\n"
end
end
ret += "|}\n"
puts ret
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment