module Lucky::TextHelpers

Overview

These helper methods will return a String.

Direct including types

Defined in:

lucky/page_helpers/text_helpers.cr

Instance Method Summary

Instance Method Detail

def current_cycle(name : String = "default") #

[View source]
def cycle(values : Array, name = "default") : String #

[View source]
def cycle(*values, name : String = "default") : String #

[View source]
def excerpt(text : String, phrase : Regex | String, separator : String = "", radius : Int32 = 100, omission : String = "...") : String #

Grab a window of longer string

You'll need to specify a phrase to center on, either a Regex or a String.

Optionally:

  • A radius (default: 100) which controls how many units out from the phrase on either side the excerpt will be.
  • A separator (default "") which controls what the radius will count. The unit by default is any character, which means the default is 100 character from the phrase in either direction. For example, an excerpt of # 10 words would use a radius of 10 and a separator of " ".
  • An omission string (default: "..."), which prepends and appends to the excerpt.
lyrics = "We represent the Lolly pop Guild, The Lolly pop Guild"
excerpt(text, phrase: "Guild", radius: 10)

outputs:

...Lolly pop Guild, The Loll...

[View source]
def pluralize(count : Int | String | Nil, singular : String, plural = nil) : String #

It pluralizes singular unless count is 1. You can specify the plural option to override the chosen plural word.


[View source]
def reset_cycle(name : String = "default") #

[View source]
def reset_cycles #

[View source]
def to_sentence(list : Enumerable, word_connector : String = ", ", two_word_connector : String = " and ", last_word_connector : String = ", and ") : String #

Creates a comma-separated sentence from the provided Enumerable list and appends it to the view.

Options:

The following options allow you to specify how the sentence is constructed:

  • word_connector - A string used to join the elements in lists containing three or more elements (Default is ", ")
  • two_word_connector - A string used to join the elements in lists containing exactly two elements (Default is " and ")
  • last_word_connector - A string used to join the last element in lists containing three or more elements (Default is ", and ")

Examples:

to_sentence([] of String)            # => ""
to_sentence([1])                     # => "1"
to_sentence(["one", "two"])          # => "one and two"
to_sentence({"one", "two", "three"}) # => "one, two, and three"

to_sentence(["one", "two", "three"], word_connector: " + ")
# => one + two, and three

to_sentence(Set{"a", "z"}, two_word_connector: " to ")
# => a to z

to_sentence(1..3, last_word_connector: ", or ")
# => 1, 2, or 3

NOTE By default #to_sentence will include a serial comma. This can be overridden like so:

to_sentence(["one", "two", "three"], last_word_connector: " and ")
# => one, two and three

[View source]
def truncate_text(text : String, length : Int32 = 30, omission : String = "...", separator : String? = nil) : String #

Shorten text after a length point.

Unlike truncate, this method can be used inside of other tags because it returns a String. See truncate method for argument documentation.

link "#" do
  text truncate_text("Four score and seven years ago", length: 27)
end

outputs:

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

[View source]
def word_wrap(text : String, line_width : Int32 = 80, break_sequence : String = "\n") : String #

[View source]