Sha256: e8813608aef76082efa6d02e0f81d7526294a284e17dc1ec700a1519bff7fd56

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

require "amakanize/filters/base_filter"

module Amakanize
  module Filters
    class BracketsNormalizationFilter < ::Amakanize::Filters::BaseFilter
      PAIRS = %w|
        ‾ ‾
        - -
        ― ―
        〜 〜
        ‹ ›
        « »
        ( )
        [ ]
        { }
        { }
        〈 〉
        《 》
        【 】
        〔 〕
        〘 〙
        〚 〛
        \[ \]
        < >
        < >
        ~ ~
      |.each_slice(2)

      # @note Override
      # @param output [String] e.g. `"IS〈インフィニット・ストラトス〉 1 (オーバーラップ文庫)"`
      # @return [Hash] e.g. `"IS (インフィニット・ストラトス) 1 (オーバーラップ文庫)"`
      def call(context:, output:)
        {
          context: context,
          output: PAIRS.each_with_object(output) do |(open, close), result|
            result.gsub!(/#{open}([^\( ]+?)#{close}/, '(\1)')
          end.gsub(/\s*\((.+?)\)(?:\z|(\s*))/) do
            " \(#{::Regexp.last_match(1)})#{' ' if Regexp.last_match(2)}"
          end
        }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
amakanize-0.7.0 lib/amakanize/filters/brackets_normalization_filter.rb
amakanize-0.6.5 lib/amakanize/filters/brackets_normalization_filter.rb
amakanize-0.6.4 lib/amakanize/filters/brackets_normalization_filter.rb
amakanize-0.6.3 lib/amakanize/filters/brackets_normalization_filter.rb
amakanize-0.6.2 lib/amakanize/filters/brackets_normalization_filter.rb