Module: Governor::Formatters
- Defined in:
- lib/governor/formatters.rb
Overview
heavily based on Feather
Class Method Summary collapse
-
.available_formatters ⇒ Object
Returns an array of available formatters that have been registered.
-
.default_formatter ⇒ Object
Returns a default formatter used for replacing line breaks within text This is the only formatter included within Governor.
-
.format_article(article) ⇒ Object
Performs the relevant formatting for the article, and returns the formatted article content.
-
.format_text(formatter, text) ⇒ Object
Performs the requested formatting, returning the formatted text.
-
.register_formatter(name, &block) ⇒ Object
Registers a block to format article content.
Class Method Details
.available_formatters ⇒ Object
Returns an array of available formatters that have been registered
15 16 17 18 |
# File 'lib/governor/formatters.rb', line 15 def available_formatters @formatters ||= {'default' => default_formatter} return @formatters end |
.default_formatter ⇒ Object
Returns a default formatter used for replacing line breaks within text This is the only formatter included within Governor
23 24 25 26 27 |
# File 'lib/governor/formatters.rb', line 23 def default_formatter Proc.new do |text| text.to_s.gsub("\r\n", "\n").gsub("\n", '<br>') end end |
.format_article(article) ⇒ Object
Performs the relevant formatting for the article, and returns the formatted article content
31 32 33 |
# File 'lib/governor/formatters.rb', line 31 def format_article(article) format_text(article.format, article.post) end |
.format_text(formatter, text) ⇒ Object
Performs the requested formatting, returning the formatted text
37 38 39 40 |
# File 'lib/governor/formatters.rb', line 37 def format_text(formatter, text) formatter = 'default' unless available_formatters.include?(formatter) @formatters[formatter].call(text).html_safe end |
.register_formatter(name, &block) ⇒ Object
Registers a block to format article content
7 8 9 10 11 |
# File 'lib/governor/formatters.rb', line 7 def register_formatter(name, &block) @formatters ||= {'default' => default_formatter} raise "Formatter `#{name}` already registered!" unless @formatters[name].nil? @formatters[name] = block end |