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/random.rules b/nanoc/rules/random.rules index 9186b9c..58606d3 100644 --- a/nanoc/rules/random.rules +++ b/nanoc/rules/random.rules @@ -15,6 +15,6 @@ locales.each do |locale| end compile "/js/main/random.ts" do - filter(:webpack) + filter(:webpack, depend_on: ["/js/lib/"]) write("/js/main/random.js") end diff --git a/nanoc/rules/redirect.rules b/nanoc/rules/redirect.rules index 8273425..f3509db 100644 --- a/nanoc/rules/redirect.rules +++ b/nanoc/rules/redirect.rules @@ -12,6 +12,6 @@ compile "/html/redirect.html.erb" do end compile "/js/main/redirect.ts" do - filter(:webpack) + filter(:webpack, depend_on: ["/js/lib/"]) write("/js/main/redirect.js") 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/random.html.erb b/src/html/random.html.erb index 5055821..7c2c737 100644 --- a/src/html/random.html.erb +++ b/src/html/random.html.erb @@ -5,7 +5,6 @@ <%= erb("partials/favicon.html.erb") %>
- <%= inline_json("/json/slugs.json") %>