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 83 84 85 86 87 88 89
|
# frozen_string_literal: true
module JekyllOptionalFrontMatter
class Generator < Jekyll::Generator
attr_accessor :site
safe true
priority :normal
CONFIG_KEY = "optional_front_matter"
ENABLED_KEY = "enabled"
CLEANUP_KEY = "remove_originals"
def initialize(site)
@site = site
end
def generate(site)
@site = site
return if disabled?
site.pages.concat(pages_to_add)
site.static_files -= static_files_to_remove if cleanup?
end
private
# An array of Jekyll::Pages to add, *excluding* blacklisted files
def pages_to_add
pages.reject { |page| blacklisted?(page) }
end
# An array of Jekyll::StaticFile's, *excluding* blacklisted files
def static_files_to_remove
markdown_files.reject { |page| blacklisted?(page) }
end
# An array of potential Jekyll::Pages to add, *including* blacklisted files
def pages
markdown_files.map { |static_file| page_from_static_file(static_file) }
end
# An array of Jekyll::StaticFile's with a site-defined markdown extension
def markdown_files
site.static_files.select { |file| markdown_converter.matches(file.extname) }
end
# Given a Jekyll::StaticFile, returns the file as a Jekyll::Page
def page_from_static_file(static_file)
base = static_file.instance_variable_get("@base")
dir = static_file.instance_variable_get("@dir")
name = static_file.instance_variable_get("@name")
Jekyll::Page.new(site, base, dir, name)
end
# Does the given Jekyll::Page match our filename blacklist?
def blacklisted?(page)
return false if whitelisted?(page)
FILENAME_BLACKLIST.include?(page.basename.upcase)
end
def whitelisted?(page)
return false unless site.config["include"].is_a? Array
entry_filter.included?(page.relative_path)
end
def markdown_converter
@markdown_converter ||= site.find_converter_instance(Jekyll::Converters::Markdown)
end
def entry_filter
@entry_filter ||= Jekyll::EntryFilter.new(site)
end
def option(key)
site.config[CONFIG_KEY] && site.config[CONFIG_KEY][key]
end
def disabled?
option(ENABLED_KEY) == false || site.config["require_front_matter"]
end
def cleanup?
option(CLEANUP_KEY) == true || site.config["require_front_matter"]
end
end
end
|