Sha256: 6f9b9d6a34246adf6bb5c8c5f4390860c2805461dae26a14ec546bebfe19cbc4

Contents?: true

Size: 854 Bytes

Versions: 2

Compression:

Stored size: 854 Bytes

Contents

# -*- coding: utf-8 -*-
require "agate/formatter/html"
require "agate/formatter/plain"

module Agate
  class Parser
    # Default options
    DEFAULTS = {
      :delimiters => "【】",
      :formatter  => :plain
    }

    def initialize(options = {})
      @options = DEFAULTS.merge(options)

      @formatter = case @options[:formatter]
        when :html
          Agate::Formatter::HTML
        when :plain
          Agate::Formatter::Plain
        else
          Agate::Formatter::Plain
        end
    end

    # Parse `text` and return it with ruby character markup
    def parse(text)
      first = Regexp.escape(@options[:delimiters][0])
      last  = Regexp.escape(@options[:delimiters][-1])

      expr = /(\p{Han}+)(#{first})([\p{Hiragana}\p{Katakana}]+)(#{last})/u
      text.gsub(expr) { |match| @formatter.format($~) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
agate-0.5.1 lib/agate/parser.rb
agate-0.5.0 lib/agate/parser.rb