diff --git a/nanoc/lib/mixin.rb b/nanoc/lib/mixin.rb index 3b7608c37..812ca5658 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 0021414cf..efbbdf429 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 e9fd834a7..31624bfe5 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 649fbd8b3..4559a2c4b 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 7d52c10d4..000000000 --- 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 000000000..d527372f0 --- /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 96fc2a3e6..99a94e55e 100644 --- a/src/html/main/random.html.erb +++ b/src/html/main/random.html.erb @@ -3,7 +3,7 @@ <%= t(locale, "TheNobleQuran") %> - <%= erb("_revision.html.erb") %> + <%= erb("_version.html.erb") %> <%= opengraph(context) %> - + diff --git a/src/html/main/redirect.html.erb b/src/html/main/redirect.html.erb index 5cb03359a..f38456597 100644 --- a/src/html/main/redirect.html.erb +++ b/src/html/main/redirect.html.erb @@ -2,7 +2,7 @@ <%= t("en", "TheNobleQuran") %> - <%= erb("_revision.html.erb") %> + <%= erb("_version.html.erb") %> <%= opengraph(Ryo(filename: "redirect.html.erb", locale: "en")) %> - + diff --git a/src/html/main/surah-index.html.erb b/src/html/main/surah-index.html.erb index 808a02f87..6636ff843 100644 --- a/src/html/main/surah-index.html.erb +++ b/src/html/main/surah-index.html.erb @@ -6,7 +6,7 @@ - <%= erb("_revision.html.erb") %> + <%= erb("_version.html.erb") %> <%= opengraph(context) %> <%= erb("_postman.html.erb", {locale:, dir:}) %>
- + diff --git a/src/html/main/surah-stream.html.erb b/src/html/main/surah-stream.html.erb index bc11ba524..299b20110 100644 --- a/src/html/main/surah-stream.html.erb +++ b/src/html/main/surah-stream.html.erb @@ -6,7 +6,7 @@ - <%= erb("_revision.html.erb") %> + <%= erb("_version.html.erb") %> <%= opengraph(context) %> - +