Class: Eggshell::Bundles::Basics::SectionBlocks

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

Constant Summary collapse

TOC_TEMPLATE =
{
	:default => "<div class='toc-h$level'><a href='\#$id'>$title</a></div>"
}

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, #reset

Methods included from ProcessHandler

#process

Instance Method Details

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



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/eggshell/bundles/basics-old.rb', line 405

def collect(line, buffer, indents = '', indent_level = 0, line_count = -1)
	if line == '' || !line
		buffer << @proc.fmt_line(@toc_template[:start]) if @toc_template[:start]
		@header_list.each do |entry|
			if entry == 'section'
				buffer << @proc.fmt_line(@toc_template[:section]) if @toc_template[:section]
			elsif entry == 'section_end'
				buffer << @proc.fmt_line(@toc_template[:section_end]) if @toc_template[:section_end]
			elsif entry.is_a?(Hash)
				tpl = @toc_template[entry[:tag]] || @toc_template[:default]
				buffer << @proc.fmt_line(
					tpl.gsub('$id', entry[:id]).gsub('$title', entry[:title]).gsub('$level', entry[:level].to_s)
				)
			end
		end
		buffer << @proc.fmt_line(@toc_template[:end]) if @toc_template[:end]
		return DONE
	else
		key, val = line.split(':', 2)
		@toc_template[key.to_sym] = val
		return COLLECT
	end
end

#set_processor(proc) ⇒ Object



350
351
352
353
354
355
# File 'lib/eggshell/bundles/basics-old.rb', line 350

def set_processor(proc)
	@proc = proc
	@proc.register_block(self, *%w(h1 h2 h3 h4 h5 h6 hr section end-section toc))
	@header_list = []
	@header_idx = {}
end

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



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/eggshell/bundles/basics-old.rb', line 357

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

	if name[0] == 'h'
		if name == 'hr'
			buffer << "<hr />"
		else
			lvl = name[1].to_i
			attrs = bp['attributes'] || {}
			attrs['class'] = bp['class'] || ''
			attrs['style'] = bp['style'] || ''

			id = bp['id'] || line.downcase.strip.gsub(/[^a-z0-9_-]+/, '-')
			lid = id
			i = 1
			while @header_idx[lid] != nil
				lid = "#{id}-#{i}"
				i += 1
			end
			id = lid
			attrs['id'] = id
			title = @proc.fmt_line(line)

			buffer << "#{create_tag(name, attrs)}#{title}</#{name}>"

			@header_list << {:level => lvl, :id => lid, :title => title, :tag => name}
			@header_idx[lid] = @header_list.length - 1
		end
		return DONE
	elsif name == 'section'
		attrs = bp['attributes'] || {}
		attrs['class'] = bp['class'] || ''
		attrs['style'] = bp['style'] || ''
		attrs['id'] = bp['id'] || ''
		buffer << create_tag('section', attrs)
		@header_list << name
		return DONE
	elsif name == 'end-section'
		buffer << '</section>'
		@header_list << name
		return DONE
	elsif name == 'toc'
		@toc_template = TOC_TEMPLATE.clone
		return COLLECT
	end
end