Class: Eggshell::Bundles::Basics::BasicBlocks

Inherits:
Object
  • Object
show all
Includes:
Eggshell::BlockHandler
Defined in:
lib/eggshell/bundles/basics-old.rb

Overview

‘table` block parameters:

  • ‘row.classes`: defaults to `[’odd’, ‘even’]‘. The number of elements represents the number of cycles.

Constant Summary collapse

CELL_ATTR_START =
'!'
CELL_ATTR_END =
'<!'

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 Eggshell::BlockHandler

#can_handle, #continue_with, #current_type, #equal?, #reset

Methods included from ProcessHandler

#process

Instance Method Details

#collect(line, buff, indents = '', indent_level = 0, line_count = -1)) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/eggshell/bundles/basics-old.rb', line 63

def collect(line, buff, indents = '', indent_level = 0, line_count = -1)
	line = '' if !line
	ret = COLLECT
	if @type == :list
		ret = do_list(line, buff, indents, indent_level)
	elsif @type == :table
		ret = do_table(line, buff, indents, indent_level)
	elsif @type == :dt
		ret = do_dt(line, buff, indents, indent_level)
	else
		ret = do_default(line, buff, indents, indent_level)
	end
	return ret
end

#do_default(line, buff, indents = '', indent_level = 0) ⇒ Object



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
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/eggshell/bundles/basics-old.rb', line 229

def do_default(line, buff, indents = '', indent_level = 0)
	ret = COLLECT
	blank = false

	raw = @type == 'raw'
	pre = @type == 'pre'
	nofmt = raw

	# we need to group indented lines as part of block, especially if it's otherwise empty
	if raw || pre || @type == 'bq'
		raw = true

		if indent_level > 0
			line = indents + line
			if pre
				line = "\n#{line.chomp}"
			end
		else
			blank = line == ''
			if pre
				line = "\n#{line}"
			end
		end
	else
		blank = line == ''
	end

	if blank
		@lines.delete('')
		start_tag = ''
		end_tag = ''
		content = ''
		if @type != 'raw'
			bp = @block_params[@type]
			attrs = bp['attributes'] || {}
			attrs['class'] = bp['class'] || ''
			attrs['style'] = bp['style'] || ''
			attrs['id'] = bp['id'] || ''
			@type = 'blockquote' if @type == 'bq'
			# @todo get attributes from block param
			start_tag = create_tag(@type, attrs)
			end_tag = "</#{@type}>"
			join = @type == 'pre' ? "" : "<br />\n"

			content = "#{start_tag}#{@lines.join(join)}#{end_tag}"
		else
			content = @lines.join("\n")
		end

		buff << content
		@lines = []
		ret = DONE
	else
		line = !nofmt ? @proc.fmt_line(line) : @proc.expand_expr(line)
		@lines << line
		ret = raw ? COLLECT_RAW : COLLECT
	end
	ret
end

#do_dt(line, buff, indents = '', indent_level = 0) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/eggshell/bundles/basics-old.rb', line 209

def do_dt(line, buff, indents = '', indent_level = 0)
	ret = COLLECT
	if line == '' || line[0] != '>'
		ret = DONE
		ret = RETRY if line[0] != '>'

		buff << "<dl class='#{@proc.vars['dd.class']}'>"
		@lines.each do |line|
			key = line[0]
			val = line[1]
			buff << "<dt class='#{@proc.vars['dt.class']}'>#{key}</dt><dd class='#{@proc.vars['dd.class']}'>#{val}</dd>"
		end
		buff << "</dl>"
	else
		@lines << line[1..-1].split('::', 2)
		ret = COLLECT
	end
	ret
end

#do_list(line, buff, indents = '', indent_level = 0) ⇒ Object



78
79
80
81
82
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
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/eggshell/bundles/basics-old.rb', line 78

def do_list(line, buff, indents = '', indent_level = 0)
	ret = COLLECT
	lstrip = line.lstrip
	if line && (lstrip[0] == '#' || lstrip[0] == '-')
		@lines << [line[1..-1].strip, indent_level+@indent_start, lstrip[0] == '#' ? 'ol' : 'ul']
	else
		# if non-empty line, reprocess this line but also process buffer
		ret = (line && line != '') ? RETRY : DONE
		order_stack = []
		otype_stack = []
		last = nil
		@lines.each do |tuple|
			line, indent, type = tuple
			# normalize indent
			indent -= @indent_start
			if order_stack.length == 0
				order_stack << "<#{type}>"
				otype_stack << type
			# @todo make sure that previous item was a list
			# remove closing li to enclose sublist
			elsif indent > (otype_stack.length-1) && order_stack.length > 0
				last = order_stack[-1]
				last = last[0...last.length-5]
				order_stack[-1] = last

				order_stack << "#{"\t"*indent}<#{type}>"
				otype_stack << type
			elsif indent < (otype_stack.length-1)
				count = otype_stack.length - 1 - indent
				while count > 0
					ltype = otype_stack.pop	
					order_stack << "#{"\t"*count}</#{ltype}>\n#{"\t"*(count-1)}</li>"
					count -= 1
				end
			end
			order_stack << "#{"\t"*indent}<li>#{@proc.fmt_line(line)}</li>"
		end

		# close nested lists
		d = otype_stack.length
		c = 1
		otype_stack.each do |type|
			ident = d - c
			order_stack << "#{"\t" * ident}</#{type}>#{c == d ? '' : "</li>"}"
			c += 1
		end
		buff << order_stack.join("\n")
	end
	ret
end

#do_table(line, buff, indents = '', indent_level = 0) ⇒ Object



129
130
131
132
133
134
135
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
172
173
174
175
176
177
178
179
180
181
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
# File 'lib/eggshell/bundles/basics-old.rb', line 129

def do_table(line, buff, indents = '', indent_level = 0)
	ret = COLLECT
	if line[0] == '|' || line[0] == '/'
		@lines << line
	else
		ret = (line[0] != '\\' && line != '') ? RETRY : DONE
		map = @block_params['table'] || {}
		tbl_class = map['class'] || ''
		tbl_style = map['style'] || ''
		tbl_attrib = ''
		if map['attribs'].is_a?(String)
			tbl_attrib = map['attribs']
		elsif map['attribs'].is_a?(Hash)
			map['attribs'].each do |key,val|
				tbl_attrib = "#{tbl_attrib} #{key}='#{val}'"
			end
		end
		row_classes = map['row.classes']
		row_classes = ['odd', 'even'] if !row_classes.is_a?(Array)

		@proc.vars['t.row'] = 0
		buff << "<table class='#{tbl_class}' style='#{tbl_style}'#{tbl_attrib}>"
		cols = []
		rows = 0
		rc = 0
		@lines.each do |line|
			ccount = 0
			if line[0] == '/' && rows == 0
				cols = line[1..line.length].split('|')
				buff << "<thead><tr class='#{map['head.class']}'>"
				cols.each do |col|
					buff << "\t#{fmt_cell(col, true, ccount)}"
					ccount += 1
				end
				buff << '</tr></thead>'
				buff << '<tbody>'
			elsif line[0] == '/'
				# implies footer
				cols = line[1..line.length].split('|')
				buff << "<tfoot><tr class='#{map['foot.class']}'>"
				cols.each do |col|
					buff << "\t#{fmt_cell(col, true, ccount)}"
					ccount += 1
				end
				buff << '</tr></tfoot>'
			elsif line[0] == '|' || line[0..1] == '|>'
				idx = 1
				sep = /(?<!\\)\|/
				if line[1] == '>'
					idx = 2
					sep = /(?<!\\)\|\>/
				end
				cols = line[idx..line.length].split(sep)
				@proc.vars['t.row'] = rc
				rclass = row_classes[rc % row_classes.length]
				buff << "<tr class='tr-row-#{rc} #{rclass}'>"
				cols.each do |col|
					buff << "\t#{fmt_cell(col, false, ccount)}"
					ccount += 1
				end
				buff << '</tr>'
				rc += 1
			else
				cols = line[1..line.length].split('|') if line[0] == '\\'
			end
			rows += 1
		end

		buff << '</tbody>'
		if cols.length > 0
			# @todo process footer
		end
		buff << "</table>"
		@proc.vars['table.class'] = ''
		@proc.vars['table.style'] = ''
		@proc.vars['table.attribs'] = ''
	end
	ret
end

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



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/eggshell/bundles/basics-old.rb', line 289

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_START
		rt = val.index(CELL_ATTR_END)
		if rt
			attribs = val[1...rt]
			val = val[rt+CELL_ATTR_END.length..val.length]
		end
	end

	# inject column position via class
	olen = attribs.length
	attribs = attribs.gsub(/class=(['"])/, 'class=$1' + "td-col-#{colnum}")
	if olen == attribs.length
		attribs += " class='td-col-#{colnum}'"
	end

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

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

#set_processor(proc) ⇒ Object



24
25
26
27
# File 'lib/eggshell/bundles/basics-old.rb', line 24

def set_processor(proc)
	@proc = proc
	@proc.register_block(self, *%w(table pre p bq div raw ol ul # - / | >))
end

#start(name, line, buffer, indents = '', indent_level = 0, line_count = -1)) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/eggshell/bundles/basics-old.rb', line 29

def start(name, line, buffer, indents = '', indent_level = 0, line_count = -1)
	set_block_params(name)
	bp = @block_params[name]

	if name == '#' || name == '-' || name == 'ol' || name == 'ul'
		@type = :list
		@lines = []
		@lines << [line, indent_level, name == '#' ? 'ol' : 'ul'] if name == '#' || name == '-'
		@indent_start = indent_level
		return COLLECT
	end

	if name == 'table' || name == '/' || name == '|'
		@type = :table
		@lines = []
		@lines << line if name != 'table'
		return COLLECT
	end

	if name == '>'
		@type = :dt
		@lines = [line.split('::', 2)]
		return COLLECT
	end

	# assume block text
	@type = name
	if line.index(name) == 0
		line = line.lstrip
	end
	@lines = [@type == 'raw' ? @proc.expand_expr(line) : @proc.fmt_line(line)]
	return @type == 'raw' ? COLLECT_RAW : COLLECT
end