Skip to content

Instantly share code, notes, and snippets.

@kmuto
Created September 6, 2017 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmuto/ccdb4df81f5b90bb9624911d676f01f8 to your computer and use it in GitHub Desktop.
Save kmuto/ccdb4df81f5b90bb9624911d676f01f8 to your computer and use it in GitHub Desktop.
#!/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