Skip to content

Commit

Permalink
Revert "[MD055, MD056, MD057] Features for validating table format (#464
Browse files Browse the repository at this point in the history
)" (#485)

This reverts commit 9d508cf.

While these rules seem good, they have caused regressions (see
#472). Reverting for
now until the original submitter has time to look into them.

Signed-off-by: Phil Dibowitz <phil@ipom.com>
  • Loading branch information
jaymzh committed Nov 20, 2023
1 parent a3f7c0f commit f97a364
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 481 deletions.
83 changes: 0 additions & 83 deletions docs/RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
* [MD041 - First line in file should be a top level header](#md041---first-line-in-file-should-be-a-top-level-header)
* [MD046 - Code block style](#md046---code-block-style)
* [MD047 - File should end with a single newline character](#md047---file-should-end-with-a-single-newline-character)
* [MD055 - Table row doesn't begin/end with pipes](#md055---table-row-doesnt-begin-end-with-pipes)
* [MD056 - Table has inconsistent number of columns](#md056---table-has-inconsistent-number-of-columns)
* [MD057 - Table has missing or invalid header separation (second row)](#md057---table-has-missing-or-invalid-header-separation)

# Rules

Expand Down Expand Up @@ -1323,83 +1320,3 @@ This file ends with a newline.
Rationale: Some programs have trouble with files that do not end with a newline.
More information:
<https://unix.stackexchange.com/questions/18743/whats-the-point-in-adding-a-new-line-to-the-end-of-a-file>.

## MD055 - Table row doesn't begin/end with pipes

Tags: tables

Aliases: table-rows-start-and-end-with-pipes

This rule is triggered when a table row does not start or end with pipe, '|',
character:

```markdown
| Heading 1 | Heading 2 |
| ------- | ---------
Data | Data |
```

To fix this, add or align pipe characters to the beginning or end of the row.

```markdown
| Heading 1 | Heading 2 |
| ------- | ---------|
| Data | Data |
```

Rationale: If the table rows are not well-formed, not all renderers
may show it correctly.

## MD056 - Table has inconsistent number of columns

Tags: tables

Aliases: inconsistent-columns-in-table

This rule is triggered when the number of columns in table rows do not match:

```markdown
| Heading 1 | Heading 2 |
| :--------:| ---------:| ----- |
|Data|Data|Data|
|Data|Data|
```

To fix this, match your number of columns with the header by either deleting
the extra columns or adding missing ones. One solution can be

```markdown
| Heading 1 | Heading 2 |
| :--------:| ---------:|
|Data|Data|
|Data|Data|
```

Rationale: If the table rows do not have the same number of columns, there may be
redundant or missing data.

## MD057 - Table has missing or invalid header separation (second row)

Tags: tables

Aliases: table-invalid-second-row

This rule is triggered when the header separation (second row in the table)
is not well-formed. It should at least contain three dashes, i.e. '---' and
might have colons, i.e. ':' at the start or end of the separation string:

```markdown
| Heading 1 | Heading 2 | Heading 3 | Heading 4 |
| -- | ::---:: | ---a--- |:----
|Data|Data|Data|Data|
```

To fix this, make sure the second row of the table conforms with the above rules

```markdown
| Heading 1 | Heading 2 | Heading 3 | Heading 4 |
| --- | :---: | ------ |:----|
|Data|Data|Data|Data|
```

Rationale: If the table is not well-formed, not all renderers may show it correctly.
89 changes: 0 additions & 89 deletions lib/mdl/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -799,92 +799,3 @@ def check_blockquote(errors, elements)
error_lines
end
end

rule 'MD055', 'Table row doesn\'t begin/end with pipes' do
tags :tables
aliases 'table-rows-start-and-end-with-pipes'
check do |doc|
error_lines = []
tables = doc.find_type_elements(:table)
lines = doc.lines

tables.each do |table|
table_pos = table.options[:location] - 1
table_rows = get_table_rows(lines, table_pos)

table_rows.each_with_index do |line, index|
if line.length < 2 || line[0] != '|' || line[-1] != '|'
error_lines << (table_pos + index + 1)
end
end
end

error_lines
end
end

rule 'MD056', 'Table has inconsistent number of columns' do
tags :tables
aliases 'inconsistent-columns-in-table'
check do |doc|
error_lines = []
tables = doc.find_type_elements(:table)
lines = doc.lines

tables.each do |table|
table_pos = table.options[:location] - 1
table_rows = get_table_rows(lines, table_pos)

num_headings = number_of_columns_in_a_table_row(lines[table_pos])

table_rows.each_with_index do |line, index|
if number_of_columns_in_a_table_row(line) != num_headings
error_lines << (table_pos + index + 1)
end
end
end

error_lines
end
end

rule 'MD057', 'Table has missing or invalid header separation (second row)' do
tags :tables
aliases 'table-invalid-second-row'
check do |doc|
error_lines = []
tables = doc.find_type_elements(:table)
lines = doc.lines

tables.each do |table|
second_row = ''

# line number of table start (1-indexed)
# which is equal to second row's index (0-indexed)
line_num = table.options[:location]
second_row = lines[line_num] if line_num < lines.length

# This pattern matches if
# 1) The row starts and stops with | characters
# 2) Only consists of characters '|', '-', ':' and whitespace
# 3) Each section between the separators (i.e. '|')
# a) has at least three consecutive dashes
# b) can have whitespace at the beginning or the end
# c) can have colon before and/or after dashes (for alignment)
# Some examples:
# |-----|----|-------| --> matches
# |:---:|:---|-------| --> matches
# | :------: | ----| --> matches
# | - - - | - - - | --> does NOT match
# |::---| --> does NOT match
# |----:|:--|----| --> does NOT match
pattern = /^(\|\s*:?-{3,}:?\s*)+\|$/
unless second_row.match(pattern)
# Second row is not in the form described by the pattern
error_lines << (line_num + 1)
end
end

error_lines
end
end
61 changes: 0 additions & 61 deletions lib/mdl/ruleset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,67 +48,6 @@ def docs(url = nil, &block)
def docs_url
@generate_docs&.call(id, description)
end

# This method calculates the number of columns in a table row
#
# @param [String] table_row A row of the table in question.
# @return [Numeric] Number of columns in the row
def number_of_columns_in_a_table_row(table_row)
columns = table_row.strip.split('|')

if columns.empty?
# The stripped line consists of zero or more pipe characters
# and nothing more.
#
# Examples of stripped rows:
# '||' --> one column
# '|||' --> two columns
# '|' --> zero columns
[0, table_row.count('|') - 1].max
else
# Number of columns is the number of splited
# segments with pipe separator. The first segment
# is ignored when it's empty string because
# someting like '|1|2|' is split into ['', '1', '2']
# when using split('|') function.
#
# Some examples:
# '|foo|bar|' --> two columns
# ' |foo|bar|' --> two columns
# '|foo|bar' --> two columns
# 'foo|bar' --> two columns
columns.size - (columns[0].empty? ? 1 : 0)
end
end

# This method returns all the rows of a table
#
# @param [Array<String>] lines Lines of a doc as an array
# @param [Numeric] pos Position/index of the table in the array
# @return [Array<String>] Rows of the table in an array
def get_table_rows(lines, pos)
table_rows = []
while pos < lines.length
line = lines[pos]

# If the previous line is a table and the current line
# 1) includes pipe character
# 2) does not start with code block identifiers
# a) >= 4 spaces
# b) < 4 spaces and ``` right after
#
# it is possibly a table row
unless line.include?('|') && !line.start_with?(' ') &&
!line.strip.start_with?('```')
break
end

table_rows << line
pos += 1
end

table_rows
end
end

# defines a ruleset
Expand Down
Loading

0 comments on commit f97a364

Please sign in to comment.