From 7c2825b25d736c7fd64fcc82f04f4d7785f9e434 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sat, 23 Aug 2025 17:06:16 -0500 Subject: [PATCH] Add target config setting and front matter variables so one could create multiple outputs from a single site codebase with various target site included files --- docs/_docs/configuration/default.md | 1 + docs/_docs/deployment.md | 1 + lib/jekyll/collection.rb | 9 ++++++--- lib/jekyll/configuration.rb | 1 + lib/jekyll/document.rb | 9 ++++++--- lib/jekyll/drops/document_drop.rb | 2 +- lib/jekyll/page.rb | 2 +- lib/jekyll/readers/post_reader.rb | 3 +++ lib/jekyll/site.rb | 6 +++--- 9 files changed, 23 insertions(+), 11 deletions(-) diff --git a/docs/_docs/configuration/default.md b/docs/_docs/configuration/default.md index e96c5071..bda69e8e 100644 --- a/docs/_docs/configuration/default.md +++ b/docs/_docs/configuration/default.md @@ -43,6 +43,7 @@ show_drafts : null limit_posts : 0 future : false unpublished : false +target : "default" # Plugins whitelist : [] diff --git a/docs/_docs/deployment.md b/docs/_docs/deployment.md index 45722e4e..9e69d909 100644 --- a/docs/_docs/deployment.md +++ b/docs/_docs/deployment.md @@ -9,3 +9,4 @@ Sites built using Jekyll can be deployed in a large number of ways due to the st * [Manually]({{ '/docs/deployment/manual/' | relative_url }}) * [Automated]({{ '/docs/deployment/automated/' | relative_url }}) * [Third Party]({{ '/docs/deployment/third-party/' | relative_url }}) +* [Multiple Sites and Target]({{ '/docs/deployment/multiple-sites' | relative_url }}) diff --git a/lib/jekyll/collection.rb b/lib/jekyll/collection.rb index 1ce33bf2..3a20af79 100644 --- a/lib/jekyll/collection.rb +++ b/lib/jekyll/collection.rb @@ -183,9 +183,12 @@ module Jekyll # Whether the collection's documents ought to be written as individual # files in the output. # - # Returns true if the 'write' metadata is true, false otherwise. + # Returns true if the 'write' metadata is true and + # if the collection's target matches the site's target, + # false otherwise. def write? - !!metadata.fetch("output", false) + !!metadata.fetch("output", false) && + (site.target == metadata.fetch("target", site.target)) end # The URL template to render collection's documents at. @@ -217,7 +220,7 @@ module Jekyll def read_document(full_path) doc = Document.new(full_path, :site => site, :collection => self) doc.read - docs << doc if site.unpublished || doc.published? + docs << doc if (site.unpublished || doc.published?) && (site.target == doc.data.fetch("target", site.target)) end def sort_docs! diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb index 9b2fbffc..ad19b2e7 100644 --- a/lib/jekyll/configuration.rb +++ b/lib/jekyll/configuration.rb @@ -30,6 +30,7 @@ module Jekyll "limit_posts" => 0, "future" => false, "unpublished" => false, + "target" => "default", # Plugins "whitelist" => [], diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index 2e39024e..4f03cec8 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -348,17 +348,20 @@ module Jekyll end # Determine whether this document should be written. - # Based on the Collection to which it belongs. + # Based on the Collection to which it belongs + # and site and document target. # # True if the document has a collection and if that collection's #write? - # method returns true, and if the site's Publisher will publish the document. + # method returns true, and if the site's Publisher will publish the document, + # and if the document's target matches the site target or is undefined. # False otherwise. # # rubocop:disable Naming/MemoizedInstanceVariableName def write? return @write_p if defined?(@write_p) - @write_p = collection&.write? && site.publisher.publish?(self) + @write_p = collection&.write? && site.publisher.publish?(self) && + (site.target == data.fetch("target", site.target)) end # rubocop:enable Naming/MemoizedInstanceVariableName diff --git a/lib/jekyll/drops/document_drop.rb b/lib/jekyll/drops/document_drop.rb index 0cff3741..b1bf3ed0 100644 --- a/lib/jekyll/drops/document_drop.rb +++ b/lib/jekyll/drops/document_drop.rb @@ -15,7 +15,7 @@ module Jekyll private delegate_method_as :data, :fallback_data delegate_methods :id, :output, :content, :to_s, :relative_path, :url, :date - data_delegators "title", "categories", "tags" + data_delegators "title", "categories", "tags", :target def collection @obj.collection.label diff --git a/lib/jekyll/page.rb b/lib/jekyll/page.rb index 64821a82..c94767cb 100644 --- a/lib/jekyll/page.rb +++ b/lib/jekyll/page.rb @@ -180,7 +180,7 @@ module Jekyll end def write? - true + site.config["target"] == data.fetch("target", site.config["target"]) end def excerpt_separator diff --git a/lib/jekyll/readers/post_reader.rb b/lib/jekyll/readers/post_reader.rb index 25c5f98f..23a5260d 100644 --- a/lib/jekyll/readers/post_reader.rb +++ b/lib/jekyll/readers/post_reader.rb @@ -69,6 +69,9 @@ module Jekyll elsif !doc.content.valid_encoding? Jekyll.logger.debug "Skipping:", "#{doc.relative_path} is not valid UTF-8" false + elsif site.target != doc.data.fetch("target", site.target) + Jekyll.logger.debug "Skipping:", "#{doc.relative_path} does not belong to this site target" + false else publishable?(doc) end diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index adec4cc0..f531878e 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -6,7 +6,7 @@ module Jekyll :file_read_opts, :future, :gems, :generators, :highlighter, :include, :inclusions, :keep_files, :layouts, :limit_posts, :lsi, :pages, :permalink_style, :plugin_manager, :plugins, - :reader, :safe, :show_drafts, :static_files, :theme, :time, + :reader, :safe, :target, :show_drafts, :static_files, :theme, :time, :unpublished attr_reader :cache_dir, :config, :dest, :filter_cache, :includes_load_paths, @@ -47,7 +47,7 @@ module Jekyll def config=(config) @config = config.clone - %w(safe lsi highlighter baseurl exclude include future unpublished + %w(safe lsi highlighter baseurl exclude include future unpublished target show_drafts limit_posts keep_files).each do |opt| send(:"#{opt}=", config[opt]) end @@ -360,7 +360,7 @@ module Jekyll end def each_site_file - pages.each { |page| yield page } + pages.each { |page| yield (page) if page.write? } static_files.each { |file| yield(file) if file.write? } collections.each_value { |coll| coll.docs.each { |doc| yield(doc) if doc.write? } } end