Skip to content
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

Closed
Tuckie opened this issue Oct 16, 2013 · 8 comments
Closed

Advanced tables (colspan / rowspan / multi-paragraph)? #87

Tuckie opened this issue Oct 16, 2013 · 8 comments
Assignees

Comments

@Tuckie
Copy link

Tuckie commented Oct 16, 2013

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

@ghost
Copy link

ghost commented Oct 18, 2013

#41 applies here

From John Gruber himself

Markdown is not a replacement for HTML, or even close to it. Its syntax is
very small, corresponding only to a very small subset of HTML tags. The idea
is not to create a syntax that makes it easier to insert HTML tags. In my
opinion, HTML tags are already easy to insert. The idea for Markdown is to
make it easy to read, write, and edit prose.

@ghost ghost assigned gettalong Oct 26, 2013
@gettalong
Copy link
Owner

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.

@unibr
Copy link
Contributor

unibr commented Jan 17, 2014

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.

@Tuckie
Copy link
Author

Tuckie commented Jan 17, 2014

Interesting, thank you!

@unibr
Copy link
Contributor

unibr commented Feb 2, 2014

@Tuckie to support col-span and row-span, revise converter/html.rb (it should work. it is revised from workable code for kramdown-1.1.0, but itself not tested.):

# 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

@nickwelhar
Copy link

A detailed tutorial about HTML Tables...HTML Table help

nick

@dolfandringa
Copy link

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.

@jeffreytse
Copy link

jeffreytse commented Apr 25, 2020

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:

jekyll-spaceship - 🚀 A Jekyll plugin to provide powerful supports for table, mathjax, mermaid, plantuml, emoji, youtube, etc.

https://github.com/jeffreytse/jekyll-spaceship

For now, these extended features are provided:

  • Cells spanning multiple columns
  • Cells spanning multiple rows
  • Cells text align separately
  • Table header not required
  • Grouped table header rows or data rows
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants