Sha256: b7fbd15cdc4706e7df3e9447d104c4837bc5461ec4608f0030865906c3d3ad40

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

# encoding: utf-8

module Infoboxer
  module Tree
    # Represents plain text node.
    #
    # Think of it like this: if you have paragraph
    # ```
    # Some paragraph with ''italic'' and [wikilink].
    # ```
    # ...then it will be parsed as a sequence of `[Text`, {Italic}, `Text`,
    # {Wikilink}, `Text]`.
    #
    class Text < Node
      # Text fragment without decodint of HTML entities.
      attr_accessor :raw_text

      def initialize(text, **params)
        super(params)
        @raw_text = text
      end

      # See {Node#text}
      def text
        @text ||= decode(@raw_text)
      end

      # See {Node#to_tree}
      def to_tree(level = 0)
        "#{indent(level)}#{text} <#{descr}>\n"
      end

      # @private
      # Internal, used by {Parser}
      def can_merge?(other)
        other.is_a?(String) || other.is_a?(Text)
      end

      # @private
      # Internal, used by {Parser}
      def merge!(other)
        if other.is_a?(String)
          @raw_text << other
        elsif other.is_a?(Text)
          @raw_text << other.raw_text
        else
          fail("Not mergeable into text: #{other.inspect}")
        end
      end

      # @private
      # Internal, used by {Parser}
      def empty?
        raw_text.empty?
      end

      private

      def _eq(other)
        text == other.text
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
infoboxer-0.3.1.pre lib/infoboxer/tree/text.rb
infoboxer-0.3.0.pre lib/infoboxer/tree/text.rb