Add 'Mixin'
This commit is contained in:
parent
600f5cb799
commit
4850546eb1
6 changed files with 58 additions and 48 deletions
19
nanoc/lib/mixin.rb
Normal file
19
nanoc/lib/mixin.rb
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Mixin
|
||||||
|
require_relative "mixin/t"
|
||||||
|
require_relative "mixin/inline"
|
||||||
|
require_relative "mixin/erb"
|
||||||
|
require_relative "mixin/opengraph"
|
||||||
|
|
||||||
|
def build_dir
|
||||||
|
nanoc = Ryo.from YAML.load_file(File.join(Dir.getwd, "nanoc.yaml"))
|
||||||
|
nanoc.output_dir
|
||||||
|
end
|
||||||
|
|
||||||
|
include T
|
||||||
|
include Inline
|
||||||
|
include ERB
|
||||||
|
include OpenGraph
|
||||||
|
end
|
||||||
|
use_helper Mixin
|
8
nanoc/lib/mixin/erb.rb
Normal file
8
nanoc/lib/mixin/erb.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
module Mixin::ERB
|
||||||
|
def erb(partial, local_assigns = {})
|
||||||
|
erb = File.binread File.join(Dir.getwd, "src", "html", "partials", partial)
|
||||||
|
scope = binding
|
||||||
|
local_assigns.each { scope.local_variable_set(_1, _2) }
|
||||||
|
::ERB.new(erb).result(scope)
|
||||||
|
end
|
||||||
|
end
|
15
nanoc/lib/mixin/inline.rb
Normal file
15
nanoc/lib/mixin/inline.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
module Mixin::Inline
|
||||||
|
def inline_json(path)
|
||||||
|
class_name = File.basename(path, File.extname(path))
|
||||||
|
"<script class='json #{class_name}' type='application/json'>" \
|
||||||
|
"#{items[path].compiled_content}" \
|
||||||
|
"</script>"
|
||||||
|
end
|
||||||
|
|
||||||
|
def inline_css(path)
|
||||||
|
class_name = File.basename(path, File.extname(path))
|
||||||
|
"<style class='css #{class_name}'>" \
|
||||||
|
"#{File.binread(File.join(build_dir, "css", "postman.css"))}" \
|
||||||
|
"</style>"
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,9 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
use_helper Module.new {
|
module Mixin::OpenGraph
|
||||||
require_relative "helper"
|
|
||||||
include Helper
|
|
||||||
|
|
||||||
def opengraph(context)
|
def opengraph(context)
|
||||||
erb "opengraph.html.erb", local_assigns(context)
|
erb "opengraph.html.erb", local_assigns(context)
|
||||||
end
|
end
|
||||||
|
@ -17,4 +14,4 @@ use_helper Module.new {
|
||||||
image: "https://al-quran.reflectslight.io/images/opengraph/#{context.surah.id}.png"}
|
image: "https://al-quran.reflectslight.io/images/opengraph/#{context.surah.id}.png"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
}
|
end
|
14
nanoc/lib/mixin/t.rb
Normal file
14
nanoc/lib/mixin/t.rb
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
module Mixin::T
|
||||||
|
def t(locale, key, locals = {})
|
||||||
|
str = [locale, *key.split(".")].inject(i18n) { |h, k| h[k] }
|
||||||
|
format(str, locals)
|
||||||
|
end
|
||||||
|
|
||||||
|
def i18n
|
||||||
|
@i18n ||= Ryo.from(
|
||||||
|
JSON.parse(
|
||||||
|
File.read(File.join(Dir.getwd, "src", "i18n.json"))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,43 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Helper
|
|
||||||
def inline_json(path)
|
|
||||||
class_name = File.basename(path, File.extname(path))
|
|
||||||
"<script class='json #{class_name}' type='application/json'>" \
|
|
||||||
"#{items[path].compiled_content}" \
|
|
||||||
"</script>"
|
|
||||||
end
|
|
||||||
|
|
||||||
def inline_css(path)
|
|
||||||
class_name = File.basename(path, File.extname(path))
|
|
||||||
"<style class='css #{class_name}'>" \
|
|
||||||
"#{File.binread(File.join(build_dir, "css", "postman.css"))}" \
|
|
||||||
"</style>"
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_dir
|
|
||||||
nanoc = Ryo.from YAML.load_file(File.join(Dir.getwd, "nanoc.yaml"))
|
|
||||||
nanoc.output_dir
|
|
||||||
end
|
|
||||||
|
|
||||||
def t(locale, key, locals = {})
|
|
||||||
str = [locale, *key.split(".")].inject(i18n) { |h, k| h[k] }
|
|
||||||
format(str, locals)
|
|
||||||
end
|
|
||||||
|
|
||||||
def i18n
|
|
||||||
@i18n ||= Ryo.from(
|
|
||||||
JSON.parse(
|
|
||||||
File.read(File.join(Dir.getwd, "src", "i18n.json"))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def erb(partial, local_assigns = {})
|
|
||||||
erb = File.binread File.join(Dir.getwd, "src", "html", "partials", partial)
|
|
||||||
scope = binding
|
|
||||||
local_assigns.each { scope.local_variable_set(_1, _2) }
|
|
||||||
ERB.new(erb).result(scope)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
use_helper Helper
|
|
Loading…
Reference in a new issue