Sha256: 4ac9134d6d061b7b6f1a06fb8a1bc621226976fad0a32ca09f39653ce51b851d

Contents?: true

Size: 1.15 KB

Versions: 12

Compression:

Stored size: 1.15 KB

Contents

require 'erb'

module Mastalk
  # Extension DSL for adding new
  # mastalk snippets.
  #
  # Add new snippets in the form:
  # # start_tag, end_tag, new_line_ending
  #
  # <p>replacement html <%= body %></p>
  module Extensions
    SNIPPETS_FOLDER = File.join(File.dirname(__FILE__), 'snippets')
    @@extensions = []

    def extension(start, stop = nil, &block)
      re_start = Regexp.escape(start)
      re_stop = Regexp.escape(stop || start)
      regex = Regexp.new("#{re_start}(.*?)(#{re_stop}+)", Regexp::MULTILINE)
      @@extensions << [regex, block]
    end

    def extensions
      return @@extensions unless @@extensions.empty?

      Dir.foreach(SNIPPETS_FOLDER) do |file|
        next if file == '.' || file == '..'
        content = File.read(File.join(SNIPPETS_FOLDER, file))
        start, stop = args(content)
        extension(start, stop) do |body|
          ERB.new(remove_syntax_from(content)).result(binding)
        end
      end

      @@extensions
    end

    def args(content)
      content.match(/#(.*)\n\n/m).captures.first.split(',').map(&:strip)
    end

    def remove_syntax_from(content)
      content.split(/#(.*)\n\n/).last
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
mastalk-0.5.9 lib/mastalk/extensions.rb
mastalk-0.5.8 lib/mastalk/extensions.rb
mastalk-0.5.7 lib/mastalk/extensions.rb
mastalk-0.5.6 lib/mastalk/extensions.rb
mastalk-0.5.5 lib/mastalk/extensions.rb
mastalk-0.5.4 lib/mastalk/extensions.rb
mastalk-0.5.3 lib/mastalk/extensions.rb
mastalk-0.5.2 lib/mastalk/extensions.rb
mastalk-0.5.1 lib/mastalk/extensions.rb
mastalk-0.5.0 lib/mastalk/extensions.rb
mastalk-0.4.0 lib/mastalk/extensions.rb
mastalk-0.3.5 lib/mastalk/extensions.rb