Sha256: 53c7538e1e5ea9fa0320cc86a7bc41e1331bb07e39a1536b76c8437a256cb850

Contents?: true

Size: 1.49 KB

Versions: 41

Compression:

Stored size: 1.49 KB

Contents

# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
  module Lexers
    class Escape < Lexer
      tag 'escape'
      aliases 'esc'

      desc 'A generic lexer for including escaped content - see Formatter.enable_escape!'

      option :start, 'the beginning of the escaped section, default "<!"'
      option :end, 'the end of the escaped section, e.g. "!>"'
      option :lang, 'the language to lex in unescaped sections'

      attr_reader :start
      attr_reader :end
      attr_reader :lang

      def initialize(*)
        super
        @start = string_option(:start) { '<!' }
        @end = string_option(:end) { '!>' }
        @lang = lexer_option(:lang) { PlainText.new }
      end

      def to_start_regex
        @to_start_regex ||= /(.*?)(#{Regexp.escape(@start)})/m
      end

      def to_end_regex
        @to_end_regex ||= /(.*?)(#{Regexp.escape(@end)})/m
      end

      def stream_tokens(str, &b)
        stream = StringScanner.new(str)

        loop do
          if stream.scan(to_start_regex)
            puts "pre-escape: #{stream[1].inspect}" if @debug
            @lang.continue_lex(stream[1], &b)
          else
            # no more start delimiters, scan til the end
            @lang.continue_lex(stream.rest, &b)
            return
          end

          if stream.scan(to_end_regex)
            yield Token::Tokens::Escape, stream[1]
          else
            yield Token::Tokens::Escape, stream.rest
            return
          end
        end
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
rouge-3.26.0 lib/rouge/lexers/escape.rb
rouge-3.25.0 lib/rouge/lexers/escape.rb
rouge-3.24.0 lib/rouge/lexers/escape.rb
rouge-3.23.0 lib/rouge/lexers/escape.rb
rouge-3.22.0 lib/rouge/lexers/escape.rb
rouge-3.21.0 lib/rouge/lexers/escape.rb
rouge-3.20.0 lib/rouge/lexers/escape.rb
rouge-3.19.0 lib/rouge/lexers/escape.rb
rouge-3.18.0 lib/rouge/lexers/escape.rb
rouge-3.17.0 lib/rouge/lexers/escape.rb
rouge-3.16.0 lib/rouge/lexers/escape.rb
rouge-3.15.0 lib/rouge/lexers/escape.rb
rouge-3.14.0 lib/rouge/lexers/escape.rb
rouge-3.13.0 lib/rouge/lexers/escape.rb
rouge-3.12.0 lib/rouge/lexers/escape.rb
rouge-3.11.1 lib/rouge/lexers/escape.rb
rouge-3.11.0 lib/rouge/lexers/escape.rb
rouge-3.10.0 lib/rouge/lexers/escape.rb
rouge-3.9.0 lib/rouge/lexers/escape.rb
rouge-3.8.0 lib/rouge/lexers/escape.rb