Class: Eggshell::Bundles::Basic::TableBlock

Inherits:
Object
  • Object
show all
Includes:
Eggshell::BlockHandler, Eggshell::BlockHandler::BlockParams, Eggshell::BlockHandler::HtmlUtils, FormatHandler::Utils
Defined in:
lib/eggshell/bundles/basics.rb

Constant Summary collapse

DELIM1 =
'|'
DELIM2 =
'|>'
HEADER =
'/'
SPECIAL_DEF =
/^!(\w+):(.+$)/
CELL_ATTR_START =
'!@'
CELL_ATTR_IDX =
1
CELL_ATTR_END =
'@!'
T_ROW =
't.row'

Constants included from Eggshell::BlockHandler::HtmlUtils

Eggshell::BlockHandler::HtmlUtils::HASH_HTML_ESCAPE

Constants included from Eggshell::BlockHandler::BlockParams

Eggshell::BlockHandler::BlockParams::ATTRIBUTES, Eggshell::BlockHandler::BlockParams::CLASS, Eggshell::BlockHandler::BlockParams::ID, Eggshell::BlockHandler::BlockParams::KEYS, Eggshell::BlockHandler::BlockParams::STYLE

Constants included from Eggshell::BlockHandler

Eggshell::BlockHandler::COLLECT, Eggshell::BlockHandler::COLLECT_RAW, Eggshell::BlockHandler::DONE, Eggshell::BlockHandler::RETRY

Instance Method Summary collapse

Methods included from FormatHandler::Utils

#parse_args

Methods included from Eggshell::BlockHandler::HtmlUtils

#attrib_string, #create_tag, #css_string, #html_escape

Methods included from Eggshell::BlockHandler::BlockParams

#get_block_params, #set_block_params

Methods included from Eggshell::BlockHandler

#current_type, #reset, #set_processor

Methods included from Eggshell::BaseHandler

#set_processor

Constructor Details

#initializeTableBlock

Returns a new instance of TableBlock.



150
151
152
# File 'lib/eggshell/bundles/basics.rb', line 150

def initialize
	@block_types = ['table']
end

Instance Method Details

#can_handle(line) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/eggshell/bundles/basics.rb', line 164

def can_handle(line)
	if !@block_type
		if line.match(/^(table[(.]?|\||\|>|\/)/) || line.match(SPECIAL_DEF)
			@block_type = 'table'
			return BH::COLLECT
		end
	end
	return BH::RETRY
end

#continue_with(line) ⇒ Object



174
175
176
177
178
179
# File 'lib/eggshell/bundles/basics.rb', line 174

def continue_with(line)
	if line.match(/^(\||\|>|\/)/) || line.match(SPECIAL_DEF)
		return BH::COLLECT
	end
	return BH::RETRY
end

#fmt_cell(val, header = false, colnum = 0) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/eggshell/bundles/basics.rb', line 278

def fmt_cell(val, header = false, colnum = 0)
	tag = header ? 'th' : 'td'
	buff = []
	attribs = ''

	if val[0] == '\\'
		val = val[1..val.length]
	elsif val[0..CELL_ATTR_IDX] == CELL_ATTR_START
		rt = val.index(CELL_ATTR_END)
		if rt
			attribs = val[CELL_ATTR_IDX+1...rt]
			val = val[rt+CELL_ATTR_END.length..val.length]
		end
	end

	# inject column position via class
	olen = attribs.length
	match = attribs.match(/class=(['"])([^'"]*)(['"])/)
	if !match
		attribs += " class='td-col-#{colnum}'"
	else
		attribs = attribs.gsub(match[0], "class='#{match[2]} td-col-#{colnum}'")
	end

	buff << "<#{tag} #{attribs}>"
	cclass = 
	if val[0] == '\\'
		val = val[1..val.length]
	end

	buff << @eggshell.expand_formatting(val)
	buff << "</#{tag}>"
	return buff.join('')
end

#process(type, args, lines, out, call_depth = 0) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
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
# File 'lib/eggshell/bundles/basics.rb', line 182

def process(type, args, lines, out, call_depth = 0)
	args = [] if !args
	bp = get_block_params(type, args[0])
	row_classes = bp['row.classes']
	row_classes = ['odd', 'even'] if !row_classes.is_a?(Array)
	
	o_out = out
	out = []

	@eggshell.vars[T_ROW] = 0
	out << create_tag('table', bp)
	out << "%caption%\n%colgroup%"
	caption = {:text => '', :atts => {}}
	colgroup = []

	cols = []
	rows = 0
	rc = 0
	data_started = false

	lines.each do |line_obj|
		ccount = 0
		line = line_obj.line
		special = line.match(SPECIAL_DEF)
		if special
			type = special[1]
			atts = special[2]
			if type == 'caption'
				text, atts = atts.split('|', 2)
				caption[:text] = text
				caption[:attributes] = parse_args(atts, true)[0]
			else
				atts = parse_args(atts, true)[0]
				puts atts.inspect
				colgroup << create_tag('col', attrib_string(atts), false)
			end
		elsif line[0] == '/' && rc == 0
			cols = line[1..line.length].split('|')
			out << "<thead><tr class='#{bp['head.class']}'>"
			cols.each do |col|
				out << "\t#{fmt_cell(col, true, ccount)}"
				ccount += 1
			end
			out << '</tr></thead>'
		elsif line[0] == '/'
			# implies footer
			out << '</tbody>' if rc > 0
			cols = line[1..line.length].split('|')
			out << "<tfoot><tr class='#{bp['foot.class']}'>"
			cols.each do |col|
				out << "\t#{fmt_cell(col, true, ccount)}"
				ccount += 1
			end
			out << '</tr></tfoot>'
			break
		elsif line[0] == DELIM1 || line[0..1] == DELIM2
			out << '<tbody>' if rc == 0
			idx = 1
			sep = /(?<!\\)\|/
			if line[1] == '>'
				idx = 2
				sep = /(?<!\\)\|\>/
			end
			cols = line[idx..line.length].split(sep)
			@eggshell.vars[T_ROW] = rc
			rclass = row_classes[rc % row_classes.length]
			out << "<tr class='tr-row-#{rc} #{rclass}'>"
			cols.each do |col|
				out << "\t#{fmt_cell(col, false, ccount)}"
				ccount += 1
			end
			out << '</tr>'
			rc += 1
		else
			cols = line[1..line.length].split('|') if line[0] == '\\'
		end
		rows += 1
	end

	out << "</table>"

	if caption[:text] != ''
		out[1].gsub!('%caption%', create_tag('caption', attrib_string(caption[:attributes]), true, caption[:text]))
	else
		out[1].gsub!("%caption%\n", '')
	end

	if colgroup.length > 0
		out[1].gsub!('%colgroup%', "<colgroup>\n\t#{colgroup.join("\n\t")}\n</colgroup>")
	else
		out[1].gsub("%colgroup%\n", '')
	end
	
	o_out << out.join("\n")
end