Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Multiline table functionality #93

Open
wants to merge 2 commits into from

2 participants

@cleverdripper

Adds functionality to handle markdown tables that have rows that span multiple lines of source.

This enables the source code to look considerably cleaner for table cells that have a lot of content. For example, instead of having

Column A  | Column B
----------|---------
row a     | Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
more a... | Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

You can do

Column A  | Column B
----------+----------
row a     | Lorem ipsum dolor sit amet, consectetur adipisicing elit,
          | sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
          | Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
etc       | nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
          | reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
          | pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
          | culpa qui officia deserunt mollit anim id est laborum.
----------+----------
more a... | Lorem ipsum dolor sit amet, consectetur adipisicing elit,
          | sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
          | Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
etc       | nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
          | reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
          | pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
          | culpa qui officia deserunt mollit anim id est laborum.

The only difference is that you use + in the underline for the column headers instead of a |, and when you want a new row you use ---+---. The two tables above will end up translated to the same HTML table.

This does not break existing Markdown features, and it stays consistent with Markdown's goal to be readable in plain-text form.

cleverdripper added some commits
@cleverdripper cleverdripper Added multiline table capability. bfacbe1
@cleverdripper cleverdripper Fixed a dumb mistake in the multiline table reader.
Where it never stops reading.

Also made the regexes for determining row separators more tolerant.
c00d745
@bhollis
Owner

Thanks @cleverdripper. If you don't mind, I'm going to park this pull request until I'm starting to pull together the 1.0 release, where I'll be considering new features and Markdown syntax extensions.

@cleverdripper

@bhollis No problem, thanks for considering. Great work on maruku.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 5, 2013
  1. @cleverdripper
  2. @cleverdripper

    Fixed a dumb mistake in the multiline table reader.

    cleverdripper authored
    Where it never stops reading.
    
    Also made the regexes for determining row separators more tolerant.
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 9 deletions.
  1. +4 −1 lib/maruku/input/mdline.rb
  2. +38 −8 lib/maruku/input/parse_block.rb
View
5 lib/maruku/input/mdline.rb
@@ -130,7 +130,10 @@ def line_md_type
Sep = /\s*(\:)?\s*-+\s*(\:)?\s*/
# | -------------:| ------------------------------ |
TableSeparator = /^(\|?#{Sep}\|?)+?\s*$/
-
+ # + -------------:+ ------------------------------ +
+ MultilineTableSeparator = /^(\+?#{Sep}\+?)+?\s*$/
+ # + ------------- + ------------------------------ +
+ MultilineTableSeparatorNoAlignment = /^\s*\+?(\s*-+\s*\+?)+?\s*$/
EMailAddress = /<([^:@>]+?@[^:@>]+?)>/
end
View
46 lib/maruku/input/parse_block.rb
@@ -136,10 +136,16 @@ def parse_blocks(src)
end
def read_text_material(src, output)
- if src.cur_line =~ MightBeTableHeader &&
+ maybeTableHeader = src.cur_line =~ MightBeTableHeader
+
+ if maybeTableHeader &&
src.next_line &&
src.next_line =~ TableSeparator
- output << read_table(src)
+ output << read_table(src, false)
+ elsif maybeTableHeader &&
+ src.next_line &&
+ src.next_line =~ MultilineTableSeparator
+ output << read_table(src, true)
elsif src.next_line && [:header1, :header2].include?(src.next_line.md_type)
output << read_header12(src)
elsif eventually_comes_a_def_list(src)
@@ -520,16 +526,40 @@ def read_ref_definition(src, out)
out << md_ref_def(id, url, :title => title)
end
- def split_cells(s)
- s.split('|').reject(&:empty?).map(&:strip)
+ def distribute!(a, b)
+ diff = [0, b.size - a.size].max
+ a.concat(Array.new(diff) { Array.new })
+ 0.upto a.size - 1 do |i|
+ a[i] << b[i]
+ end
+ a
+ end
+
+ def split_cells(src, isMultiline)
+ if isMultiline
+ cells = []
+
+ while src.cur_line && src.cur_line =~ /\|/ && src.cur_line !~ MultilineTableSeparatorNoAlignment
+ l = src.shift_line.split('|').reject(&:empty?).map(&:strip)
+ cells = distribute!(cells, l)
+ end
+
+ # If we stopped on a --+--+-- line, discard it
+ src.shift_line if src.cur_line && src.cur_line =~ MultilineTableSeparatorNoAlignment
+
+ cells.map { |a| a.join(' ') }
+ else
+ sep = src.cur_line =~ /\|/ ? '|' : '+'
+ src.shift_line.split(sep).reject(&:empty?).map(&:strip)
+ end
end
- def read_table(src)
- head = split_cells(src.shift_line).map do |s|
+ def read_table(src, isMultiline)
+ head = split_cells(src, false).map do |s|
md_el(:head_cell, parse_span(s))
end
- separator = split_cells(src.shift_line)
+ separator = split_cells(src, false)
align = separator.map do |s|
s =~ Sep
@@ -554,7 +584,7 @@ def read_table(src)
rows = []
while src.cur_line && src.cur_line =~ /\|/
- row = split_cells(src.shift_line).map do |s|
+ row = split_cells(src, isMultiline).map do |s|
md_el(:cell, parse_span(s))
end
Something went wrong with that request. Please try again.