New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Advanced tables (colspan / rowspan / multi-paragraph)? #87
Comments
#41 applies here From John Gruber himself
|
kramdown only supports simple tables with no block elements inside cells, no colspan/rowspan or anything else. Use an HTML table for more complex tables. |
I have made some modification to support col-span and row-span, which utilizes some cell text. I have no idea whether it is a good convention for kramdown, but it works for me, at least to some extent. I will share it later, and hope it helps. |
Interesting, thank you! |
@Tuckie to support col-span and row-span, revise # col span previous cell if cell is empty
# if want to keep an empty one, use non-white char like '-'
#
# following table contains two layer headers
# first header has two colspan with first empty
# second header has two cells and one colspan cell
# | | | Column 1 | |
# | a | b | haha | |
# |----
#
# row span cells with same text
# following table contains rowspan
# | :---: | -----
# | names | tom |
# | names | jerry |
# | film | cartoon |
#
# use option `table_rowspan` and `table_colspan` to control
# option values: :all, :first, nil
def convert_table(el, indent)
attr = el.attr.dup
format_as_indented_block_html(el.type, attr, inner(el, indent), indent)
end
def convert_tbody(el, indent)
span = @options[:table_rowspan]
if span == :all
col_rowspan_all el
elsif span == :first
col_rowspan_first el
end
span = @options[:table_colspan]
el.children.each{|row| row_colspan(row)} if span == :all
format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
end
def convert_thead(el, indent)
span = @options[:table_colspan]
if span == :all
el.children.each{|row| row_colspan(row)}
elsif span == :first
row = el.children.first
row_colspan row
end
format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
end
alias :convert_tfoot :convert_thead
def convert_tr(el, indent)
format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
end
def convert_td(el, indent)
return '' if el.attr[:skip]
res = inner(el, indent)
type = el.type
type = :th if type == :td and @stack[-2].type == :thead
attr = el.attr
alignment = @stack[-3].options[:alignment][@stack.last.children.index(el)]
# do not apply align style to th
if alignment != :default && type != :th
attr = el.attr.dup
attr['style'] = (attr.has_key?('style') ? "#{attr['style']}; ": '') << "text-align: #{alignment}"
end
format_as_block_html(type, attr, res.empty? ? entity_to_str(ENTITY_NBSP) : res, indent)
end
alias :convert_th :convert_td
def mark_skip_cell(cell)
cell.attr[:skip] = true
end
def mark_span_cell(cell, span, size)
cell.attr[span] = size if size > 1
end
def row_colspan(row)
span = :colspan
cells = row.children
groups = cells.chunk{|c| c.children.empty?}.to_a
total = groups.size
start = 0
if groups.first[0]
start = 1
count = groups.first[1].size
cell = groups.first[1][0]
mark_span_cell cell, span, count
(1...count).each{|i| mark_skip_cell groups.first[1][i]}
end
(start...total).each do |i|
if groups[i][0]
groups[i][1].each{|cell| mark_skip_cell cell}
end
next if i == total - 1
next unless groups[i+1][0]
cell = groups[i][1][-1]
cell.attr[span] = groups[i+1][1].size + 1
end
end
# perform col_rowspan on one row
def col_rowspan_row(cells)
span = :rowspan
cells.chunk{|c| c.inspect}.each do |r,cs|
first = cs.first
total = cs.size
unless total < 2
mark_span_cell first, span, total
cs[1..-1].each{|c| mark_skip_cell c}
end
end
end
# only for first col
def col_rowspan_first(body)
rows = body.children
cells = rows.map{|r| r.children.first}
col_rowspan_row cells
end
# rowspan for all cols
def col_rowspan_all(body)
rows = body.children
cells = rows.map(&:children)
cols = cells.transpose
cols.each do |cells|
col_rowspan_row cells
end
end |
A detailed tutorial about HTML Tables...HTML Table help nick |
Sorry, but he didn't ask about HTML tables. He even said that he was looking for a non-html solution. |
As we known, kramdown only supports simple tables with no block elements inside cells, no colspan/rowspan or anything else. We have to use an HTML table for more complex tables. To support col-span and row-span even more table features that using an awesome non-html solution, I think the below Jekyll plugin could help you:
https://github.com/jeffreytse/jekyll-spaceship For now, these extended features are provided:
|
Is colspan or rowspan supported?
Is there any way to make a multi-paragraph cell?
Or any ideas as to how we could go about adding it? (cell-level IAL?)
Tables are one of my remaining headaches preventing me from going going full markdown (no html).
The text was updated successfully, but these errors were encountered: