Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/file_exists.rb
1677 views
1
module Jekyll
2
module Tags
3
##
4
# This class adds a tag that checks if a file exists.
5
class FileExistsTag < Liquid::Tag
6
def initialize(tag_name, path, tokens) # :nodoc:
7
super
8
@path = path
9
end
10
11
##
12
# file_exists - Check if a file exists and return an appropriate boolean
13
#
14
# Examples:
15
#
16
# {% capture hasfaq %}{% file_exists {{faqpage}} %}{% endcapture %}
17
def render(context)
18
# Pipe parameter through Liquid to make additional replacements possible
19
url = Liquid::Template.parse(@path).render context
20
21
# Adds the site source, so that it also works with a custom one
22
site_source = context.registers[:site].config['source']
23
file_path = "#{site_source}/#{url}"
24
25
# Check if file exists (returns true or false)
26
File.exist?(file_path.strip!).to_s
27
end
28
end
29
end
30
end
31
32
Liquid::Template.register_tag('file_exists', Jekyll::Tags::FileExistsTag)
33
34