From 51e507e64958a5cd8c37918077e7fd174011fdcf Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 21 Jun 2024 19:04:17 -0300 Subject: [PATCH] Update mixin.rb --- nanoc/lib/mixin.rb | 50 ++++++++++++++++++++--------- nanoc/lib/mixin/erb.rb | 10 +++--- nanoc/lib/mixin/opengraph.rb | 2 +- nanoc/lib/mixin/t.rb | 5 +-- src/html/_revision.html.erb | 2 -- src/html/_version.html.erb | 2 ++ src/html/main/random.html.erb | 4 +-- src/html/main/redirect.html.erb | 4 +-- src/html/main/surah-index.html.erb | 4 +-- src/html/main/surah-stream.html.erb | 4 +-- 10 files changed, 51 insertions(+), 36 deletions(-) delete mode 100644 src/html/_revision.html.erb create mode 100644 src/html/_version.html.erb diff --git a/nanoc/lib/mixin.rb b/nanoc/lib/mixin.rb index 3b7608c..812ca56 100644 --- a/nanoc/lib/mixin.rb +++ b/nanoc/lib/mixin.rb @@ -7,19 +7,44 @@ module Mixin require_relative "mixin/erb" require_relative "mixin/opengraph" - def app_version - @app_version ||= begin - ver = File.read File.join(Dir.getwd, "VERSION") - ver.gsub(/[^\d.]/, "") - end + ## + # @return [Ryo::Object] + # Returns common directory paths as a Ryo object + def dirs + @dirs ||= Ryo( + root:, + build: nanoc.output_dir, + content: File.join(root, nanoc.data_sources[0].content_dir) + ) end - def revision - @rev ||= cmd("git", "rev-parse", "HEAD").stdout.strip + ## + # @return [Ryo::Object] + # Returns the contents of nanoc.yaml as a Ryo object + def nanoc + @nanoc ||= Ryo.from_yaml(path: File.join(root, "nanoc.yaml")) end - def build_dir - nanoc.output_dir + ## + # @return [String] + # Returns an absolute path to the root directory of the website + def root + @root ||= File.realpath(File.join(__dir__, "..", "..", ".")) + end + + ## + # @return [String] + # Returns the website version + def version + @version ||= File.read(File.join(dirs.root, "VERSION")) + .gsub(/[^\d.]/, "") + end + + ## + # @return [String] + # Returns the most recent git commit hash + def commit + @commit ||= cmd("git", "rev-parse", "HEAD").stdout.strip end ## @@ -44,13 +69,6 @@ module Mixin nanoc.server.base_url end - ## - # @return [Ryo::Object] - # Returns the contents of nanoc.yaml as a Ryo object - def nanoc - @nanoc ||= Ryo.from_yaml(path: File.join(Dir.getwd, "nanoc.yaml")) - end - include T include Inline include ERB diff --git a/nanoc/lib/mixin/erb.rb b/nanoc/lib/mixin/erb.rb index 0021414..efbbdf4 100644 --- a/nanoc/lib/mixin/erb.rb +++ b/nanoc/lib/mixin/erb.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true module Mixin::ERB - def erb(path, local_assigns = {}) - erb = File.binread File.join(Dir.getwd, "src", "html", path) - scope = binding - local_assigns.each { scope.local_variable_set(_1, _2) } - ::ERB.new(erb).result(scope) + def erb(file, local_assigns = {}) + erb = File.binread File.join(dirs.content, "html", file) + ctx = binding + local_assigns.each { ctx.local_variable_set(_1, _2) } + ::ERB.new(erb).result(ctx) end end diff --git a/nanoc/lib/mixin/opengraph.rb b/nanoc/lib/mixin/opengraph.rb index e9fd834..31624bf 100644 --- a/nanoc/lib/mixin/opengraph.rb +++ b/nanoc/lib/mixin/opengraph.rb @@ -12,7 +12,7 @@ module Mixin::OpenGraph {title: t(context.locale, "TheNobleQuran"), description: surah.name, url: "#{base_url}/#{context.locale}/#{surah.slug}/", - image: "#{base_url}/images/og/#{surah.id}.png?v=#{revision}"} + image: "#{base_url}/images/og/#{surah.id}.png?v=#{commit}"} when "redirect.html.erb", "surah-index.html.erb" {title: t(context.locale, "TheNobleQuran"), description: t(context.locale, "meta.index.description"), diff --git a/nanoc/lib/mixin/t.rb b/nanoc/lib/mixin/t.rb index 649fbd8..4559a2c 100644 --- a/nanoc/lib/mixin/t.rb +++ b/nanoc/lib/mixin/t.rb @@ -7,9 +7,6 @@ module Mixin::T end def tdata - @tdata ||= begin - path = File.join(Dir.getwd, "src", "json", "t.json") - Ryo.from_json(path:) - end + @tdata ||= Ryo.from_json(path: File.join(dirs.content, "json", "t.json")) end end diff --git a/src/html/_revision.html.erb b/src/html/_revision.html.erb deleted file mode 100644 index 7d52c10..0000000 --- a/src/html/_revision.html.erb +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/src/html/_version.html.erb b/src/html/_version.html.erb new file mode 100644 index 0000000..d527372 --- /dev/null +++ b/src/html/_version.html.erb @@ -0,0 +1,2 @@ + + diff --git a/src/html/main/random.html.erb b/src/html/main/random.html.erb index 96fc2a3..99a94e5 100644 --- a/src/html/main/random.html.erb +++ b/src/html/main/random.html.erb @@ -3,7 +3,7 @@