class Eggshell::Bundles::Basics::BasicBlocks
`table` block parameters:
-
`row.classes`: defaults to `['odd', 'even']`. The number of elements represents the number of cycles.
Constants
- CELL_ATTR_END
- CELL_ATTR_START
Public Instance Methods
collect(line, buff, indents = '', indent_level = 0, line_count = -1)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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