Module: LicenseFinder::License::Text

Defined in:
lib/license_finder/license/text.rb

Constant Summary collapse

SPACES =
/\s+/.freeze
QUOTES =
/['`"]{1,2}/.freeze
YEAR_PLACEHOLDERS =
/<year>/.freeze
PLACEHOLDERS =
/<[^<>]+>/.freeze
SPECIAL_SINGLE_QUOTES =
/[‘’]/.freeze
SPECIAL_DOUBLE_QUOTES =
/[“”„«»]/.freeze
ALPHABET_ORDERED_LIST =
/\\\([a-z]\\\)\\\s/.freeze
ALPHABET_ORDERED_LIST_OPTIONAL =
'(\([a-z]\)\s)?'
LIST_BULLETS =
/(\d{1,2}\\\.|\\\*|\\\-)\\\s/.freeze
LIST_BULLETS_OPTIONAL =
'(\d{1,2}.|\*|\-)?\s*'
NEWLINE_CHARACTER =
/\n+/.freeze
QUOTE_COMMENT_CHARACTER =
/^\s*\>+/.freeze
ESCAPED_QUOTES =
/\\\"/.freeze

Class Method Summary collapse

Class Method Details

.compile_to_regex(text) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/license_finder/license/text.rb', line 34

def self.compile_to_regex(text)
  Regexp.new(Regexp.escape(normalize_punctuation(text))
                 .gsub(YEAR_PLACEHOLDERS, '(\S*)')
                 .gsub(PLACEHOLDERS, '(.*)')
                 .gsub(',', '(,)?')
                 .gsub('HOLDER', '(HOLDER|OWNER)')
                 .gsub(ALPHABET_ORDERED_LIST, ALPHABET_ORDERED_LIST_OPTIONAL)
                 .gsub(LIST_BULLETS, LIST_BULLETS_OPTIONAL))
end

.normalize_punctuation(text) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/license_finder/license/text.rb', line 20

def self.normalize_punctuation(text)
  text.dup.force_encoding('UTF-8')
      .gsub(SPECIAL_DOUBLE_QUOTES, '"')
      .gsub(SPECIAL_SINGLE_QUOTES, "'")
      .gsub(QUOTE_COMMENT_CHARACTER, '')
      .gsub(SPACES, ' ')
      .gsub(NEWLINE_CHARACTER, ' ')
      .gsub(ESCAPED_QUOTES, '"')
      .gsub(QUOTES, '"')
      .strip
rescue ArgumentError => _e
  text
end