Class: Compile

Inherits:
Object
  • Object
show all
Defined in:
lib/aml/compile.rb

Constant Summary collapse

@@structure =
[]
@@selfClosing =
[]

Instance Method Summary collapse

Constructor Details

#initialize(parse, argument, definition, error) ⇒ Compile

Returns a new instance of Compile.



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

def initialize(parse,argument,definition,error)
	@@selfClosing = argument.read('selfClosing').split(',')
	error.compile(parse,definition,error)
	if(error.count == 0)
		prepare_line_variable(parse.file[:line],definition)
		lines = []
		parse.file[:line].each do |line|
			lines << post_prepare_line_variable(line)
		end

		parse.file[:line] = lines

		prepare_mixin_structure(parse.file[:line],definition)
		prepare_partial_structure(parse.file[:line],definition)
		prepare_method_structure(parse.file[:line],definition)
		
		structure = prepare_structure(parse.file[:line])
		structure.each do |group|
			recursive_close(group,0,definition)
		end
		make = Make.new(@@structure)
	end
end

Instance Method Details

#hash_to_attribute_build(hash, base = "", definition, line) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/aml/compile.rb', line 260

def hash_to_attribute_build(hash,base="",definition,line)
	hash = hash.sort_by{|key, value| key}
	string = ""
	hash.each do |key, value|
		if(value.is_a?(Hash))
			value.sort_by{|key, value| key}
			string << "#{hash_to_attribute_build(value,"#{base}#{key}-",definition,line)} "
		else
			#if key == :id
			#	regex = /\@\(((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)\)/
			#	value = value.to_s.gsub(regex){definition.variable($2,line[:number],$1.to_s.length > 0 ? $1 : false).to_s} if value.to_s.length > 0
			#end
			string << "#{base}#{key}=\"#{value}\" "
		end
	end
	string.strip
end

#line_attribute_replace(line, default_attributes, mixin_attributes) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/aml/compile.rb', line 148

def line_attribute_replace(line,default_attributes,mixin_attributes)
	regex = /@\(\:(?<name>[\w|\-]+)\)/
	mixin_attributes.merge!(default_attributes){|key, mixin, default| mixin}
	if mixin_attributes.length > 0
		%w"text value".each do |key|
			line[key.to_sym] = line[key.to_sym].to_s.gsub(regex){mixin_attributes[$1.to_sym]} if line.has_key?(key.to_sym)
		end
		if(line.has_key?(:attributes))
			attributes = line[:attributes].clone
			attributes.each do |k,v|
				attributes[k] = v.to_s.gsub(regex){mixin_attributes[$1.to_sym]}
			end
			line[:attributes] = attributes
		end
	end
	line
end

#line_variable_replace(line, definition) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/aml/compile.rb', line 131

