From 8087d3403543e792c959a12044f6542efc90d2da Mon Sep 17 00:00:00 2001 From: Ghabry Date: Fri, 1 Jul 2022 00:04:31 +0200 Subject: [PATCH 1/2] Add Github datasource for Player and liblcf --- Rules | 2 ++ lib/github_liblcf_datasource.rb | 11 +++++++++++ lib/github_player_datasource.rb | 11 +++++++++++ make_cache.rb | 34 +++++++++++++++++++++++++++++++++ nanoc.yaml | 6 ++++++ 5 files changed, 64 insertions(+) create mode 100644 lib/github_liblcf_datasource.rb create mode 100644 lib/github_player_datasource.rb create mode 100755 make_cache.rb diff --git a/Rules b/Rules index 590a7e7..a3d0fcd 100644 --- a/Rules +++ b/Rules @@ -1,5 +1,7 @@ #!/usr/bin/env ruby +ignore '/github/**/*' + preprocess do hide_items_from_sitemap end diff --git a/lib/github_liblcf_datasource.rb b/lib/github_liblcf_datasource.rb new file mode 100644 index 0000000..60c63f3 --- /dev/null +++ b/lib/github_liblcf_datasource.rb @@ -0,0 +1,11 @@ +require 'json' + +class GithubLibLcfDataSource < Nanoc::DataSource + identifier :github_liblcf + + def items + JSON.parse(open(".cache/liblcf_issues.json").read()).map do |x| + new_item(x["title"], x, "/liblcf/issues/#{x["number"]}") + end + end +end diff --git a/lib/github_player_datasource.rb b/lib/github_player_datasource.rb new file mode 100644 index 0000000..18bffb8 --- /dev/null +++ b/lib/github_player_datasource.rb @@ -0,0 +1,11 @@ +require 'json' + +class GithubPlayerDataSource < Nanoc::DataSource + identifier :github_player + + def items + JSON.parse(open(".cache/player_issues.json").read()).map do |x| + new_item(x["title"], x, "/player/issues/#{x["number"]}") + end + end +end diff --git a/make_cache.rb b/make_cache.rb new file mode 100755 index 0000000..b3e665d --- /dev/null +++ b/make_cache.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +require "json" +require "open-uri" + +def fetch(repo) + i = 1 + res = [] + + loop do + j = JSON.parse(URI.open("https://api.github.com/repos/easyrpg/#{repo}/issues?per_page=100&page=#{i}&filter=all&state=all&sort=created").read()) + return res if j.length == 0 + j.each { |item| + res.push({ + title: item["title"], + number: item["number"], + is_pr: item.key?("pull_request") + }) + puts item["title"] + } + i += 1 + end +end + +liblcf = fetch("liblcf") +player = fetch("player") + +File.open(".cache/liblcf_issues.json", "w") do |f| + f.write(JSON.pretty_generate(liblcf)) +end + +File.open(".cache/player_issues.json", "w") do |f| + f.write(JSON.pretty_generate(player)) +end diff --git a/nanoc.yaml b/nanoc.yaml index b289432..99882d4 100644 --- a/nanoc.yaml +++ b/nanoc.yaml @@ -51,6 +51,12 @@ data_sources: # UTF-8 (which they should be!), change this. encoding: utf-8 + - type: github_player + items_root: /github + + - type: github_liblcf + items_root: /github + # Configuration for the “check” command, which run unit tests on the site. checks: # Configuration for the “internal_links” checker, which checks whether all From bd7c134ca42badef8f53857124a62f6a88afe9d2 Mon Sep 17 00:00:00 2001 From: Ghabry Date: Fri, 1 Jul 2022 00:37:32 +0200 Subject: [PATCH 2/2] Add Github issue filter using the datasource Replaces player#1234 and liblcf#123 with appropriate links --- Rules | 1 + lib/github_issues_filter.rb | 27 +++++++++++++++++++++++++++ make_cache.rb | 1 - 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 lib/github_issues_filter.rb diff --git a/Rules b/Rules index a3d0fcd..512c513 100644 --- a/Rules +++ b/Rules @@ -8,6 +8,7 @@ end compile '/**/*.{html,md}' do filter :erb + filter :github_issues filter :kramdown, auto_ids: false if item.identifier =~ '/**/*.md' filter :colorize_syntax, :default_colorizer => :rouge layout '/default.*' diff --git a/lib/github_issues_filter.rb b/lib/github_issues_filter.rb new file mode 100644 index 0000000..822605c --- /dev/null +++ b/lib/github_issues_filter.rb @@ -0,0 +1,27 @@ +class GitHubIssuesFilter < Nanoc::Filter + identifier :github_issues + type :text + + def run(content, params={}) + liblcf_issues = @items.find_all('/github/liblcf/issues/*') + player_issues = @items.find_all('/github/player/issues/*') + + content.scan(/player#\d+/).each { |x| + n = (x.match /\d+/)[0].to_i + issue = player_issues[n] + type = issue[:is_pr] ? "pulls" : "issues" + url = "https://github.com/EasyRPG/Player/#{type}/#{n}" + content = content.gsub(x, "[\##{n}](#{url} \"Player: #{issue[:title]}\")") + } + + content.scan(/liblcf#\d+/).each { |x| + n = (x.match /\d+/)[0].to_i + issue = liblcf_issues[n] + type = issue[:is_pr] ? "pulls" : "issues" + url = "https://github.com/EasyRPG/liblcf/#{type}/#{n}" + content = content.gsub(x, "[\##{n}](#{url} \"liblcf: #{issue[:title]}\")") + } + + content + end +end diff --git a/make_cache.rb b/make_cache.rb index b3e665d..e9ccc42 100755 --- a/make_cache.rb +++ b/make_cache.rb @@ -16,7 +16,6 @@ def fetch(repo) number: item["number"], is_pr: item.key?("pull_request") }) - puts item["title"] } i += 1 end