module Motion::HTML::HTMLTextHelpers

Overview

These helper methods will write directly to the view.

Direct including types

Defined in:

motion/html/page_helpers/html_text_helpers.cr

Instance Method Summary

Instance Method Detail

def highlight(text : String, phrases : Array(String | Regex), highlighter : Proc | String = "<mark>\\1</mark>", escape : Bool = true) : Nil #

Wrap phrases to make them stand out

This will wrap all the phrases inside a piece of text specified by the phrases array. The default is to wrap each with the <mark> element. This can be customized with the highlighter argument.

Note: This method writes HTML directly to the page. It does not return a String

highlight("Crystal is type-safe and compiled.", phrases: ["type-safe", "compiled"])

outputs:

Crystal is <mark>type-safe</mark> and <mark>compiled</mark>.

With a custom highlighter

highlight(
  "You're such a nice and attractive person.",
  phrases: ["nice", "attractive"],
  highlighter: "<strong>\\1</strong>"
)

outputs:

You're such a <strong>nice</strong> and <strong>attractive</strong> person.

[View source]
def highlight(text : String, phrases : Array(String | Regex), escape : Bool = false, &block : String -> _) : Nil #

Highlight a single phrase

Exactly the same as the #highlight that takes multiple phrases, but with a singular phrase argument for readability.


[View source]
def highlight(text : String, phrase : String | Regex, highlighter : Proc | String = "<mark>\\1</mark>", escape : Bool = true) : Nil #

[View source]
def highlight(text : String, phrase : String | Regex, escape : Bool = true, &block : String -> _) : Nil #

[View source]
def simple_format(text : String, &block : String -> _) : Nil #

Wraps text in whatever you'd like based on line breaks

Note: This method writes HTML directly to the page. It does not return a String

simple_format("foo\n\nbar\n\nbaz") do |paragraph|
  text paragraph
  hr
end

outputs:

foo<hr>

bar<hr>

baz<hr>

[View source]
def simple_format(text : String, escape : Bool = true, **html_options) : Nil #

Wraps text in paragraphs based on line breaks

simple_format("foo\n\nbar\n\nbaz")

outputs:

<p>foo</p>

<p>bar</p>

<p>baz</p>

[View source]
def truncate(text : String, length : Int32 = 30, omission : String = "...", separator : String | Nil = nil, escape : Bool = true, blk : Nil | Proc = nil) : Nil #

Shortens text after a length point and inserts content afterward

Note: This method writes HTML directly to the page. It does not return a String.

This is ideal if you want an action associated with shortened text, like "Read more".

  • length (default: 30) will control the maximum length of the text, including the omission.
  • omission (default: ...) will insert itself at the end of the truncated text.
  • separator (default: nil) is where words are cut off. This is often overridden to break on word boundaries by setting the separator to a space " ". Keep in mind this, may cause your text to be truncated before your length value if the length - omission is before the separator.
  • escape (default: true) weather or not to HTML escape the truncated string.
  • blk (default: nil) A block to run after the text has been truncated. Often used to add an action to read more text, like a "Read more" link.
truncate("Four score and seven years ago", length: 20) do
  link "Read more", to: "#"
end

outputs:

"Four score and se...<a href="#">Read more</a>"

[View source]
def truncate(text : String, length : Int32 = 30, omission : String = "...", separator : String | Nil = nil, escape : Bool = true, &block : -> _) : Nil #

[View source]