sul-dlss/cocina-models

View on GitHub
lib/cocina/models/mapping/escape_html.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

module Cocina
  module Models
    module Mapping
      # Escaps HTML entities as CDATA for MODS since HTML is not permitted in MODS
      class EscapeHtml
        # @param [String] data
        # @param [Nokogiri::XML::Builder]
        def self.with_cdata(data, builder)
          tokens = data.split(%r{(</?(?:i|cite)>)})
          tokens.map do |token|
            if /\A<.+>\z/.match? token
              builder.cdata(token)
            else
              builder.text(token)
            end
          end
        end
      end
    end
  end
end