diff --git a/README.md b/README.md index 991ac03..4b3938d 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,10 @@ A Liquid tag plugin for Jekyll that allows remote includes from external sources ## Installation Add this line to your application's Gemfile: + ```ruby group :jekyll_plugins do - gem 'jekyll-remote-include', "~> 1.1.3", github: 'ianjevans/jekyll-remote-include' + gem 'jekyll-remote-include', "~> 1.1.4", github: 'ianjevans/jekyll-remote-include' end ``` @@ -30,7 +31,7 @@ plugins: ## Usage -The tag can be used to include the entire contents of a file, or just the content between begin and end tokens, in your Jekyll pages, posts and collections. +The tag can be used to include the entire contents of a file, or just the content between begin and end tokens, in your Jekyll pages, posts and collections. The remote content is [cached](https://jekyllrb.com/tutorials/cache-api/) to improve build times. To include the entire file: @@ -44,12 +45,25 @@ To include only a portion of the file, specify the begin and end tokens separate {% remote_include https://raw.githubusercontent.com/jekyll/jekyll/master/README.markdown|begin_token|end_token %} ``` -If the linked file doesn't contain the begin or end tokens, the entire file will be included. +The `on_token_error` configuration option handles scenarios where the linked file doesn't contain the begin or end tokens. The build log will include information about which file contains the token error and whether the begin or end token is missing. + +Set `on_token_error` to `fail` (the default) to stop the build with a failure if the begin or end token doesn't exist in the target file. + +Set `on_token_error` to `full` if you want to include the entire remote file if there are token errors. + +Set `on_token_error` to `none` if you want no content from the `remote_include`. + +To use `on_token_error` add a `remote_include` section to your site's `_config.yml` and set `on_token_error`. + +```yaml +remote_include: + on_token_error: full +``` ## Contributing -1. Fork it. -2. Create your feature branch (`git checkout -b my-new-feature`) -3. Commit your changes (`git commit -am 'Add some feature'`) -4. Push to the branch (`git push origin my-new-feature`) -5. Create a new Pull Request +1. Fork it. +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request diff --git a/jekyll-remote-include.gemspec b/jekyll-remote-include.gemspec index 52703f9..be2d483 100644 --- a/jekyll-remote-include.gemspec +++ b/jekyll-remote-include.gemspec @@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) Gem::Specification.new do |spec| spec.name = "jekyll-remote-include" - spec.version = "1.1.3" + spec.version = "1.1.4" spec.authors = ["Jason Maggiacomo", "Kris Northfield", "Ian Evans"] spec.email = ["jason.maggiacomo@gmail.com"] spec.summary = "A Liquid tag plugin for Jekyll that allows remote includes from external assets." diff --git a/lib/jekyll-remote-include.rb b/lib/jekyll-remote-include.rb index d049c7a..c9e8ec9 100644 --- a/lib/jekyll-remote-include.rb +++ b/lib/jekyll-remote-include.rb @@ -6,13 +6,24 @@ module Jekyll class RemoteInclude < Liquid::Tag + def cache + @@cache ||= Jekyll::Cache.new("RemoteInclude") + end + def initialize(tag_name, remote_include, tokens) super @remote_include = remote_include end + # Returns the plugin's config or an empty hash if not set + def config + @config ||= @site.config["remote_include"] || {} + end + def open(url) - Net::HTTP.get(URI.parse(url.strip)).force_encoding 'utf-8' + cache.getset(url) do + Net::HTTP.get(URI.parse(url.strip)).force_encoding 'utf-8' + end end def render(context) @@ -33,8 +44,6 @@ def render(context) else begin_match = raw.match?(begin_token) ? "True" : "False" end_match = raw.match?(end_token) ? "True" : "False" - # if the file doesn't have the begin and end token just output the entire file - output = raw @logger.warn("Remote file fragment doesn't contain begin and end token.") @logger.warn("Page: " << context.environments.first["page"]["path"]) @logger.warn("Url: " << url) @@ -42,7 +51,15 @@ def render(context) @logger.warn("Begin token present? " << begin_match) @logger.warn("End token: " << end_token) @logger.warn("End token present? " << end_match) - raise ArgumentError, "Remote fragment include error in " << context.environments.first["page"]["path"] + if config["on_token_error"] == "fail" || !config["on_token_error"] + # Fail the build by default if there are token errors + raise ArgumentError, "Remote fragment include error in " << context.environments.first["page"]["path"] + elsif config["on_token_error"] == "full" + # if the file doesn't have the begin and end token just output the entire file + output = raw + elsif config["on_token_error"] == "none" + output = "" + end end else output = open(url)