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
29
30
31
32
33
34
# 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)
		
		prepare_string_line_merge(parse.file[:line])

		structure = prepare_structure(parse.file[:line])

		recursive_merge_lines(structure)

		structure.each do |group|
			recursive_close(group,0,definition,0)
		end

		make = Make.new(@@structure)
	end
end

Instance Method Details

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



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/aml/compile.rb', line 308

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



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

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/aml/compile.rb', line 173

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/aml/compile.rb', line 136

def post_prepare_line_variable(line)
	types	=	{
		:method			=>	LineType.new(/^(\s{1,})?::((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)(\{(?<attributes>.+)\})?(?<value>.+)?/),
		: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|\-]+)(?<tab_reset>\*{1,})?(\#(?<id_first>(\@\(([\w|-]+\.)?)?[\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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/aml/compile.rb', line 117

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



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/aml/compile.rb', line 71

def prepare_method_structure(lines,definition)
	lines.each_with_index do |line,index|
		if line[:type] == :method
			line[:type] = :string
			bundle = line[:bundle]
			bundle = "Core" if bundle == 'core'
			line[:value] = AbstractMarkupLanguage::Base.const_get(bundle).method(line[:name]).call(line[:index],line[:attributes]) + line[:value]
		end
	end
	lines
end

#prepare_mixin_structure(lines, definition) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/aml/compile.rb', line 208

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



83
84
85
86
87
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
114
115
# File 'lib/aml/compile.rb', line 83

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_string_line_merge(lines) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/aml/compile.rb', line 60

def prepare_string_line_merge(lines)
	regex = /\%\+\+$/
	lines.each_with_index do |line,index|
		if(line[:type] == :string)
			line[:merge] = line[:value].to_s.match(regex) != nil
			line[:value] = line[:value].to_s.gsub(regex,'') if line[:merge]
		end
	end
	lines
end

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



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/aml/compile.rb', line 231

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, index_reset) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/aml/compile.rb', line 249

def recursive_close(struct,index=0,definition,index_reset)
	next_index	=	struct.key?(:line) ? index+1 : index
	tab_index	=	"\t" * (index-index_reset)
	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)
		new_line = "\r\n"
		
		#testing * aka tab_reset
		index_reset = struct[:line][:index]+1 if struct[:line][:tab_reset].to_s.length > 0

		tag_text = "#{new_line}#{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,index_reset)
		end
		@@structure << "#{tab_index}#{closing_tag}" if struct.key?(:line)
	else
		@@structure << "#{tab_index}#{opening_tag}#{tag_text}#{closing_tag}"
	end

end

#recursive_merge_lines(struct, count = 0) ⇒ Object



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/aml/compile.rb', line 36

def recursive_merge_lines(struct,count=0)
    struct.each_with_index do |group,struct_index|
        group.each_with_index do |line,gindex|
            if line.first == :line
                if line.last[:type] == :string and line.last[:merge]
                    next_line = struct[struct_index+1].first
                    if next_line.first == :line and next_line.last[:type] == :string
                        line.last[:value] += next_line.last[:value]
                        line.last[:merge] = next_line.last[:merge]
                        line.last[:merge] = struct_index+1 == struct.count ? false : line.last[:merge]
                        struct.delete_at struct_index+1
                    else
                       line.last[:merge] = false 
                    end
                    count +=1 if line.last[:merge]
                end
            else
                recursive_merge_lines(line.last)
            end
        end
    end
    recursive_merge_lines(struct) if count > 0
end

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



299
300
301
302
303
304
305
306
# File 'lib/aml/compile.rb', line 299

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