Class: Livetext::Formatter
- Defined in:
- lib/livetext/formatter.rb,
lib/livetext/formatter_component.rb
Overview
Formatter - Centralized text formatting for Livetext
Instance Method Summary collapse
-
#bold(text) ⇒ Object
Convenience methods for common formatting patterns.
- #code(text) ⇒ Object
- #escape_html(text) ⇒ Object
- #format(text) ⇒ Object
- #format_line(line) ⇒ Object
- #format_multiple(lines) ⇒ Object
-
#initialize(parent) ⇒ Formatter
constructor
A new instance of Formatter.
- #italic(text) ⇒ Object
- #link(text, url) ⇒ Object
- #strike(text) ⇒ Object
Constructor Details
#initialize(parent) ⇒ Formatter
Returns a new instance of Formatter.
3 4 5 |
# File 'lib/livetext/formatter.rb', line 3 def initialize(parent) @parent = parent end |
Instance Method Details
#bold(text) ⇒ Object
Convenience methods for common formatting patterns
36 37 38 |
# File 'lib/livetext/formatter.rb', line 36 def bold(text) "<b>#{text}</b>" end |
#code(text) ⇒ Object
44 45 46 |
# File 'lib/livetext/formatter.rb', line 44 def code(text) "<tt>#{text}</tt>" end |
#escape_html(text) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/livetext/formatter.rb', line 56 def escape_html(text) text.gsub(/[&<>"']/) do |char| case char when '&' then '&' when '<' then '<' when '>' then '>' when '"' then '"' when "'" then ''' else char end end end |
#format(text) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/livetext/formatter.rb', line 7 def format(text) return "" if text.nil? || text.empty? text = text.chomp # First, mark escaped characters so they won't be processed as formatting text = mark_escaped_characters(text) # Process all marker types in sequence text = handle_double_markers(text) text = handle_bracketed_markers(text) text = handle_single_markers(text) text = handle_underscore_markers(text) text = handle_backtick_markers(text) text = handle_tilde_markers(text) text = unmark_escaped_characters(text) text end |
#format_line(line) ⇒ Object
26 27 28 29 |
# File 'lib/livetext/formatter.rb', line 26 def format_line(line) return "" if line.nil? format(line.chomp) end |
#format_multiple(lines) ⇒ Object
31 32 33 |
# File 'lib/livetext/formatter.rb', line 31 def format_multiple(lines) lines.map { |line| format_line(line) } end |
#italic(text) ⇒ Object
40 41 42 |
# File 'lib/livetext/formatter.rb', line 40 def italic(text) "<i>#{text}</i>" end |
#link(text, url) ⇒ Object
52 53 54 |
# File 'lib/livetext/formatter.rb', line 52 def link(text, url) "<a href='#{url}'>#{text}</a>" end |
#strike(text) ⇒ Object
48 49 50 |
# File 'lib/livetext/formatter.rb', line 48 def strike(text) "<strike>#{text}</strike>" end |