Sha256: 268b4cdae8d440979b2d1ff42f9d58c6e9c0c6c9679097fa93e80cf9ca7731e4

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'pathname'
require 'redcarpet'
require 'active_support/concern'
require 'active_support/core_ext/object/blank'

module Dionysus
  module Redcarpet
    module Includes
      extend ActiveSupport::Concern

      included do
        if method_defined?(:preprocess)
          alias_method_chain :preprocess, :includes
        else
          alias_method :preprocess, :preprocess_with_includes
        end
      end

      def preprocess_with_includes(full_document)
        lines = full_document.split($/)
        lines.each_with_index do |ln, i|
          if m = ln.match(/\A\s*<<<\s*(\S+)\s*\Z/)
            path = Pathname.new(m[1])
            if path.file?
              newlines = path.readlines
              newlines.collect! {|ln| ln.chomp}
              newlines.shift while(newlines.first.blank?)
              newlines.pop while(newlines.last.blank?)
              lines[i] = newlines
            else
              warn "[WARNING] Cannot find path %s to include"%[path.to_s]
            end
          end
        end
        lines.flatten.join("\n")
      end
    end
  end
end

Redcarpet::Render::HTML.send(:include, Dionysus::Redcarpet::Includes)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dionysus-2.0.0 lib/dionysus/redcarpet/includes.rb