class Rouge::Formatter
A Formatter
takes a token stream and formats it for human viewing.
Constants
- REGISTRY
-
@private
Public Class Methods
Source
# File lib/rouge/formatter.rb, line 38 def self.disable_escape! @escape_enabled = false Thread.current[:'rouge/with-escape'] = false end
Source
# File lib/rouge/formatter.rb, line 34 def self.enable_escape! @escape_enabled = true end
Source
# File lib/rouge/formatter.rb, line 30 def self.escape_enabled? !!(((defined? @escape_enabled) && @escape_enabled) || Thread.current[:'rouge/with-escape']) end
Source
# File lib/rouge/formatter.rb, line 19 def self.find(tag) REGISTRY[tag] end
Find a formatter class given a unique tag.
Source
# File lib/rouge/formatter.rb, line 44 def self.format(tokens, *args, **kwargs, &b) new(*args, **kwargs).format(tokens, &b) end
Format a token stream. Delegates to {#format}.
Source
# File lib/rouge/formatter.rb, line 11 def self.tag(tag=nil) return @tag unless tag REGISTRY[tag] = self @tag = tag end
Specify or get the unique tag for this formatter. This is used for specifying a formatter in ‘rougify`.
Source
# File lib/rouge/formatter.rb, line 23 def self.with_escape Thread.current[:'rouge/with-escape'] = true yield ensure Thread.current[:'rouge/with-escape'] = false end
Public Instance Methods
Source
# File lib/rouge/formatter.rb, line 52 def escape?(tok) tok == Token::Tokens::Escape end
Source
# File lib/rouge/formatter.rb, line 56 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
Source
# File lib/rouge/formatter.rb, line 67 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
Format a token stream.
Source
# File lib/rouge/formatter.rb, line 79 def render(tokens) warn 'Formatter#render is deprecated, use #format instead.' format(tokens) end
@deprecated Use {#format} instead.
Source
# File lib/rouge/formatter.rb, line 86 def stream(tokens, &b) raise 'abstract' end
@abstract yield strings that, when concatenated, form the formatted output
Protected Instance Methods
Source
# File lib/rouge/formatter.rb, line 91 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