Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

Fix multiple markdown table issues. #2

Merged
merged 1 commit into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions src/main/java/com/overzealous/remark/convert/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,58 @@ public class Table extends AbstractNodeHandler {

// Largest amount of cells found in a row
private int maxRowCells;
private boolean hasHeader;

private static final Pattern STYLE_ALIGNMENT_PATTERN =
Pattern.compile("text-align:\\s*([a-z]+)", Pattern.CASE_INSENSITIVE);
private static final Pattern STYLE_ALIGNMENT_PATTERN = Pattern.compile("text-align:\\s*([a-z]+)",
Pattern.CASE_INSENSITIVE);


public void handleNode(NodeHandler parent, Element node, DocumentConverter converter) {
MarkdownTable table = new MarkdownTable();
boolean hasHeader = false;
hasHeader = false;
maxRowCells = 0;

processTable(table, node, converter);

if(!hasHeader) {
// No header was created, need to insert an empty one for markdown to work
insertEmptyRow(table.addHeaderRow(), maxRowCells);
}

// OK, now render this sucker
Options.Tables opts = converter.options.getTables();
converter.output.startBlock();
table.renderTable(converter.output, opts.isColspanEnabled(), opts.isRenderedAsCode());
converter.output.endBlock();
}

private void processTable(MarkdownTable table, Element node, DocumentConverter converter) {
// loop over every direct child of the table node.
for(final Element child : node.children()) {

if(child.tagName().equals("thead")) {
String childTagName = child.tagName();
if(childTagName.equals("thead")) {
hasHeader = true;
// handle explicitly declared header sections
for(final Element headerRow : child.children()) {
processRow(table.addHeaderRow(), headerRow, converter);
}

} else if(child.tagName().equals("tbody") || child.tagName().equals("tfoot")) {
// handle body or foot sections - note: there's no special handling for tfoot
for(final Element bodyRow : child.children()) {
updateMaxRowCells(bodyRow);
processRow(table.addBodyRow(), bodyRow, converter);
}

} else if(child.tagName().equals("tr")) {
} else if(childTagName.equals("tbody") || childTagName.equals("tfoot")) {
// Chance there are headers in body/footer need to go inside to verify.
processTable(table, child, converter);
} else if (childTagName.equals("tr")) {
// Hrm, a row was added outside a valid table body or header...
if(!child.children().isEmpty()) {
if(child.children().get(0).tagName().equals("th")) {
hasHeader = true;
// handle manual TH cells
processRow(table.addHeaderRow(), child, converter);
if(hasHeader == true) {
// already has header, treat this as a regular body row
processRow(table.addBodyRow(), child, converter);
} else {
hasHeader = true;
// handle manual TH cells
processRow(table.addHeaderRow(), child, converter);
}

} else {
// OK, must be a table row.
updateMaxRowCells(child);
Expand All @@ -74,17 +92,6 @@ public void handleNode(NodeHandler parent, Element node, DocumentConverter conve
}
}
}

if(!hasHeader) {
// No header was created, need to insert an empty one for markdown to work
insertEmptyRow(table.addHeaderRow(), maxRowCells);
}

// OK, now render this sucker
Options.Tables opts = converter.options.getTables();
converter.output.startBlock();
table.renderTable(converter.output, opts.isColspanEnabled(), opts.isRenderedAsCode());
converter.output.endBlock();
}

private void processRow(List<MarkdownTableCell> row, Element tableRow, DocumentConverter converter) {
Expand Down
26 changes: 26 additions & 0 deletions src/test/resources/conversions/html/tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@
</tr>
</table>

<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>One</td>
<td>Two</td>
</tr>
</table>

<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>One</td>
<td>Two</td>
</tr>
<tr>
<th>Header A</th>
<th>Header B</th>
</tr>
</table>

<table>
<thead><tr><th>head 1</th><th>head 2</th></tr></thead>
<tbody><tr><td>one</td><td>two</td></tr></tbody>
Expand Down
9 changes: 9 additions & 0 deletions src/test/resources/conversions/markdown/tables-codeblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
| One |
| -Two- |

| Header 1 | Header 2 |
| -------- | -------- |
| One | Two |

| Header 1 | Header 2 |
| -------- | -------- |
| One | Two |
| Header A | Header B |

| head 1 | head 2 |
| ------ | ------ |
| one | two |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
| One |
| -Two- |

| Header 1 | Header 2 |
| -------- | -------- |
| One | Two |

| Header 1 | Header 2 |
| -------- | -------- |
| One | Two |
| Header A | Header B |

| head 1 | head 2 |
| ------ | ------ |
| one | two |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
| One |
| -Two- |

| Header 1 | Header 2 |
| -------- | -------- |
| One | Two |

| Header 1 | Header 2 |
| -------- | -------- |
| One | Two |
| Header A | Header B |

| head 1 | head 2 |
| ------ | ------ |
| one | two |
Expand Down
30 changes: 30 additions & 0 deletions src/test/resources/conversions/markdown/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@
</tbody>
</table>

<table>
<tbody>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>One</td>
<td>Two</td>
</tr>
</tbody>
</table>

<table>
<tbody>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>One</td>
<td>Two</td>
</tr>
<tr>
<th>Header A</th>
<th>Header B</th>
</tr>
</tbody>
</table>

<table>
<thead>
<tr>
Expand Down