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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ Dynamic parameters:
{% include_file "{{ $templatingAllowedHere }}/Dockerfile" snippet="{{ $hereToo }}" %}
```

## Plugin options in `_config.yml`

Default options:
```yml
jekyll_include_plugin:
snippet_prefix: '...'
```

### `snippet_prefix`
Type: `string` Default: `...`

Prepends the prefix at the end of included snippet to differentiate whole file includes vs partial file includes (snippet)

## Installation

Add this line to your application's Gemfile:
Expand Down
11 changes: 10 additions & 1 deletion lib/jekyll_include_plugin/jekyll_include_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ class IncludeFileTag < Liquid::Tag
def initialize(tag_name, raw_markup, tokens)
super
@raw_markup = raw_markup
@config = {}
@params = {}
end

def render(context)
read_config(context)
parse_params(context)

file_contents = get_raw_file_contents(context)

if @params["snippet"]
file_contents = pick_snippet(file_contents, @params["snippet"])
file_contents = pick_snippet(file_contents, @config['snippet_prefix'], @params["snippet"])
else
file_contents = remove_all_snippets(file_contents)
end
Expand All @@ -34,6 +36,13 @@ def render(context)

private

def read_config(context)
site = context.registers[:site]
plugin_config = site.config["jekyll_include_plugin"] || {}

@config["snippet_prefix"] = plugin_config['snippet_prefix'] || '...'
end

def parse_params(context)
rendered_markup = Liquid::Template
.parse(@raw_markup)
Expand Down
6 changes: 4 additions & 2 deletions lib/jekyll_include_plugin/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def abort(msg)
module TextUtils
include Utils

def pick_snippet(text, snippet_name)
def pick_snippet(text, snippet_prefix, snippet_name)
snippet_content = ""
snippet_start_found = false
snippet_end_found = false
Expand All @@ -42,8 +42,10 @@ def pick_snippet(text, snippet_name)
abort("End of the snippet '#{snippet_name}' has not been found.") unless snippet_end_found
abort("Snippet '#{snippet_name}' appears to be empty. Fix and retry.") if snippet_content.empty?

return snippet_content if snippet_prefix.empty?

first_line_indent = %r!^\s*!.match(snippet_content)[0]
return "#{first_line_indent}...\n#{snippet_content}"
return "#{first_line_indent}#{snippet_prefix}\n#{snippet_content}"
end

def remove_all_snippets(text)
Expand Down
Loading