Make it work again

This commit is contained in:
Roberto Alsina 2024-08-24 20:09:29 -03:00
parent 58fd42d936
commit fc9f834bc8

View File

@ -1,6 +1,6 @@
require "yaml" require "yaml"
module Tartrazine module Linguist
# Use linguist's heuristics to disambiguate between languages # Use linguist's heuristics to disambiguate between languages
class Heuristic class Heuristic
@ -34,12 +34,13 @@ module Tartrazine
end end
end end
class Rule class LangRule
include YAML::Serializable include YAML::Serializable
property pattern : (String | Array(String))? property pattern : (String | Array(String))?
property negative_pattern : (String | Array(String))? property negative_pattern : (String | Array(String))?
property named_pattern : String? property named_pattern : String?
property and : Array(Rule)? property and : Array(LangRule)?
property language : String | Array(String)?
# ameba:disable Metrics/CyclomaticComplexity # ameba:disable Metrics/CyclomaticComplexity
def match(content, named_patterns) def match(content, named_patterns)
@ -68,17 +69,13 @@ module Tartrazine
result = p_arr.any? { |pat| ::Regex.new(pat).matches?(content) } result = p_arr.any? { |pat| ::Regex.new(pat).matches?(content) }
end end
if and if and
result = and.as(Array(Rule)).all?(&.match(content, named_patterns)) result = and.as(Array(LangRule)).all?(&.match(content, named_patterns))
end end
result result
end end
end end
class LangRule < Rule
include YAML::Serializable
property language : String | Array(String)
end
end end
# h = Tartrazine::Heuristic.from_yaml(File.read("heuristics/heuristics.yml")) h = Linguist::Heuristic.from_yaml(File.read("heuristics/heuristics.yml"))
# p! h.run(ARGV[0], File.read(ARGV[0])) fname = "/usr/include/sqlite3.h"
p! h.run(fname, File.read(fname))