Sha256: 7a07d105ad36c497dcbcf3274600005b574e4fcf41bb41deeab6ad191f46d772

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module Licensee
  module ProjectFiles
    class ReadmeFile < Licensee::ProjectFiles::LicenseFile
      EXTENSIONS = %w[md markdown mdown txt rdoc rst].freeze
      SCORES = {
        /\AREADME\z/i                                       => 1.0,
        /\AREADME\.(#{Regexp.union(EXTENSIONS).source})\z/i => 0.9
      }.freeze

      TITLE_REGEX = /licen[sc]e:?/i.freeze
      UNDERLINE_REGEX = /\n[-=]+/m.freeze
      CONTENT_REGEX = /^
          (?:                                # Header lookbehind
            [\#=]+\s#{TITLE_REGEX}\s*[\#=]*  # Start of hashes or rdoc header
          |
            #{TITLE_REGEX}#{UNDERLINE_REGEX} # Start of underlined header
          )$
          (.*?)                              # License content
          (?=^                               # Header or end of file lookahead
            (?:
              [\#=]+                         # Next hash or rdoc header
            |
              [^\n]+#{UNDERLINE_REGEX}       # Next of underlined header
            )
          |
            \z                               # End of file
          )
        /mix.freeze

      def possible_matchers
        super.push(Matchers::Reference)
      end

      def self.name_score(filename)
        SCORES.each do |pattern, score|
          return score if pattern =~ filename
        end
        0.0
      end

      def self.license_content(content)
        match = CONTENT_REGEX.match(content)
        match[1].strip if match
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
licensee-9.11.0 lib/licensee/project_files/readme_file.rb