module
Amber::Controller::Helpers::TextHelpers
Direct including types
Defined in:
amber/controller/helpers/text_helpers.crInstance Method Summary
-
#escape_html(text : String) : String
Escapes HTML entities in the given text.
-
#highlight(text : String, phrase : String, tag : String = "mark") : String
Wraps occurrences of a phrase in the text with an HTML tag.
-
#pluralize(count : Int32, singular : String, plural : String | Nil = nil) : String
Returns the singular or plural form of a word based on count.
-
#simple_format(text : String) : String
Converts newlines into
<br />tags and wraps paragraphs in<p>tags. -
#strip_tags(html : String) : String
Strips all HTML tags from the input string.
-
#truncate(text : String, length : Int32 = 30, omission : String = "...") : String
Truncates text to a given length and appends an omission string.
-
#word_wrap(text : String, line_width : Int32 = 80) : String
Wraps text at the specified line width.
Instance Method Detail
Escapes HTML entities in the given text.
escape_html("<script>alert('xss')</script>")
# => "<script>alert('xss')</script>"
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"
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"
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>"
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"
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"
Wraps text at the specified line width.
word_wrap("A very long sentence", line_width: 10)