diff --git a/lib/rouge/lexer.rb b/lib/rouge/lexer.rb index 4261510589..d11950202b 100644 --- a/lib/rouge/lexer.rb +++ b/lib/rouge/lexer.rb @@ -259,7 +259,7 @@ def option(k, v=:absent) # @example # debug { "hello, world!" } def debug(&b) - puts(b.call) if option :debug + puts(b.call) if defined?(@debug) ? @debug : @debug ||= option(:debug) end # @abstract diff --git a/lib/rouge/regex_lexer.rb b/lib/rouge/regex_lexer.rb index 267533025a..5b87578a06 100644 --- a/lib/rouge/regex_lexer.rb +++ b/lib/rouge/regex_lexer.rb @@ -274,7 +274,7 @@ def scan(scanner, re, &b) # StringScanner's implementation of ^ is b0rken. # TODO: this doesn't cover cases like /(a|^b)/, but it's # the most common, for now... - return false if re.source[0] == ?^ && !scanner.beginning_of_line? + return false if re.beginning_of_line? && !scanner.beginning_of_line? @null_steps ||= 0 @@ -398,3 +398,17 @@ def with_output_stream(output_stream, &b) end end end + +class Regexp + # Does this expression start with a ^? + # + # Since Regexps are immuntable, this method is cached to avoid + # calling Regexp#source more than once. + def beginning_of_line? + if defined? @beginning_of_line + @beginning_of_line + else + @beginning_of_line = source[0] == ?^ + end + end +end