lib/rouge/formatter.rb in rouge-3.3.0 vs lib/rouge/formatter.rb in rouge-3.4.0

- old
+ new

@@ -19,20 +19,56 @@ # 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? + !!(@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, *a, &b) new(*a).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 }