Class: Eggshell::Bundles::Basic::BasicFormatHandlers

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

Constant Summary collapse

TAG_MAP =
{
	'[*' => 'b',
	'[**' => 'strong',
	'[/' => 'i',
	'[//' => 'em',
	'[__' => 'u',
	'[-' => 'strike',
	'[^' => 'sup',
	'[_' => 'sub',
	'[[' => 'span'
}.freeze
ESCAPE_MAP_HTML =
{
	'<' => '&lt;',
	'>' => '&gt;',
	'/<' => '&laquo;',
	'>/' => '&raquo;',
	'/\'' => '&lsquo;',
	'\'/' => '&rsquo;',
	'/"' => '&ldquo;',
	'"/' => '&rdquo;',
	'c' => '&copy;',
	'r' => '&reg;',
	'!' => '&iexcl;',
	'-' => '&ndash;',
	'--' => '&mdash;',
	'&' => '&amp;'
}.freeze

Constants included from Eggshell::BlockHandler::HtmlUtils

Eggshell::BlockHandler::HtmlUtils::HASH_HTML_ESCAPE

Instance Method Summary collapse

Methods included from Eggshell::BlockHandler::HtmlUtils

#attrib_string, #create_tag, #css_string, #html_escape

Methods included from FormatHandler::Utils

#parse_args

Methods included from FormatHandler

#set_processor

Constructor Details

#initializeBasicFormatHandlers

Returns a new instance of BasicFormatHandlers.



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/eggshell/bundles/basics.rb', line 539

def initialize
	@fmt_delimeters = [
		['[*', '*]'], # bold
		['[**', '**]'], # strong
		['[/', '/]'], # italic
		['[//', '//]'], # emphasis
		['[__', '__]'], # underline
		['[-', '-]'], # strike
		['[^', '^]'], # superscript
		['[_', '_]'], # subscript,
		['[[', ']]'], # span
		['[~', '~]'], # anchor
		['[!', '!]'], # image
		['%{', '}%', true], # entity expansion
		['`', '`', '%{'], # code, backtick
		['{{', '}}', '%{'] # code, normal
	]
end

Instance Method Details

#format(tag, str) ⇒ Object



558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/eggshell/bundles/basics.rb', line 558

def format(tag, str)
	if tag == '{{' || tag == '`'
		cls = tag == '{{' ? 'normal' : 'backtick'
		return "<code class='#{cls}'>#{str}</code>"
	elsif tag == '%{'
		# @todo find a way to expand char map and handle unmapped strings at runtime
		buff = ''
		str.split(' ').each do |part|
			c = ESCAPE_MAP_HTML[part]
			buff += c || part
		end
		return buff
	end
	
	st = tag[0..1]
	args = parse_args(str.strip)
	akey = BH::BlockParams::ATTRIBUTES
	atts = {akey => {}}
	atts[akey].update(args.pop)

	tagname = nil
	tagopen = true
	text = args[0]

	if tag == '[~'
		link, text = args
		link = '' if !link 
		text = '' if !text
		if text.strip == ''
			text = link
		end
		atts[akey]['href'] = link if link != ''
		tagname = 'a'
	elsif tag == '[!'
		link, alt = args
		link = '' if !link
		alt = '' if !alt

		atts[akey]['src'] = link if link != ''
		atts[akey]['alt'] = alt if alt != ''
		tagname = 'img'
		tagopen = false
	else
		tagname = TAG_MAP[tag]
	end

	return create_tag(tagname, atts, tagopen, text)
end