Skip to content

Instantly share code, notes, and snippets.

@5thWall
Created August 17, 2011 06:58
Show Gist options
  • Save 5thWall/1150990 to your computer and use it in GitHub Desktop.
Save 5thWall/1150990 to your computer and use it in GitHub Desktop.
A little script for replacing classed spans with html tags for converting GoogleDocs documents to MOBI format.
require 'nokogiri'
@doc = Nokogiri::HTML File.open($*[0])
if $*[1] != nil
iclass = $*[1]
ital = @doc.css "span.#{iclass}"
ital.each do |n|
n['class'] = n['class'].gsub(/#{iclass}/, "").strip
i = Nokogiri::XML::Node.new "i", @doc
n.children.each { |c| c.parent = i }
n.add_child i
end
end
if $*[2] != nil
bclass = $*[2]
bold = @doc.css "span.#{bclass}"
bold.each do |n|
n['class'] = n['class'].gsub(/#{bclass}/, "").strip
b = Nokogiri::XML::Node.new "b", @doc
n.children.each { |c| c.parent = b }
n.add_child b
end
end
if $*[3] != nil
biclass = $*[3]
bi = @doc.css "span.#{biclass}"
bi.each do |n|
n['class'] = n['class'].gsub(/#{biclass}/, "").strip
b = Nokogiri::XML::Node.new "b", @doc
i = Nokogiri::XML::Node.new "i", @doc
n.children.each { |c| c.parent = b }
i.add_child b
n.add_child i
end
end
if $*[4] != nil
uclass = $*[4]
under = @doc.css "span.#{uclass}"
under.each do |n|
n['class'] = n['class'].gsub(/#{uclass}/, "").strip
u = Nokogiri::XML::Node.new "u", @doc
n.children.each { |c| c.parent = u}
n.add_child u
end
end
if $*[5] != nil
iuclass = $*[5]
iu = @doc.css "span.#{iuclass}"
iu.each do |n|
n['class'] = n['class'].gsub(/#{iuclass}/, "").strip
i = Nokogiri::XML::Node.new "i", @doc
u = Nokogiri::XML::Node.new "u", @doc
n.children.each { |c| c.parent = u }
i.add_child u
n.add_child i
end
end
if $*[6] != nil
buclass = $*[6]
bu = @doc.css "span.#{buclass}"
bu.each do |n|
n['class'] = n['class'].gsub(/#{buclass}/, "").strip
b = Nokogiri::XML::Node.new "b", @doc
u = Nokogiri::XML::Node.new "u", @doc
n.children.each { |c| c.parent = u }
b.add_child u
n.add_child b
end
end
File.open($*[0], 'w') do |f|
f.puts @doc.to_xhtml
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment