Replace slugs.json with nameById.json

This commit is contained in:
0x1eef 2024-03-16 21:31:06 -03:00
parent 43bd4f1fc4
commit 05ac909e80
8 changed files with 39 additions and 65 deletions

View file

@ -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)

66
Rules
View file

@ -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:}
##

View file

@ -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

View file

@ -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"

View file

@ -10,14 +10,14 @@
<link
rel="canonical"
hreflang="<%= context.locale %>"
href="https://al-quran.reflectslight.io/<%= context.locale %>/<%= context.surah.slug %>/"
href="https://al-quran.reflectslight.io/<%= context.locale %>/<%= context.surah.transliterated_name %>/"
/>
<% context.locales.each do |locale| %>
<link
rel="alternate"
type="text/html"
hreflang="<%= locale %>"
href="https://al-quran.reflectslight.io/<%= locale %>/<%= context.surah.slug %>/"
href="https://al-quran.reflectslight.io/<%= locale %>/<%= context.surah.transliterated_name %>/"
/>
<% end %>
<link rel="icon" href="/favicon.svg">

View file

@ -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);

View file

@ -7,9 +7,9 @@
<priority>1.0</priority>
<changefreq>weekly</changefreq>
</url>
<% Ryo.each(slugs) do |_id, slug| %>
<% Ryo.each(name_by_id) do |_id, transliterated_name| %>
<url>
<loc>https://al-quran.reflectslight.io/<%= locale %>/<%= slug %>/</loc>
<loc>https://al-quran.reflectslight.io/<%= locale %>/<%= transliterated_name %>/</loc>
<priority>0.7</priority>
<changefreq>weekly</changefreq>
</url>