1
0
mirror of https://github.com/github/choosealicense.com synced 2024-12-23 05:10:11 +01:00
choosealicense.com/spec/spec_helper.rb

124 lines
2.6 KiB
Ruby
Raw Normal View History

2015-03-07 12:47:24 -05:00
require 'jekyll'
2015-03-07 13:38:52 -05:00
require 'open-uri'
require 'json'
2016-01-15 17:40:32 -05:00
require 'open-uri'
require 'nokogiri'
2015-03-07 12:47:24 -05:00
def config_file
File.expand_path "./_config.yml", source
end
def source
File.expand_path("../", File.dirname(__FILE__))
end
def config
config = Jekyll::Configuration.new.read_config_file config_file
config = Jekyll::Utils.deep_merge_hashes(config, {:source => source})
Jekyll::Utils.deep_merge_hashes(Jekyll::Configuration::DEFAULTS, config)
end
def licenses
2016-01-18 17:05:02 -05:00
site.collections["licenses"].docs.map do |license|
id = File.basename(license.basename, ".txt")
license.to_liquid.merge("id" => id)
end
end
def license_ids
licenses.map { |l| l["id"] }
2015-08-22 15:25:16 -04:00
end
2015-09-09 00:04:34 +01:00
def families
licenses.map { |l| l["family"] }.compact.uniq
2015-03-07 12:47:24 -05:00
end
def site
$site ||= begin
site = Jekyll::Site.new(config)
site.reset
site.read
site
end
end
def rules
site.data["rules"]
end
2015-08-22 15:25:16 -04:00
def fields
site.data["fields"]
end
def meta
site.data["meta"]
end
2015-03-07 12:47:24 -05:00
def rule?(tag, group)
rules[group].any? { |r| r["tag"] == tag }
end
2015-03-07 13:38:52 -05:00
def spdx_list
url = "https://raw.githubusercontent.com/sindresorhus/spdx-license-list/master/spdx.json"
$spdx ||= JSON.parse(open(url).read)
end
2016-01-15 17:40:32 -05:00
def spdx_ids
spdx_list.map { |name, properties| name.downcase }
end
2015-03-07 13:38:52 -05:00
def find_spdx(license)
spdx_list.find { |name, properties| name.downcase == license }
end
2016-01-15 17:40:32 -05:00
2016-01-15 18:01:07 -05:00
def osi_approved_licenses
$osi_approved_licenses ||= begin
licenses = {}
list = spdx_list.select { |id, meta| meta["osiApproved"] }
list.each do |id, meta|
licenses[id.downcase] = meta["name"]
end
licenses
end
2016-01-15 17:40:32 -05:00
end
def fsf_approved_licenses
$fsf_approved_licenses ||= begin
url = "https://www.gnu.org/licenses/license-list.en.html"
doc = Nokogiri::HTML(open(url).read)
list = doc.css(".green dt")
licenses = {}
list.each do |license|
a = license.css("a").find { |link| !link.text.nil? && !link.text.empty? && link.attr("id") }
next if a.nil?
id = a.attr("id").downcase
name = a.text.strip
licenses[id] = name
end
# FSF approved the Clear BSD, but doesn't use its SPDX ID or Name
if licenses.keys.include? "clearbsd"
licenses["bsd-3-clause-clear"] = licenses["clearbsd"]
end
2016-01-15 17:40:32 -05:00
licenses
end
end
def od_approved_licenses
$od_approved_licenses ||= begin
url = "http://licenses.opendefinition.org/licenses/groups/od.json"
data = open(url).read
data = JSON.parse(data)
licenses = {}
data.each do |id, meta|
licenses[id.downcase] = meta["title"]
end
licenses
end
end
2016-01-15 18:01:07 -05:00
def approved_licenses
(osi_approved_licenses.keys + fsf_approved_licenses.keys + od_approved_licenses.keys).flatten.uniq.sort
2016-01-15 17:40:32 -05:00
end