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

Methods included from FormatHandler::Utils

#parse_args

Methods included from FormatHandler

#set_processor

Constructor Details

#initializeBasicFormatHandlers

Returns a new instance of BasicFormatHandlers.



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/eggshell/bundles/basics.rb', line 560

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



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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/eggshell/bundles/basics.rb', line 579

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