Class: Deplate::SkeletonExpander
Overview
Instance Method Summary
collapse
class_attribute, class_attributes, class_attributes=, class_attributes_ensure, class_meta_attributes, inherited, method_missing, respond_to?
Constructor Details
Returns a new instance of SkeletonExpander.
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/deplate/skeletons.rb', line 11
def initialize(deplate)
@deplate = deplate
@formatter = deplate.formatter
@formatter_name = @formatter.class.myname
@max_iteration = 5
@skeletons = deplate.options.skeletons
@skeletons_tmpl = {}
@skel_ids = {}
@skel_names = @skeletons.collect {|n| Regexp.escape(n)}.join('|')
@rx = Deplate::Rx.builder("\\{\\{((#@skel_names)\\b(?>\\\\\\{|\\\\\\}|\\\\\\\\|[^{}]+?|{#})*)\\}\\}|^[[:blank:]]+",
Regexp::MULTILINE, 5, false)
srand(Time.now.to_i)
end
|
Instance Method Details
#expand(string, source = nil, iterations = @max_iteration) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/deplate/skeletons.rb', line 47
def expand(string, source=nil, iterations=@max_iteration)
if string.empty?
return string
end
expanded = false
indent = ''
string.gsub!(@rx) do |s|
text = $1
if text
expansion = expand_skeleton(text, indent, source)
if expansion
expanded ||= true
expansion
else
text
end
else
indent = s
end
end
if expanded and iterations > 0
return expand(string, source, iterations - 1)
else
return string
end
end
|
#expand_skeleton(string, indent = '', source = nil) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/deplate/skeletons.rb', line 88
def expand_skeleton(string, indent='', source=nil)
name, args = split_name_args(string)
if @skeletons.include?(name)
special = "skeleton_#{name}"
text = if respond_to?(special)
send(special, args)
else
skeleton = require_skeleton(name, source)
if skeleton
tmpl = Deplate::Template.new(:template => skeleton,
:source => source)
accum = Deplate::Define.let_variables(@deplate, args) do
tmpl.fill_in(@deplate, :source => source)
end
accum.flatten!
accum.join("\n")
else
nil
end
end
if text
return @formatter.indent_text(text, :hanging => true, :indenttail => indent)
end
end
return nil
end
|
#load_skeleton(name) ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/deplate/skeletons.rb', line 37
def load_skeleton(name)
tmpl = @deplate.find_in_lib(name, :pwd => true)
if tmpl and File.exist?(tmpl)
t = File.open(tmpl) {|io| io.read}
return t
end
return nil
end
|
#require_skeleton(name, source = nil) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/deplate/skeletons.rb', line 25
def require_skeleton(name, source=nil)
skels = @skeletons_tmpl[@formatter_name] ||= {}
t = skels[name] ||= load_skeleton(name)
return t if t
Deplate::Core.log(['Unknown skeleton', name], :error, source)
end
|
#skeleton_id(args) ⇒ Object
115
116
117
118
119
120
121
122
|
# File 'lib/deplate/skeletons.rb', line 115
def skeleton_id(args)
name = args['name'] || args['id'] || args['@body']
sep = '_' * (4 + rand(4))
rid = '%x' % rand.to_s[2..-1].to_i
@skel_ids[name] ||= [sep, name, rid, sep].join('_')
return @skel_ids[name]
end
|
#split_name_args(string) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/deplate/skeletons.rb', line 74
def split_name_args(string)
m = Regexp.new("^(#@skel_names)\s*").match(string)
name = m[1]
args = m.post_match.strip
if args
args = Deplate::Core.remove_backslashes(args)
args, body = @deplate.input.parse_args(args)
args['@body'] = body
else
args = {}
end
return name, args
end
|