Class: Jekyll::IncludeSnippet::Extractor

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

Defined Under Namespace

Classes: Snippet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment_prefix:) ⇒ Extractor



6
7
8
# File 'lib/jekyll/include_snippet/extractor.rb', line 6

def initialize(comment_prefix:)
  @comment_prefix = comment_prefix
end

Instance Attribute Details

#comment_prefixObject (readonly)

Returns the value of attribute comment_prefix.



4
5
6
# File 'lib/jekyll/include_snippet/extractor.rb', line 4

def comment_prefix
  @comment_prefix
end

Instance Method Details

#call(source) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jekyll/include_snippet/extractor.rb', line 10

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

  source.each_line.each_with_index do |line, lineno|
    case line
    when begin_regex
      active_snippets << Snippet.new(name: $2.strip, indent: $1.length)
    when end_regex
      raise missing_begin_snippet(lineno) if active_snippets.empty?
      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