Sha256: 66b3b77fef190cc7db2e62a9a5dee194748977fb868118f8ed620a72cd5b9e95

Contents?: true

Size: 1.55 KB

Versions: 10

Compression:

Stored size: 1.55 KB

Contents

# -*- coding: utf-8 -*- #

module Rouge
  # A Formatter takes a token stream and formats it for human viewing.
  class Formatter
    # @private
    REGISTRY = {}

    # Specify or get the unique tag for this formatter.  This is used
    # for specifying a formatter in `rougify`.
    def self.tag(tag=nil)
      return @tag unless tag
      REGISTRY[tag] = self

      @tag = tag
    end

    # Find a formatter class given a unique tag.
    def self.find(tag)
      REGISTRY[tag]
    end

    # Format a token stream.  Delegates to {#format}.
    def self.format(tokens, *a, &b)
      new(*a).format(tokens, &b)
    end

    def initialize(opts={})
      # pass
    end

    # Format a token stream.
    def format(tokens, &b)
      return stream(tokens, &b) if block_given?

      out = ''
      stream(tokens) { |piece| out << piece }

      out
    end

    # @deprecated Use {#format} instead.
    def render(tokens)
      warn 'Formatter#render is deprecated, use #format instead.'
      format(tokens)
    end

    # @abstract
    # yield strings that, when concatenated, form the formatted output
    def stream(tokens, &b)
      raise 'abstract'
    end

  protected
    def token_lines(tokens, &b)
      return enum_for(:token_lines, tokens) unless block_given?

      out = []
      tokens.each do |tok, val|
        val.scan /\n|[^\n]+/ do |s|
          if s == "\n"
            yield out
            out = []
          else
            out << [tok, s]
          end
        end
      end

      # for inputs not ending in a newline
      yield out if out.any?
    end

  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/rouge-2.2.1/lib/rouge/formatter.rb
rouge-3.2.1 lib/rouge/formatter.rb
rouge-3.2.0 lib/rouge/formatter.rb
rouge_ecl-1.0.0 lib/rouge/formatter.rb
rouge_ecl-0.0.1 lib/rouge/formatter.rb
rouge-3.1.1 lib/rouge/formatter.rb
rouge-3.1.0 lib/rouge/formatter.rb
rouge-3.0.0 lib/rouge/formatter.rb
rouge-2.2.1 lib/rouge/formatter.rb
rouge-2.2.0 lib/rouge/formatter.rb