Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/jekyll-github-metadata/site_github_munger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module GitHubMetadata
class SiteGitHubMunger
extend Forwardable

class << self
attr_accessor :global_munger
end

def_delegators Jekyll::GitHubMetadata, :site, :repository
private :repository

Expand All @@ -16,13 +20,14 @@ def initialize(site)
def munge!
Jekyll::GitHubMetadata.log :debug, "Initializing..."

# This is the good stuff.
site.config["github"] = github_namespace

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that payload is munged outside the #munge! method, perhaps we can assign the metadata directly..

payload.site["github"] = case site.config["github"]
                           ...
                         end

Not a blocker to merge current PR, though.

end

private

def github_namespace
Expand Down Expand Up @@ -80,9 +85,14 @@ def should_warn_about_site_name?
site.config["name"] && !site.config["title"]
end
end
end
end

Jekyll::Hooks.register :site, :after_init do |site|
Jekyll::GitHubMetadata::SiteGitHubMunger.new(site).munge!
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
21 changes: 12 additions & 9 deletions spec/site_github_munger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,46 @@
let(:site) { Jekyll::Site.new(Jekyll::Configuration.from(user_config)) }
subject { described_class.new(site) }
let!(:stubs) { stub_all_api_requests }
let(:unified_payload) { site.site_payload }

context "generating" do
before(:each) do
ENV["JEKYLL_ENV"] = "production"
subject.munge!
subject.inject_metadata!(unified_payload)
end

context "with site.github as nil" do
it "replaces site.github with the drop" do
expect(site.config["github"]).to be_a(Liquid::Drop)
it "sets site.github to the drop" do
expect(unified_payload.site["github"]).to be_a(Liquid::Drop)
end
end

context "without site.github" do
let(:user_config) { {} }

it "replaces site.github with the drop" do
expect(site.config["github"]).to be_a(Liquid::Drop)
expect(unified_payload.site["github"]).to be_a(Liquid::Drop)
end
end

context "with site.github as a non-hash" do
let(:github_namespace) { "foo" }

it "doesn't munge" do
expect(site.config["github"]).to eql("foo")
expect(unified_payload.site["github"]).to eql("foo")
end
end

context "with site.github as a hash" do
let(:github_namespace) { { "source" => { "branch" => "foo" } } }

it "lets user-specified values override the drop" do
expect(site.config["github"].invoke_drop("source")["branch"]).to eql("foo")
expect(unified_payload.site["github"].invoke_drop("source")["branch"]).to eql("foo")
end

it "still sets other values" do
expect(site.config["github"].invoke_drop("source")["path"]).to eql("/")
expect(unified_payload.site["github"].invoke_drop("source")["path"]).to eql("/")
end
end

Expand Down Expand Up @@ -202,8 +204,8 @@
end

it "sets the site.github config" do
subject.munge!
expect(site.config["github"]).to be_instance_of(Jekyll::GitHubMetadata::MetadataDrop)
subject.inject_metadata!(unified_payload)
expect(unified_payload.site["github"]).to be_instance_of(Jekyll::GitHubMetadata::MetadataDrop)
end
end

Expand All @@ -223,8 +225,9 @@

it "fails loudly upon call to any drop method" do
subject.munge!
subject.inject_metadata!(unified_payload)
expect do
site.config["github"]["url"]
unified_payload.site["github"]["url"]
end.to raise_error(Jekyll::GitHubMetadata::Client::BadCredentialsError)
end
end
Expand Down