Skip to content

Instantly share code, notes, and snippets.

@fenomenog4
Created December 2, 2011 12:37
Show Gist options
  • Save fenomenog4/1423094 to your computer and use it in GitHub Desktop.
Save fenomenog4/1423094 to your computer and use it in GitHub Desktop.
Conversión de algunos tags problemáticos para el Editor Grammata en ficheros epubs con Loofah
require "loofah"
style2tag = Loofah::Scrubber.new do |node|
if node.attributes and node.attr('style')
if node.attr('style')['font-style:italic'] and
node.attr('style')['font-weight:bold']
new_node = node.dup
new_node.name = 'i'
new_node.remove_attribute('style')
node.children = new_node
node.name = 'b'
node.remove_attribute('style')
elsif node.attr('style')['font-style:italic']
node.name = 'i'
node.remove_attribute('style')
elsif node.attr('style')['font-weight:bold']
node.name = 'b'
node.remove_attribute('style')
end
end
end
Dir.foreach('in') do |entry|
if File.file?("in/#{entry}")
html = nil
File.open("in/#{entry}", "r") do |f|
html = f.read
end
html_sanitized = Loofah.document(html).scrub!(style2tag)
File.open("out/#{entry}", "w") do |f|
f.write(html_sanitized)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment