Sha256: 2a9b45a786a7ab4a7821268c9ed93d4859f9e58638b41683f170b8345b4c00b2

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

# encoding: UTF-8

module TmxParser

  class Listener
    include TagNames

    attr_reader :units, :proc

    def initialize(&block)
      @stack = []
      @proc = block
    end

    def unit(tuid, segtype)
      @current_unit = Unit.new(tuid, segtype)
    end

    def variant(locale)
      variant = Variant.new(locale)
      current_unit.variants << variant
      stack.push(variant)
    end

    def property(name)
      val = PropertyValue.new
      current_unit.properties[name] = val
      stack.push(val)
    end

    def text(str)
      if last = stack.last
        last.receive_text(str)
      end
    end

    def done(tag_name)
      if tag_name == UNIT_TAG
        proc.call(current_unit)
      else
        if tag_name_for(stack.last) == tag_name
          stack.pop
        end
      end
    end

    def placeholder(type)
      placeholder = Placeholder.new(type)
      current_unit.variants.last.elements << placeholder
      stack.push(placeholder)
    end

    def begin_paired_tag(i)
      begin_pair = BeginPair.new(i)
      current_unit.variants.last.elements << begin_pair
      stack.push(begin_pair)
    end

    def end_paired_tag(i)
      end_pair = EndPair.new(i)
      current_unit.variants.last.elements << end_pair
      stack.push(end_pair)
    end

    private

    def tag_name_for(obj)
      case obj
        when Variant       then VARIANT_TAG
        when PropertyValue then PROPERTY_TAG
        when Placeholder   then PLACEHOLDER_TAG
        when BeginPair     then BEGIN_PAIRED_TAG
        when EndPair       then END_PAIRED_TAG
      end
    end

    attr_reader :current_unit, :stack

  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
tmx-parser-2018-1.2 lib/tmx-parser/listener.rb
tmx-parser-2018-1.1.1 lib/tmx-parser/listener.rb
tmx-parser-2018-1.1.0 lib/tmx-parser/listener.rb
tmx-parser-1.1.0 lib/tmx-parser/listener.rb
tmx-parser-1.0.1 lib/tmx-parser/listener.rb
tmx-parser-1.0.0 lib/tmx-parser/listener.rb