Module: Mastalk::Extensions

Included in:
Document
Defined in:
lib/mastalk/extensions.rb

Overview

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>

Constant Summary collapse

SNIPPETS_FOLDER =
File.join(File.dirname(__FILE__), 'snippets')
@@extensions =
[]

Instance Method Summary collapse

Instance Method Details

#args(content) ⇒ Object



38
39
40
# File 'lib/mastalk/extensions.rb', line 38

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

#extension(start, stop = nil, &block) ⇒ Object



15
16
17
18
19
20
# File 'lib/mastalk/extensions.rb', line 15

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

#extensionsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mastalk/extensions.rb', line 22

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|
      body_lines = body.strip.gsub(/(\n|\r)+/, "\n").split(/\n/)
      ERB.new(remove_syntax_from(content)).result(binding)
    end
  end

  @@extensions
end

#remove_syntax_from(content) ⇒ Object



42
43
44
# File 'lib/mastalk/extensions.rb', line 42

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