forked from bitcoin-dot-org/Bitcoin.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude_absolute.rb
82 lines (67 loc) · 2.46 KB
/
include_absolute.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module Jekyll
class IncludeTagError < StandardError
attr_accessor :path
def initialize(msg, path)
super(msg)
@path = path
end
end
class IncludeAbsoluteTag < Liquid::Tag
VARIABLE_SYNTAX = %r!
(?<variable>[^{]*(\{\{\s*[\w\-\.]+\s*(\|.*)?\}\}[^\s{}]*)+)
(?<params>.*)
!x
def initialize(tag_name, markup, tokens)
super
path = markup.strip.split(' ')
@file = path[0].strip
if !path[1].nil?
@file_fallback = path[1].strip
end
end
# Render the variable if required (@see https://goo.gl/N5sMV3)
def render_variable(context)
if @file.match(VARIABLE_SYNTAX)
partial = context.registers[:site]
.liquid_renderer
.file("(variable)")
.parse(@file)
partial.render!(context)
end
end
def render(context)
file = render_variable(context) || @file
source = File.expand_path(context.registers[:site].config['source']).freeze
path = File.join(source, file)
begin
partial = Liquid::Template.parse(read_file(path, context))
context.stack do
context['include'] = parse_params(context) if @params
partial.render!(context)
end
rescue => e
if !@file_fallback.nil?
begin
path_fallback = File.join(source, @file_fallback)
partial = Liquid::Template.parse(read_file(path_fallback, context))
context.stack do
context['include'] = parse_params(context) if @params
partial.render!(context)
end
rescue => e
raise IncludeTagError.new e.message, path_fallback
end
else
raise IncludeTagError.new e.message, path
end
end
end
def read_file(file, context)
File.read(file, file_read_opts(context))
end
def file_read_opts(context)
context.registers[:site].file_read_opts
end
end
end
Liquid::Template.register_tag('include_absolute', Jekyll::IncludeAbsoluteTag)