Update mixin.rb

This commit is contained in:
0x1eef 2024-06-21 19:04:17 -03:00
parent 401464c615
commit 51e507e649
10 changed files with 51 additions and 36 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -1,2 +0,0 @@
<meta name="version" content="<%= app_version %>">
<meta name="revision" content="<%= revision %>">

View file

@ -0,0 +1,2 @@
<meta name="version" content="<%= version %>">
<meta name="revision" content="<%= commit %>">

View file

@ -3,7 +3,7 @@
<head>
<title><%= t(locale, "TheNobleQuran") %></title>
<meta name="description" content="<%= t(locale, 'meta.random.description') %>">
<%= erb("_revision.html.erb") %>
<%= erb("_version.html.erb") %>
<%= opengraph(context) %>
<link
rel="canonical"
@ -18,6 +18,6 @@
<%= erb("_favicon.html.erb") %>
</head>
<body>
<script src="/js/main/random.js?v=<%= revision %>"></script>
<script src="/js/main/random.js?v=<%= commit %>"></script>
</body>
</html>

View file

@ -2,7 +2,7 @@
<html lang="en" dir="ltr">
<head>
<title><%= t("en", "TheNobleQuran") %></title>
<%= erb("_revision.html.erb") %>
<%= erb("_version.html.erb") %>
<%= opengraph(Ryo(filename: "redirect.html.erb", locale: "en")) %>
<link
rel="canonical"
@ -17,6 +17,6 @@
<%= erb("_favicon.html.erb") %>
</head>
<body>
<script src="/js/main/redirect.js?v=<%= revision %>"></script>
<script src="/js/main/redirect.js?v=<%= commit %>"></script>
</body>
</html>

View file

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta charset="UTF-8">
<meta name="description" content="<%= t(locale, 'meta.index.description') %>">
<%= erb("_revision.html.erb") %>
<%= erb("_version.html.erb") %>
<%= opengraph(context) %>
<link
rel="canonical"
@ -22,6 +22,6 @@
<body>
<%= erb("_postman.html.erb", {locale:, dir:}) %>
<div class="root h-full"></div>
<script src="/js/loaders/surah-index-loader.js?v=<%= revision %>"></script>
<script src="/js/loaders/surah-index-loader.js?v=<%= commit %>"></script>
</body>
</html>

View file

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<meta name="description" content="<%= t(locale, 'meta.stream.description', {surah_name: surah.name}) %>">
<%= erb("_revision.html.erb") %>
<%= erb("_version.html.erb") %>
<%= opengraph(context) %>
<link
rel="canonical"
@ -25,6 +25,6 @@
data-surah-id="<%= surah.id %>"
data-audio-base-url="<%= audio_base_url %>">
</div>
<script src="/js/loaders/surah-stream-loader.js?v=<%= revision %>"></script>
<script src="/js/loaders/surah-stream-loader.js?v=<%= commit %>"></script>
</body>
</html>