Created
September 6, 2017 03:48
-
-
Save kmuto/ccdb4df81f5b90bb9624911d676f01f8 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
# coding: utf-8 | |
require 'rexml/document' | |
include REXML | |
doc = Document.new(ARGF) | |
doc.context[:attribute_quote] = :quote | |
# 親spanを追加するには%w()内に空白区切りで指定 | |
%w(bold).each do |style| | |
doc.each_element("//span[@class='#{style}']") do |e| | |
e.each do |ec| | |
if ec.class == Text | |
ec.parent.insert_after(ec, Document.new("<span class=\"#{style}\">#{ec}</span>", {:attribute_quote => :quote})) | |
ec.remove | |
elsif ec.class == Element && ec.name == 'span' && ec.attributes['class'] | |
ec.attributes['class'] = "#{style} #{ec.attributes['class']}" | |
end | |
end | |
e.name = 'removeme' | |
end | |
end | |
puts doc.to_s.gsub(/<[\/]?removeme.*?>/, '') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment