Sha256: 097ef6baeff463d2210a4dec2ada84ada51943e844203d1c5b6e9d4408f718b0

Contents?: true

Size: 2 KB

Versions: 25

Compression:

Stored size: 2 KB

Contents

module Rouge
  class InheritableHash < Hash
    def initialize(parent=nil)
      @parent = parent
    end

    def [](k)
      _sup = super
      return _sup if own_keys.include?(k)

      _sup || parent[k]
    end

    def parent
      @parent ||= {}
    end

    def include?(k)
      super or parent.include?(k)
    end

    def each(&b)
      keys.each do |k|
        b.call(k, self[k])
      end
    end

    alias own_keys keys
    def keys
      keys = own_keys.concat(parent.keys)
      keys.uniq!
      keys
    end
  end

  class InheritableList
    include Enumerable

    def initialize(parent=nil)
      @parent = parent
    end

    def parent
      @parent ||= []
    end

    def each(&b)
      return enum_for(:each) unless block_given?

      parent.each(&b)
      own_entries.each(&b)
    end

    def own_entries
      @own_entries ||= []
    end

    def push(o)
      own_entries << o
    end
    alias << push
  end

  # shared methods for some indentation-sensitive lexers
  module Indentation
    def reset!
      super
      @block_state = @block_indentation = nil
    end

    # push a state for the next indented block
    def starts_block(block_state)
      @block_state = block_state
      @block_indentation = @last_indentation || ''
      debug { "    starts_block #{block_state.inspect}" }
      debug { "    block_indentation: #{@block_indentation.inspect}" }
    end

    # handle a single indented line
    def indentation(indent_str)
      debug { "    indentation #{indent_str.inspect}" }
      debug { "    block_indentation: #{@block_indentation.inspect}" }
      @last_indentation = indent_str

      # if it's an indent and we know where to go next,
      # push that state.  otherwise, push content and
      # clear the block state.
      if (@block_state &&
          indent_str.start_with?(@block_indentation) &&
          indent_str != @block_indentation
      )
        push @block_state
      else
        @block_state = @block_indentation = nil
        push :content
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rouge-1.1.0 lib/rouge/util.rb
rouge-0.5.4 lib/rouge/util.rb
rouge-0.5.3 lib/rouge/util.rb
rouge-0.5.2 lib/rouge/util.rb
rouge-0.5.1 lib/rouge/util.rb
rouge-0.5.0 lib/rouge/util.rb
rouge-0.4.0 lib/rouge/util.rb
rouge-0.3.10 lib/rouge/util.rb
rouge-0.3.9 lib/rouge/util.rb
rouge-0.3.8 lib/rouge/util.rb
rouge-0.3.7 lib/rouge/util.rb
rouge-0.3.6 lib/rouge/util.rb
rouge-0.3.5 lib/rouge/util.rb
rouge-0.3.4 lib/rouge/util.rb
rouge-0.3.3 lib/rouge/util.rb
rouge-0.3.2 lib/rouge/util.rb
rouge-0.3.1 lib/rouge/util.rb
rouge-0.3.0 lib/rouge/util.rb
rouge-0.2.15 lib/rouge/util.rb
rouge-0.2.14 lib/rouge/util.rb