File: formatter.rb

package info (click to toggle)
ruby-rouge 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: ruby: 38,489; sed: 2,071; perl: 152; makefile: 8
file content (112 lines) | stat: -rw-r--r-- 2,491 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

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

    def self.with_escape
      Thread.current[:'rouge/with-escape'] = true
      yield
    ensure
      Thread.current[:'rouge/with-escape'] = false
    end

    def self.escape_enabled?
      !!(((defined? @escape_enabled) && @escape_enabled) || Thread.current[:'rouge/with-escape'])
    end

    def self.enable_escape!
      @escape_enabled = true
    end

    def self.disable_escape!
      @escape_enabled = false
      Thread.current[:'rouge/with-escape'] = false
    end

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

    def initialize(opts={})
      # pass
    end

    def escape?(tok)
      tok == Token::Tokens::Escape
    end

    def filter_escapes(tokens)
      tokens.each do |t, v|
        if t == Token::Tokens::Escape
          yield Token::Tokens::Error, v
        else
          yield t, v
        end
      end
    end

    # Format a token stream.
    def format(tokens, &b)
      tokens = enum_for(:filter_escapes, tokens) unless Formatter.escape_enabled?

      return stream(tokens, &b) if block_given?

      out = String.new('')
      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 %r/\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