Skip to content

Instantly share code, notes, and snippets.

@matallo
Created February 25, 2020 10:30
Show Gist options
  • Save matallo/f1a5e6f135c4f6df55b89f0fb1f37b1b to your computer and use it in GitHub Desktop.
Save matallo/f1a5e6f135c4f6df55b89f0fb1f37b1b to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'cgi'
filename = 'export.xml'
xml = File.read(filename)
doc = Nokogiri::XML(xml)
doc.xpath("//Content").each { |node, i|
if node.content != ''
node_str = node.content
unescaped_node_str = CGI.unescapeHTML(node_str)
sq_to_tags_node_str = unescaped_node_str.gsub(/\[(.*?)\]/, '<\1>')
node_mod = Nokogiri::XML::Node.new "ContentMod", doc
node_mod.add_child(sq_to_tags_node_str)
node.add_next_sibling(node_mod)
node_mod.xpath("//content").each { |node_node, i|
node_node_str = node_node.content
unescaped_node_node_str = CGI.unescape(node_node_str)
node_node.text = CGI.unescape(unescaped_node_node_str)
}
end
}
File.write(filename, doc.to_xml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment