From 05ac909e8002b0934d35949d5faf71fd0ba17789 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Sat, 16 Mar 2024 21:31:06 -0300 Subject: [PATCH] Replace slugs.json with nameById.json --- Gemfile.lock | 2 +- Rules | 66 +++++--------------------- nanoc/lib/require_rules.rb | 15 ++++++ nanoc/rules/stream.rules | 11 ++--- src/html/stream.html.erb | 4 +- src/js/main/random.ts | 2 +- src/json/{slugs.json => nameById.json} | 0 src/sitemap.xml.erb | 4 +- 8 files changed, 39 insertions(+), 65 deletions(-) create mode 100644 nanoc/lib/require_rules.rb rename src/json/{slugs.json => nameById.json} (100%) diff --git a/Gemfile.lock b/Gemfile.lock index 73679ec..01b5cdb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -110,7 +110,7 @@ GEM rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.30.0, < 2.0) ruby-progressbar (1.13.0) - ryo.rb (0.4.7) + ryo.rb (0.5.1) sass (3.7.4) sass-listen (~> 4.0.0) sass-listen (4.0.0) diff --git a/Rules b/Rules index 855ad90..db14163 100644 --- a/Rules +++ b/Rules @@ -2,23 +2,19 @@ # frozen_string_literal: true require "ryo" +require "ryo/json" require "nanoc-gzip" require "nanoc-webpack" require "nanoc-tidy" +require_relative "nanoc/lib/require_rules" -locales = %w[ar en] -slugs = Ryo.from( - JSON.parse( - File.read(File.join(Dir.getwd, "src", "json", "slugs.json")) - ) -) -i18n = Ryo.from( - JSON.parse( - File.read(File.join(Dir.getwd, "src", "json", "i18n.json")) - ) -) - -buildenv = ENV["buildenv"] || "development" +## +# Common vars +locales = %w[ar en] +json_dir = File.join(Dir.getwd, "src", "json") +name_by_id = Ryo.from_json_file("#{json_dir}/nameById.json") +i18n = Ryo.from_json_file("#{json_dir}/i18n.json") +buildenv = ENV["buildenv"] || "development" Nanoc::Webpack.default_options.merge!( "--config" => "webpack.#{buildenv}.js" ) @@ -26,44 +22,8 @@ Nanoc::Tidy.default_options.merge!( "-upper" => true ) -def require_rules(rules, locals = {}, target = binding) - locals.each { target.local_variable_set(_1, _2) } - path = File.join(Dir.getwd, rules) - target.eval( - if File.readable?(path) - File.read(path) - elsif File.readable?("#{path}.rb") - File.read("#{path}.rb") - elsif File.readable?("#{path}.rules") - File.read("#{path}.rules") - else - raise LoadError, "#{path} is not readable" - end - ) -end - ## -# Inline CSS / JSON rules -compile "/i18n.json" do - filter(:minify_json) - write(nil) -end - -compile "/surahs.json" do - filter(:minify_json) - write(nil) -end - -compile "/slugs.json" do - filter(:minify_json) - write(nil) -end - -compile "/recitations.json" do - filter(:minify_json) - write(nil) -end - +# See packages/typescript/postman compile "/css/postman.scss" do filter :sass, syntax: :scss, style: :compact filter :rainpress @@ -73,7 +33,7 @@ end ## # /sitemap.xml compile "/sitemap.xml.erb" do - filter(:erb, locals: {locales:, slugs:}) + filter(:erb, locals: {locales:, name_by_id:}) filter(:strip) write("/sitemap.xml") end @@ -88,8 +48,8 @@ end # Require rules require_rules "nanoc/rules/assets" require_rules "nanoc/rules/redirect" -require_rules "nanoc/rules/random", {locales:, i18n:, slugs:} -require_rules "nanoc/rules/stream", {locales:, i18n:, slugs:} +require_rules "nanoc/rules/random", {locales:} +require_rules "nanoc/rules/stream", {locales:, i18n:, name_by_id:} require_rules "nanoc/rules/index", {locales:, i18n:} ## diff --git a/nanoc/lib/require_rules.rb b/nanoc/lib/require_rules.rb new file mode 100644 index 0000000..aafba45 --- /dev/null +++ b/nanoc/lib/require_rules.rb @@ -0,0 +1,15 @@ +def require_rules(rules, locals = {}, target = binding) + locals.each { target.local_variable_set(_1, _2) } + path = File.join(Dir.getwd, rules) + target.eval( + if File.readable?(path) + File.read(path) + elsif File.readable?("#{path}.rb") + File.read("#{path}.rb") + elsif File.readable?("#{path}.rules") + File.read("#{path}.rules") + else + raise LoadError, "#{path} is not readable" + end + ) +end diff --git a/nanoc/rules/stream.rules b/nanoc/rules/stream.rules index 86d9c1a..45da629 100644 --- a/nanoc/rules/stream.rules +++ b/nanoc/rules/stream.rules @@ -12,22 +12,22 @@ compile "/*/*/surah.json" do end end -Ryo.each(slugs) do |id, slug| +Ryo.each(name_by_id) do |id, transliterated_name| writer = ->(locale, identifier:) do name = i18n[locale].surahs.names[id.to_i - 1] context = Ryo.from( filename: "stream.html.erb", locale:, locales:, - surah: {id:, name:, slug:} + surah: {id:, name:, transliterated_name:} ) filter(:erb, locals: {context:}) filter(:tidy) write "/#{locale}/#{identifier}/index.html" end locales.each do |locale| - compile "/html/stream.html.erb", rep: "/#{locale}/#{slug}/index.html" do - instance_exec(locale, identifier: slug, &writer) + compile "/html/stream.html.erb", rep: "/#{locale}/#{transliterated_name}/index.html" do + instance_exec(locale, identifier: transliterated_name, &writer) end compile "/html/stream.html.erb", rep: "/#{locale}/#{id}/index.html" do instance_exec(locale, identifier: id, &writer) @@ -37,8 +37,7 @@ end compile "/js/main/surah-stream.tsx" do filter :webpack, - depend_on: ["/js/components", "/js/lib/", "/js/hooks"], - reject: proc { _1.include?("WebPackage") } + depend_on: ["/js/components", "/js/lib/", "/js/hooks"] write "/js/main/surah-stream.js" filter :gzip write "/js/main/surah-stream.js.gz" diff --git a/src/html/stream.html.erb b/src/html/stream.html.erb index fb0e260..1354112 100644 --- a/src/html/stream.html.erb +++ b/src/html/stream.html.erb @@ -10,14 +10,14 @@ <% context.locales.each do |locale| %> <% end %> diff --git a/src/js/main/random.ts b/src/js/main/random.ts index 1f2c587..2113c5f 100644 --- a/src/js/main/random.ts +++ b/src/js/main/random.ts @@ -1,5 +1,5 @@ (function () { - const nameById = require("@json/slugs.json"); + const nameById = require("@json/nameById.json"); const surahId: number = Math.ceil(Math.random() * 114); const name = nameById[surahId]; const locale = location.pathname.slice(1, 3); diff --git a/src/json/slugs.json b/src/json/nameById.json similarity index 100% rename from src/json/slugs.json rename to src/json/nameById.json diff --git a/src/sitemap.xml.erb b/src/sitemap.xml.erb index 3c5717d..da1e548 100644 --- a/src/sitemap.xml.erb +++ b/src/sitemap.xml.erb @@ -7,9 +7,9 @@ 1.0 weekly - <% Ryo.each(slugs) do |_id, slug| %> + <% Ryo.each(name_by_id) do |_id, transliterated_name| %> - https://al-quran.reflectslight.io/<%= locale %>/<%= slug %>/ + https://al-quran.reflectslight.io/<%= locale %>/<%= transliterated_name %>/ 0.7 weekly