Published 2020-10-03.
Last modified 2023-11-19.
Time to read: 2 minutes.
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:
group :jekyll_plugins do gem 'jekyll_from_to_until' end
And then execute:
$ bundle
from
If the regex does not match the entire string is returned. All of these examples perform identically.
{{ 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.
{{ 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.
{{ 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.
{{ 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.
Matching Special Characters
Special characters can be specified as HTML entities.
For example, }
can be specified as }
or }
.
{{ 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.