Created
December 2, 2011 12:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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