Sha256: 186ff63ce20c8c957bb1152aaa9bff725b04a6b927a31438f635b94eee59008b

Contents?: true

Size: 1.35 KB

Versions: 14

Compression:

Stored size: 1.35 KB

Contents

module Governor
  # heavily based on Feather[https://github.com/mleung/feather]
  module Formatters
    class << self
      ##
      # Registers a block to format article content
      def register_formatter(name, &block)
        @formatters ||= {'default' => default_formatter}
        raise "Formatter `#{name}` already registered!" unless @formatters[name].nil?
        @formatters[name] = block
      end

      ##
      # Returns an array of available formatters that have been registered
      def available_formatters
        @formatters ||= {'default' => default_formatter}
        return @formatters
      end

      ##
      # Returns a default formatter used for replacing line breaks within text
      # This is the only formatter included within Governor
      def default_formatter
        Proc.new do |text|
          text.to_s.gsub("\r\n", "\n").gsub("\n", '<br>')
        end
      end

      ##
      # Performs the relevant formatting for the article, and returns the formatted article content
      def format_article(article)
        format_text(article.format, article.post)
      end

      ##
      # Performs the requested formatting, returning the formatted text
      def format_text(formatter, text)
        formatter = 'default' unless available_formatters.include?(formatter)
        @formatters[formatter].call(text).html_safe
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
governor-0.5.7 lib/governor/formatters.rb
governor-0.5.6 lib/governor/formatters.rb
governor-0.5.5 lib/governor/formatters.rb
governor-0.5.4 lib/governor/formatters.rb
governor-0.5.3 lib/governor/formatters.rb
governor-0.5.2 lib/governor/formatters.rb
governor-0.5.1 lib/governor/formatters.rb
governor-0.5.0 lib/governor/formatters.rb
governor-0.4.0 lib/governor/formatters.rb
governor-0.3.1 lib/governor/formatters.rb
governor-0.3.0 lib/governor/formatters.rb
governor-0.2.3 lib/governor/formatters.rb
governor-0.2.2 lib/governor/formatters.rb
governor-0.2.1 lib/governor/formatters.rb