lib/overapp/template_file.rb in overapp-0.4.2 vs lib/overapp/template_file.rb in overapp-0.5.0
- old
+ new
@@ -1,108 +1,44 @@
module Overapp
class TemplateFile
include FromHash
attr_accessor :path, :full_body
- def split_note_and_body
- res = []
- remaining_body = full_body
- while remaining_body
- if remaining_body =~ /^<over(?:lay|app)>(.+)<\/over(?:lay|app)>(.*)(<over(?:lay|app)>.+)/m
- note = $1
- rest = $2
- remaining_body = $3
- res << {:note => note, :body => rest}
- elsif remaining_body =~ /^<over(?:lay|app)>(.+)<\/over(?:lay|app)>(.*)/m
- note = $1
- rest = $2
- remaining_body = nil
- res << {:note => note, :body => rest}
- else
- res << {:note => nil, :body => remaining_body}
- remaining_body = nil
- end
- end
- res
- rescue => exp
- puts "Error in split_note_and_body #{path}"
- raise exp
+ fattr(:params_obj) do
+ Params.new(:full_body => full_body)
end
+ def has_note?
+ params_obj.has_note?
+ end
- def note_params_single(one)
- res = {}
- note = one[:note]
- res[:body] = one[:body]
-
- if note
- lines = note.split("\n").select { |x| x.present? }
- if lines.size == 1 && !(lines.first =~ /action:/)
- res[:action] = lines.first.strip
- else
- lines.each do |line|
- parts = line.split(":").select { |x| x.present? }
- if parts.size > 2
- parts = [parts[0],parts[1..-1].join(":")]
- end
- parts = parts.map { |x| x.strip }
- raise "bad #{path} #{parts.inspect}" unless parts.size == 2
- res[parts[0].to_sym] = parts[1]
- end
- end
- else
- # do nothing
+ def templated_body(params)
+ body = params[:body]
+ if params[:template] == 'erb'
+ v = VarObj.new(:file => self)
+ body = v.render(body)
end
- res
+ body
end
- def note_params
- split_parts.map do |one|
- note_params_single(one)
- end
+ def body_after_action(base_body,params)
+ body = templated_body(params)
+ BodyMod.transform(base_body,body,params)
end
-
def apply_body_to(base_body)
- note_params.each do |params|
- body = params[:body]
- base_body = if params[:action].blank?
- body
- elsif params[:action] == '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]} to insert #{body} 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]} to replace with #{body} in \n#{base_body}"
- end
- end
- elsif params[:action] == 'delete'
- :delete
+ params_obj.inject(base_body) do |new_base_body,params|
+ if params[:action].blank?
+ templated_body(params)
+ elsif params[:action]
+ body_after_action(new_base_body,params)
else
- raise "bad #{params.inspect}"
+ raise "bad"
end
end
- base_body
end
- fattr(:split_parts) { split_note_and_body }
- def has_note?
- split_parts.any? { |x| x[:note].present? }
- end
-
def body
full_body
end
def combined(base)
@@ -117,9 +53,14 @@
def write_to!(dir)
raise "bad path" if path.blank?
d = File.dirname("#{dir}/#{path}")
`mkdir -p #{d}`
File.create "#{dir}/#{path}",body
+ end
+
+ fattr(:vars) { {} }
+ def parsed
+ combined(OpenStruct.new(:full_body => ""))
end
end
class BasicFile < TemplateFile
def body
\ No newline at end of file