Sha256: 92ed9afe646bd512f1cbe2b3da078838c1b2b4b48d06d03671319bafe7e68702

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require 'English'

module Srx
  module Format
    # Support for XML
    #
    # @see https://www.w3.org/TR/xml/
    class Xml < BaseFormat
      # rubocop:disable Layout/LineLength
      NAME_START_CHAR = /[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}]/.freeze
      # rubocop:enable Layout/LineLength
      NAME_CHAR = /#{NAME_START_CHAR}|[-.0-9\u00B7\u0300-\u036F\u203F-\u2040]/.freeze
      NAME = /#{NAME_START_CHAR}#{NAME_CHAR}*/.freeze
      SPACE = /[\u0020\u0009\u000D\u000A]+/.freeze
      EQUALS = /#{SPACE}?=#{SPACE}?/.freeze
      ENTITY_REF = /&#{NAME};/.freeze
      CHAR_REF = /&#[0-9]+;|&#x[0-9a-fA-F]+;/.freeze
      REFERENCE = /#{ENTITY_REF}|#{CHAR_REF}/.freeze
      ATT_VALUE = /"(?:[^<&"]|#{REFERENCE})*"|'(?:[^<&']|#{REFERENCE})*'/.freeze
      ATTRIBUTE = /#{NAME}#{EQUALS}#{ATT_VALUE}/.freeze
      START_TAG = /<#{NAME}(?:#{SPACE}#{ATTRIBUTE})*#{SPACE}?>/.freeze
      END_TAG = %r{</#{NAME}#{SPACE}?>}.freeze
      EMPTY_ELEM_TAG = %r{<#{NAME}(?:#{SPACE}#{ATTRIBUTE})*#{SPACE}?/>}.freeze

      TAG = /#{START_TAG}|#{END_TAG}|#{EMPTY_ELEM_TAG}/.freeze

      def extract_markups(str)
        markups = []

        plain_text = str.gsub(TAG) do |match|
          markups << [$LAST_MATCH_INFO.begin(0), match]
          ''
        end

        [plain_text, markups]
      end

      def start_formatting?(markup)
        START_TAG.match?(markup)
      end

      def end_formatting?(markup)
        END_TAG.match?(markup)
      end

      def isolated_formatting?(markup)
        EMPTY_ELEM_TAG.match?(markup)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
srx-0.5.0 lib/srx/format/xml.rb
srx-0.4.0 lib/srx/format/xml.rb
srx-0.3.0 lib/srx/format/xml.rb
srx-0.2.0 lib/srx/format/xml.rb
srx-0.1.0 lib/srx/format/xml.rb