Sha256: 9520161218ffa20041756012715de046c006cf0361e82f8ab90acbe5e79a92f7

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

require "active_support/concern"
module Himekaminize
  module Filterable
    extend ::ActiveSupport::Concern

    def initialize(default_context = {})
      @default_context = default_context
    end

    def call(markdown, context = {})
      @markdown = markdown
      to_lines
      @result ||= Hash.new
      context = default_context.merge(context)

      @result = filters.inject(context: context, output: lines) do |output, filter|
        filter.call(output)
      end

      to_s
      @result
    end

    def result
      @result
    end

    private

    def filters
      @filters ||= self.class.filter_classes.map(&:new)
    end

    def markdown
      @markdown
    end

    def context
      @context
    end

    def default_context
      @default_context
    end

    def lines
      @lines
    end

    def to_lines
      @lines = markdown.lines("\r\n")
    end

    def to_s
      @result[:markdown] = @result[:output].map { |line|
        if line.is_a?(String)
          line
        else
          line.to_s
        end
      }.join('')
    end

    module ClassMethods
      def filter_classes
        raise ::NotImplementedError
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
himekaminize-0.1.0 lib/himekaminize/filterable.rb
himekaminize-0.0.10 lib/himekaminize/filterable.rb
himekaminize-0.0.9 lib/himekaminize/filterable.rb
himekaminize-0.0.8 lib/himekaminize/filterable.rb