Module: FsTemplate::TemplateFile::NewWay

Included in:
FsTemplate::TemplateFile
Defined in:
lib/fs_template/template_file.rb

Instance Method Summary collapse

Instance Method Details

#apply_body_to_long(base_body) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fs_template/template_file.rb', line 33

def apply_body_to_long(base_body)
  params = note_params
  if note == 'append'
    base_body + body
  elsif params[:action] == 'insert' && params[:after]
    base_body.gsub(params[:after],"#{params[:after]}#{body}").tap do |subbed|
      if subbed == base_body
        raise "no change, couldn't find #{params[:after]} in \n#{base_body}"
      end
    end
  elsif params[:action] == 'insert' && params[:before]
    base_body.gsub(params[:before],"#{body}#{params[:before]}").tap do |subbed|
      if subbed == base_body
        raise "no change, couldn't find #{params[:before]} in \n#{base_body}"
      end
    end
  elsif params[:action] == 'replace' && params[:base]
    base_body.gsub(params[:base],body).tap do |subbed|
      if subbed == base_body
        raise "no change, couldn't find #{params[:base]} in \n#{base_body}"
      end
    end
  else
    raise "bad"
  end
end

#note_paramsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fs_template/template_file.rb', line 17

def note_params
  res = {}
  lines = note.split("\n").select { |x| x.present? }
  if lines.size == 1
    res[:action] = lines.first.strip
  else
    lines.each do |line|
      parts = line.split(":").map { |x| x.strip }.select { |x| x.present? }
      raise "bad #{path} #{parts.inspect}" unless parts.size == 2
      res[parts[0].to_sym] = parts[1]
    end
  end
  res
end

#split_note_and_body_longObject



7
8
9
10
11
12
13
14
15
# File 'lib/fs_template/template_file.rb', line 7

def split_note_and_body_long
  if full_body =~ /<overlay>(.+)<\/overlay>/m
    note = $1
    rest = full_body.gsub(/<overlay>.+<\/overlay>/m,"")
    {:note => note, :body => rest, :format => :long}
  else
    nil
  end
end