File: xml.rb

package info (click to toggle)
ruby-github-linguist 7.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,204 kB
  • sloc: ruby: 1,872; lex: 173; ansic: 35; makefile: 9
file content (30 lines) | stat: -rw-r--r-- 984 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Linguist
  module Strategy
    # Detects XML files based on root tag.
    class XML
      # Scope of the search for the root tag
      # Number of lines to check at the beginning of the file
      SEARCH_SCOPE = 2

      # Public: Use the root tag to detect the XML blobs, only if no other
      # candidates were previously identified.
      #
      # blob               - An object that quacks like a blob.
      # candidates         - A list of candidate languages.
      #
      # Examples
      #
      #   XML.call(FileBlob.new("path/to/file"))
      #
      # Returns the list of candidates if it wasn't empty, an array with the
      # XML language as sole item if the root tag is detected, and an empty
      # Array otherwise.
      def self.call(blob, candidates = [])
        return candidates if candidates.any?

        header = blob.first_lines(SEARCH_SCOPE).join("\n")
        /<?xml version=/.match(header) ? [Language["XML"]] : []
      end
    end
  end
end