Mike Slinn

jekyll_from_to_until

Published 2020-10-03. Last modified 2023-11-19.
Time to read: 2 minutes.

This page is part of the jekyll_plugins collection.

These filters all return portions of a multiline string. They are all defined in the same plugin. A regular expression is used to specify the match; the simplest regular expression is a string.

  • from – returns the portion beginning with the line that satisfies a regular expression to the end of the multiline string.
  • to – returns the portion from the first line to the line that satisfies a regular expression, including the matched line.
  • until – returns the portion from the first line to the line that satisfies a regular expression, excluding the matched line.

Important: the name of each of these filters must be followed by a colon (:). If you fail to do that an error will be generated and the Jekyll site building process will halt. The error message looks something like this:

Liquid Warning: Liquid syntax error (line xxx): Expected end_of_string but found string in "{{ lines | from 'blah' | xml_escape }}" in /some_directory/some_files.html Liquid Exception: Liquid error (line 285): wrong number of arguments (given 1, expected 2) in /some_directory/some_file.html Error: Liquid error (line 285): wrong number of arguments (given 1, expected 2).

Installation

Add the following highlighted line to your Jekyll project's Gemfile, within the jekyll_plugins group:

Gemfile
group :jekyll_plugins do 
  gem 'jekyll_from_to_until'
end 

And then execute:

Shell
$ bundle

from

If the regex does not match the entire string is returned. All of these examples perform identically.

HTML or markdown
{{ sourceOfLines | from: 'regex' }}
{{ sourceOfLines | from: "regex" }}
{{ sourceOfLines | from: regex }}

to

If the regex does not match the entire string is returned. All of these examples perform identically.

HTML or markdown
{{ sourceOfLines | to: 'regex' }}
{{ sourceOfLines | to: "regex" }}
{{ sourceOfLines | to: regex }}

until

If the regex does not match the entire string is returned. All of these examples perform identically.

HTML or markdown
{{ sourceOfLines | until: 'regex' }}
{{ sourceOfLines | until: "regex" }}
{{ sourceOfLines | until: regex }}

From the string "2" until the string "4"

These examples return the lines of the file until a line with the string `"3"` is found, excluding the matched line. The only difference between the examples is the delimiter around the regular expression.

HTML or markdown
{{ lines | from: '2' | until: '4' }}
{{ lines | from: "2" | until: "4" }}
{{ lines | from: 2 | until: 4 }}

More Complex Regular Expressions

The `from`, `to` and `until` filters can all accept regular expressions. The regular expression matches lines that have either the string `sun` or `cloud` at the beginning of the line.

HTML or markdown

Matching Special Characters

Special characters can be specified as HTML entities. For example, } can be specified as } or }.

HTML or markdown
{{ css | from: '.error' | to: '}' | strip }}

demo/special.html demonstrates this.

Calling From Other Ruby Programs

The methods that implement these filters can be used in other Ruby programs. For an example of how to do this, please refer to the from, to and until option handling in jekyll_flexible_include.

* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.