module Amber::Controller::Helpers::TextHelpers

Direct including types

Defined in:

amber/controller/helpers/text_helpers.cr

Instance Method Summary

Instance Method Detail

def escape_html(text : String) : String #

Escapes HTML entities in the given text.

escape_html("<script>alert('xss')</script>")
# => "&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;"

[View source]
def highlight(text : String, phrase : String, tag : String = "mark") : String #

Wraps occurrences of a phrase in the text with an HTML tag. Both text and phrase are HTML-escaped before processing.

highlight("Hello World", "World")             # => "Hello <mark>World</mark>"
highlight("Hello World", "world")             # => "Hello <mark>World</mark>"
highlight("You found it", "found", tag: "em") # => "You <em>found</em> it"

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

Returns the singular or plural form of a word based on count.

pluralize(1, "person")           # => "1 person"
pluralize(2, "person")           # => "2 people"
pluralize(2, "person", "people") # => "2 people"
pluralize(0, "item")             # => "0 items"

[View source]
def simple_format(text : String) : String #

Converts newlines into <br /> tags and wraps paragraphs in <p> tags. Text is HTML-escaped before processing.

simple_format("Hello\nWorld")   # => "<p>Hello<br />World</p>"
simple_format("Para1\n\nPara2") # => "<p>Para1</p><p>Para2</p>"

[View source]
def strip_tags(html : String) : String #

Strips all HTML tags from the input string. Handles nested tags, malformed HTML, and self-closing tags.

strip_tags("<p>Hello <b>World</b></p>")     # => "Hello World"
strip_tags("<script>alert('xss')</script>") # => "alert('xss')"
strip_tags("No tags here")                  # => "No tags here"

[View source]
def truncate(text : String, length : Int32 = 30, omission : String = "...") : String #

Truncates text to a given length and appends an omission string.

truncate("Hello World", length: 8)                 # => "Hello..."
truncate("Hello World", length: 8, omission: ">>") # => "Hello >>"
truncate("Hi", length: 10)                         # => "Hi"

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

Wraps text at the specified line width.

word_wrap("A very long sentence", line_width: 10)

[View source]