Skip to content

Commit

Permalink
Allow whitespace markers for verbatim text: |<, |>, and |<> (#912)
Browse files Browse the repository at this point in the history
* Allow whitespace markers for verbatim text: ´|<`, `|>`, and `|<>`

* Fix README text

* Require a space after whitespace markers to handle a verbatim text starting with a tag.

* Handle indented verbatim text with whitespace markers

* Added case for `|><`
  • Loading branch information
UweKubosch authored Feb 28, 2023
1 parent 3b10346 commit e924243
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ You can also embed html in the text line
| <tr><td>#{a.name}</td><td>#{a.description}</td></tr>
~~~

#### Verbatim text with leading and/or treailing white space `|<` `|>` `|<>`

You can add white space around verbatim text in the same way as for `=` output:
~~~ slim
| This line will not have any extra white space.
| This line will have a leading space, but it is difficult to see.
|< This line will have a leading white space.
|> This line will have a trailing white space.
|<> This line will have both leading and trailing white space.
~~~

### Verbatim text with trailing white space `'`

The single quote tells Slim to copy the line (similar to `|`), but makes sure that a single trailing white space is appended.
Expand Down
8 changes: 5 additions & 3 deletions lib/slim/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@ def parse_line_indicators
when /\A\//
# Slim comment
parse_comment_block
when /\A([\|'])( ?)/
when /\A([\|'])([<>]{1,2}(?: |\z)| ?)/
# Found verbatim text block.
trailing_ws = $1 == "'"
@stacks.last << [:slim, :text, :verbatim, parse_text_block($', @indents.last + $2.size + 1)]
leading_ws = $2.include?('<'.freeze)
trailing_ws = ($1 == "'") || $2.include?('>'.freeze)
@stacks.last << [:static, ' '] if leading_ws
@stacks.last << [:slim, :text, :verbatim, parse_text_block($', @indents.last + $2.count(' ') + 1)]
@stacks.last << [:static, ' '] if trailing_ws
when /\A</
# Inline html
Expand Down
50 changes: 50 additions & 0 deletions test/literate/TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ renders as
Text block
~~~

You can add leading or trailing white space with the `<` and `>` markers:

~~~ slim
|< Text with leading whitespace.
| Text with leading whitespace.
|> Text with trailing whitespace.
|<> Text with both leading and trailing whitespace.
~~~

renders as

~~~ html
Text with leading whitespace. Text with leading whitespace.Text with trailing whitespace. Text with both leading and trailing whitespace.
~~~


Multiple lines can be indented beneath the first text line.

~~~ slim
Expand Down Expand Up @@ -77,6 +93,30 @@ Text
lines
~~~

~~~ slim
|><
Text
block
with
multiple
lines
~~~

renders as

~~~ html
Text
block

with

multiple
lines
~~~

You can nest text blocks beneath tags.

~~~ slim
Expand Down Expand Up @@ -104,6 +144,16 @@ renders as
<a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
~~~

~~~ slim
|<a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
~~~

renders as

~~~ html
<a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
~~~

### Text with trailing white space `'`

A text blocks with trailing white space starts with the `'` as line indicator.
Expand Down

0 comments on commit e924243

Please sign in to comment.