Sha256: 1680a0b01cb4c2e2f86638b143d59bbb1f0e442c14cfd2467aca261dea0b5f2e

Contents?: true

Size: 795 Bytes

Versions: 1

Compression:

Stored size: 795 Bytes

Contents

# -*- coding: utf-8 -*-
module Agate
  class Parser
    # Default options
    @@defaults = {
      :delimiters => "【】"
    }

    # Regexp reserved characters to escape when matching
    @@reserved = ["(", ")", "[", "]", "{", "}", ".", ",", "+", "*"]

    def initialize(options = {})
      @options = @@defaults.merge(options)
    end

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

      first = /#{'\\' + first}/ if @@reserved.include? first
      last  = /#{'\\' + last}/  if @@reserved.include? last

      text.gsub(/(\p{Han}+)(#{first})([\p{Hiragana}\p{Katakana}]+)(#{last})/,
        '<ruby>\1<rp>\2</rp><rt>\3</rt><rp>\4</rp></ruby>')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
agate-0.2.0 lib/agate/parser.rb