def line_variable_replace(line,definition)
	regex = /\@\(((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)\)/
	%w"text value".each do |key|
		key = key.to_sym
		if line.has_key?(key)
			line[key] = line[key].to_s.gsub(regex){definition.variable($2,line[:number],$1.to_s.length > 0 ? $1 : false).to_s} if line[key].to_s.length > 0
		end
	end
	if line.has_key?(:attributes) and line[:attributes].count > 0
		line[:attributes].each do |k,v|
			if v.is_a?(Hash) == false
				line[:attributes][k] = line[:attributes][k].to_s.gsub(regex){definition.variable($2,line[:number],$1.to_s.length > 0 ? $1 : false).to_s} if v.to_s.length > 0
			end
		end
	end
end

#post_prepare_line_variable(line) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/aml/compile.rb', line 94

def post_prepare_line_variable(line)
	types	=	{
		:method			=>	LineType.new(/^(\s{1,})?::((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)(\{(?<attributes>.+)\})?/),
		:mixin			=>	LineType.new(/^(\s{1,})?%\(((?<bundle>[\w|\-]+)\.)?(?<name>[^~][\w|\-]+)\)(\{(?<attributes>.+)\})?[^\{]?/),
		:partial		=>	LineType.new(/^(\s{1,})?%\(\~((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)\)(\{(?<attributes>.+)\}[^\{]?)?$/),
		:tag			=>	LineType.new(/^(\s{1,})?(?<!%)%(?<close>\/{0,2})?(?<name>[\w|\-]+)(\#(?<id_first>[\w|\-]+))?(?<class>(\.[\w|\-]+)?{1,})?(\#(?<id_last>[\w|\-]+))?(\{(?<attributes>.+)\})?(?<text>.+)?/)
	}
	if line[:type] == :string
		types.each do |type, line_type|
			if match = line_type.match?(line[:value])
				struct = Hash[match.names.zip(match.captures)]
				struct = Hash[struct.map{|(k,v)| [k.to_sym,v]}]
				struct[:index] = line[:index]
				struct[:number] = line[:number]
				struct[:type] = type
				match.names.each do |name|
					struct[name.to_sym] = line_type.send(name.to_s, match)
				end
				if struct[:name].to_s[0,1] == '.'
					#set undefined bundle
					struct[:bundle] = 'core'
					struct[:name] = struct[:name][1,struct[:name].length] 
				end
				struct[:bundle] = false if struct[:bundle].to_s.length == 0
				#set undefined method bundle
				struct[:bundle] = 'core' if struct[:type] == :method and !struct[:bundle]
				struct[:id] = struct[:id_first].to_s.length > 0 ? struct[:id_first] : struct[:id_last]
				struct.delete(:id_first)
				struct.delete(:id_last)
				line = struct
				break
			end
		end
	end
	line
end

#prepare_line_variable(lines, definition) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/aml/compile.rb', line 75

def prepare_line_variable(lines,definition)
	regex = /\@\(((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)\)/
	if lines.kind_of?(Array)
		lines.each do |line|
			line = line_variable_replace(line,definition)
		end
	else
		lines = line_variable_replace(lines,definition)
	end
	check = 0
	if lines.kind_of?(Array)
		check_text = lines.select{|k| k[:text].to_s.match(regex)}
		check_value = lines.select{|k| k[:value].to_s.match(regex)}
		check = check_text.count+check_value.count
	end
	prepare_line_variable(lines,definition) if check > 0
	lines
end

#prepare_method_structure(lines, definition) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/aml/compile.rb', line 30

def prepare_method_structure(lines,definition)
	#Placeholder
	lines.each_with_index do |line,index|
		if line[:type] == :method
			line[:type] = :string
			line[:value] = "<aml bundle=\"#{line[:bundle]}\" method=\"#{line[:name]}\" />"
		end
	end
	lines
end

#prepare_mixin_structure(lines, definition) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/aml/compile.rb', line 166

def prepare_mixin_structure(lines,definition)
	lines.each_with_index do |line,index|
		if line[:type] == :mixin
			lines.delete_at index
			mixin = definition.mixin(line[:name],line[:bundle])
			if(mixin.is_a?(Hash))
				mixin = mixin.clone
				mixin[:structure].each_with_index do |mline,mindex|
					mixin_line = mline.clone
					mixin_line[:index] += line[:index]
					mixin_line[:number] = line[:number]
					line_attribute_replace(mixin_line,mixin[:attribute],line[:attributes])
					lines.insert(index+mindex,mixin_line)
				end
			end
			break
		end
	end
	prepare_mixin_structure(lines,definition) if(lines.select{|k|k[:type]==:mixin}).count > 0
	prepare_line_variable(lines,definition)
	lines
end

#prepare_partial_structure(lines, definition) ⇒ Object



41
42
43
44
45
46
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
73
# File 'lib/aml/compile.rb', line 41

def prepare_partial_structure(lines,definition)
	definition_partial = definition.variables.clone
	bundle = nil
	lines.each_with_index do |line,index|
		if line[:type] == :partial
			lines.delete_at index
			bundle = line[:bundle]
			directory = 'partial'
			directory = File.join(line[:bundle],directory) if line[:bundle]
			directory = File.join(AbstractMarkupLanguage::Base.basePath, directory)
			variable = {}
			definition.variables[line[:bundle]].each do |var,val|
				value = val.count > 1 ? val.to_a.last[1] : val[1]
				value = line[:attributes][var.to_sym] if line[:attributes].has_key?(var.to_sym)
				variable[var] = {1 => value} if value != nil
			end if definition.variables[line[:bundle]] != nil
			partial = Parse.new(File.join(directory),"#{line[:name]}.aml",true, line[:bundle])
			partial.file[:line].each_with_index do |pline,pindex|
				pline[:index] += line[:index]
				pline[:number] = line[:number]
				lines.insert(index+pindex,pline)
			end
			definition_partial[line[:bundle]] = definition_partial[line[:bundle]].merge(variable) if  definition_partial[line[:bundle]] != nil
			vars = DefinitionVariable.new(definition_partial)
			prepare_line_variable(lines,vars)
			break
		end
	end
	prepare_partial_structure(lines,definition) if(lines.select{|k|k[:type]==:partial}).count > 0
	prepare_line_variable(lines,definition)
	prepare_mixin_structure(lines,definition)
	lines
end

#prepare_structure(struct, index = 0, pstruct = {:line=>false,:children=>[]}) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/aml/compile.rb', line 189

def prepare_structure(struct,index=0,pstruct={:line=>false,:children=>[]})
	parent_tags = struct.each_index.select{|i| struct[i][:index] == index}.compact
	parent_struct = []
	parent_tags.each_with_index do |struct_index,index|
		last_struct_index = parent_tags.count > index+1 ? parent_tags[index+1]-1 : parent_tags[index]
		last_struct_index = struct.count if(parent_tags.count == index+1)
		parent_struct << struct[struct_index..last_struct_index]
	end
	parent_struct.each do |parent_structure|
		index_struct = {}
		index_struct[:line] = parent_structure[0]
		c = prepare_structure(parent_structure,index+1)
		index_struct[:children] = c if c.count > 0
		pstruct[:children] << index_struct 
	end
	pstruct[:children]
end

#recursive_close(struct, index = 0, definition) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/aml/compile.rb', line 207

def recursive_close(struct,index=0,definition)
	next_index	=	struct.key?(:line) ? index+1 : index
	tab_index	=	"\t" * index
	opening_tag_attributes = ""
	opening_tag = ""
	closing_tag = ""
	
	#STRING
	if(struct[:line][:type]==:string)
		opening_tag = struct[:line][:value]
	end

	#TAG
	if(struct[:line][:type]==:tag)

		struct[:line][:close] = 'self' if @@selfClosing.include? struct[:line][:name]

		opening_tag_attributes = tag_line_attributes(struct[:line],"",definition)
		opening_tag	=	"<#{struct[:line][:name]}#{opening_tag_attributes}>"
		closing_tag =	"</#{struct[:line][:name]}>"
		if struct.key?(:line)
			if struct[:line][:close] == 'self'
				opening_tag	=	"<#{struct[:line][:name]}#{opening_tag_attributes} />"
				closing_tag	=	""
			end
			if struct[:line][:close] == 'none'
				closing_tag	=	""
			end
		end
	end
	tag_text = struct[:line][:text]
	if struct.key?(:children)
		tag_text = "\r\n#{tab_index}\t#{tag_text}" if tag_text.to_s.length > 0
		@@structure << "#{tab_index}#{opening_tag}#{tag_text}" if struct.key?(:line)
		struct[:children].each do |struct_children|
			recursive_close(struct_children,next_index,definition)
		end
		@@structure << "#{tab_index}#{closing_tag}" if struct.key?(:line)
	else
		@@structure << "#{tab_index}#{opening_tag}#{tag_text}#{closing_tag}"
	end

end

#tag_line_attributes(line, base = "", definition) ⇒ Object



251
252
253
254
255
256
257
258
# File 'lib/aml/compile.rb', line 251

def tag_line_attributes(line,base="",definition)
	if line[:type] == :tag
		line[:attributes][:id] = line[:id] if line[:id].to_s.length > 0 and line[:attributes][:id].to_s.length == 0
		line[:attributes][:class] = line[:class] if line[:class].to_s.length > 0 and line[:attributes][:class].to_s.length == 0
		attributes = hash_to_attribute_build(line[:attributes],"",definition,line)
		attributes = " #{attributes}" if(attributes.length > 0)
	end
end