Class: Eggshell::Bundles::Basic::ListBlocks

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

Constant Summary collapse

START_LIST =
/^(ol|ul|dl)[(.]/
START_LIST_SHORT =
/^\s*([#>-])/

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 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

#initializeListBlocks

Returns a new instance of ListBlocks.



319
320
321
# File 'lib/eggshell/bundles/basics.rb', line 319

def initialize
  @block_types = ['ul', 'ol', 'dl']
end

Instance Method Details

#can_handle(line) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/eggshell/bundles/basics.rb', line 326

def can_handle(line)
  match = START_LIST.match(line)
  if match
    @block_type = match[1]
    return BH::COLLECT
  end

  match = START_LIST_SHORT.match(line)
  if match
    if match[1] == '>'
      @block_type = 'dl'
    else
      @block_type = match[1] == '-' ? 'ul' : 'ol'
    end
    return BH::COLLECT
  end
  
  return BH::RETRY
end

#continue_with(line) ⇒ Object



346
347
348
349
350
351
352
# File 'lib/eggshell/bundles/basics.rb', line 346

def continue_with(line)
  if line == nil || line == "" || !START_LIST_SHORT.match(line)
    return BH::RETRY
  else
    return BH::COLLECT
  end
end

#equal?(handler, type) ⇒ Boolean

Returns:

  • (Boolean)


410
411
412
# File 'lib/eggshell/bundles/basics.rb', line 410

def equal?(handler, type)
  self.class == handler.class && (type == 'ol' || type == 'ul')
end

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



355
356
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
404
405
406
407
408
# File 'lib/eggshell/bundles/basics.rb', line 355

def process(type, args, lines, out, call_depth = 0)
  if type == 'dl'
    # @todo
  else
    order_stack = []
    otype_stack = []
    last = nil
    first_type = nil

    if lines[0] && !lines[0].line.match(/[-#]/)
      line = lines.shift
    end

    lines.each do |line_obj|
      line = line_obj.line
      indent = line_obj.indent_lvl
      ltype = line[0] == '-' ? 'ul' : 'ol'
      line = line[1..line.length].strip

      if order_stack.length == 0
        # use the given type to start; infer for sub-lists
        order_stack << create_tag(type, get_block_params(type, args[0]))
        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}<#{ltype}>"
        otype_stack << ltype
      elsif indent < (otype_stack.length-1)
        count = otype_stack.length - 1
        while count > indent
          ltype = otype_stack.pop  
          order_stack << "#{"\t"*count}</#{ltype}>\n#{"\t"*(count-1)}</li>"
          count -= 1
        end
      end
      order_stack << "#{"\t"*indent}<li>#{line}</li>"
    end

    # close nested lists
    d = otype_stack.length
    c = 1
    otype_stack.reverse.each do |ltype|
      ident = d - c
      order_stack << "#{"\t" * ident}</#{ltype}>\n#{ident-1 >= 0 ? "\t"*(ident-1) : ''}#{c == d ? '' : "</li>"}"
      c += 1
    end
    out << @eggshell.expand_all(order_stack.join("\n"))
  end
end