Sha256: 25cf215537c35dca650ed1d3daa918d0249e5666a5b1ff0ba74fa3ed8f9ac857

Contents?: true

Size: 1.25 KB

Versions: 48

Compression:

Stored size: 1.25 KB

Contents

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

module Rouge
  class TextAnalyzer < String
    # Find a shebang.  Returns nil if no shebang is present.
    def shebang
      return @shebang if instance_variable_defined? :@shebang

      self =~ /\A\s*#!(.*)$/
      @shebang = $1
    end

    # Check if the given shebang is present.
    #
    # This normalizes things so that `text.shebang?('bash')` will detect
    # `#!/bash`, '#!/bin/bash', '#!/usr/bin/env bash', and '#!/bin/bash -x'
    def shebang?(match)
      return false unless shebang
      match = /\b#{match}(\s|$)/
      match === shebang
    end

    # Return the contents of the doctype tag if present, nil otherwise.
    def doctype
      return @doctype if instance_variable_defined? :@doctype

      self =~ %r(\A\s*
        (?:<\?.*?\?>\s*)? # possible <?xml...?> tag
        <!DOCTYPE\s+(.+?)>
      )xm
      @doctype = $1
    end

    # Check if the doctype matches a given regexp or string
    def doctype?(type=//)
      type === doctype
    end

    # Return true if the result of lexing with the given lexer contains no
    # error tokens.
    def lexes_cleanly?(lexer)
      lexer.lex(self) do |(tok, _)|
        return false if tok.name == 'Error'
      end

      true
    end
  end
end

Version data entries

48 entries across 48 versions & 3 rubygems

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