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 90 91 92 93 94 95 96 97 98
|
require "jekyll"
require "uri"
module Jekyll
module GitHubMetadata
class SiteGitHubMunger
extend Forwardable
class << self
attr_accessor :global_munger
end
def_delegators Jekyll::GitHubMetadata, :site, :repository
private :repository
def initialize(site)
Jekyll::GitHubMetadata.site = site
end
def munge!
Jekyll::GitHubMetadata.log :debug, "Initializing..."
add_title_and_description_fallbacks!
add_url_and_baseurl_fallbacks! if should_add_url_fallbacks?
end
def inject_metadata!(payload)
payload.site["github"] = github_namespace
end
private
def github_namespace
case site.config["github"]
when nil
drop
when Hash
Jekyll::Utils.deep_merge_hashes(drop, site.config["github"])
else
site.config["github"]
end
end
def drop
@drop ||= MetadataDrop.new(GitHubMetadata.site)
end
# Set `site.url` and `site.baseurl` if unset.
def add_url_and_baseurl_fallbacks!
site.config["url"] ||= Value.new("url", proc { |_c, r| r.url_without_path })
return unless should_set_baseurl?
site.config["baseurl"] = Value.new("baseurl", proc { |_c, r| r.baseurl })
end
def add_title_and_description_fallbacks!
if should_warn_about_site_name?
msg = "site.name is set in _config.yml, but many plugins and themes expect "
msg << "site.title to be used instead. To avoid potential inconsistency, "
msg << "Jekyll GitHub Metadata will not set site.title to the repository's name."
Jekyll::GitHubMetadata.log :warn, msg
else
site.config["title"] ||= Value.new("title", proc { |_context, repository|
if repository.project_page?
repository.name
else
repository.owner_display_name || repository.owner
end
})
end
site.config["description"] ||= Value.new("description", proc { |_c, r| r.tagline })
end
# Set the baseurl only if it is `nil` or `/`
# Baseurls should never be "/". See http://bit.ly/2s1Srid
def should_set_baseurl?
site.config["baseurl"].nil? || site.config["baseurl"] == "/"
end
def should_add_url_fallbacks?
Jekyll.env == "production" || Pages.page_build?
end
def should_warn_about_site_name?
site.config["name"] && !site.config["title"]
end
end
Jekyll::Hooks.register :site, :after_init do |site|
SiteGitHubMunger.global_munger = SiteGitHubMunger.new(site)
SiteGitHubMunger.global_munger.munge!
end
Jekyll::Hooks.register :site, :pre_render do |_site, payload|
SiteGitHubMunger.global_munger.inject_metadata!(payload)
end
end
end
|