Class: Jekyll::IncludeSnippet::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/include_snippet/extractor.rb

Defined Under Namespace

Classes: Snippet

Constant Summary collapse

BEGIN_REGEX =
%r{
  (\s*)\#\s*      # Line must start with a hash (surrounding whitespace optional)
  begin-snippet:  # Magic string for beginning a snippet
  (.+)            # The remainder of the line is the snippet name
}x
END_REGEX =
%r{
  \s*\#\s*     # Line must start with a hash (surrounding whitespace optional)
  end-snippet  # Magic string for ending a snippet
}x

Instance Method Summary collapse

Instance Method Details

#call(source) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jekyll/include_snippet/extractor.rb', line 15

def call(source)
  everything = Snippet.new(name: 'everything', indent: 0)
  all_snippets = []
  active_snippets = []

  source.each_line do |line|
    case line
    when BEGIN_REGEX
      active_snippets << Snippet.new(name: $2.strip, indent: $1.length)
    when END_REGEX
      all_snippets << active_snippets.pop
    else
      (active_snippets + [everything]).each do |snippet|
        snippet.lines << line
      end
    end
  end

  (all_snippets + [everything])
    .map { |s| [s.name, s.dedented_text] }
    .to_h
end