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-4.5.1 lib/rouge/lexers/escape.rb
rouge-4.5.0 lib/rouge/lexers/escape.rb
rouge-4.4.0 lib/rouge/lexers/escape.rb
rouge-4.3.0 lib/rouge/lexers/escape.rb
rouge-4.2.1 lib/rouge/lexers/escape.rb
rouge-4.2.0 lib/rouge/lexers/escape.rb
rouge-4.1.3 lib/rouge/lexers/escape.rb
rouge-4.1.2 lib/rouge/lexers/escape.rb
rouge-4.1.1 lib/rouge/lexers/escape.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/rouge-3.30.0/lib/rouge/lexers/escape.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/rouge-3.30.0/lib/rouge/lexers/escape.rb
rouge-4.1.0 lib/rouge/lexers/escape.rb
rouge-4.0.1 lib/rouge/lexers/escape.rb
rouge-4.0.0 lib/rouge/lexers/escape.rb
rouge-3.30.0 lib/rouge/lexers/escape.rb
rouge-3.29.0 lib/rouge/lexers/escape.rb
rouge-3.28.0 lib/rouge/lexers/escape.rb
rouge-3.27.0 lib/rouge/lexers/escape.rb
rouge-3.26.1 lib/rouge/lexers/escape.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/rouge-3.26.0/lib/rouge/lexers/escape.rb