Module: Lyp::Transform

Defined in:
lib/lyp/transform.rb

Constant Summary collapse

R =
Lyp::DependencyResolver

Class Method Summary collapse

Class Method Details

.flatten(path, ctx = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lyp/transform.rb', line 7

def flatten(path, ctx = {})
  ctx[path] = true

  dir = File.dirname(path)
  src = IO.read(path)

  src.gsub(R::DEP_RE) do
    inc_path = File.expand_path($2, dir)
    case $1
    when R::INCLUDE, R::PINCLUDE
      "\n%%% #{inc_path}\n#{flatten(inc_path, ctx)}\n"
    when R::PINCLUDE_ONCE
      if ctx[inc_path]
        ""
      else
        "\n%%% #{inc_path}\n#{flatten(inc_path, ctx)}\n"
      end
    else
      $~
    end
  end
end