Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class TableCell extends CustomNode {

private boolean header;
private Alignment alignment;
private int width;

/**
* @return whether the cell is a header or not
Expand All @@ -32,6 +33,17 @@ public void setAlignment(Alignment alignment) {
this.alignment = alignment;
}

/**
* @return the cell width (the number of dash and colon characters in the delimiter row of the table for this column)
*/
public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

/**
* How the cell is aligned horizontally.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public class TableBlockParser extends AbstractBlockParser {

private final TableBlock block = new TableBlock();
private final List<SourceLine> rowLines = new ArrayList<>();
private final List<TableCell.Alignment> columns;
private final List<TableCellInfo> columns;

private boolean canHaveLazyContinuationLines = true;

private TableBlockParser(List<TableCell.Alignment> columns, SourceLine headerLine) {
private TableBlockParser(List<TableCellInfo> columns, SourceLine headerLine) {
this.columns = columns;
this.rowLines.add(headerLine);
}
Expand Down Expand Up @@ -120,7 +120,9 @@ private TableCell parseCell(SourceLine cell, int column, InlineParser inlinePars
}

if (column < columns.size()) {
tableCell.setAlignment(columns.get(column));
TableCellInfo cellInfo = columns.get(column);
tableCell.setAlignment(cellInfo.getAlignment());
tableCell.setWidth(cellInfo.getWidth());
}

CharSequence content = cell.getContent();
Expand Down Expand Up @@ -187,11 +189,12 @@ private static List<SourceLine> split(SourceLine line) {
// -|-
// |-|-|
// --- | ---
private static List<TableCell.Alignment> parseSeparator(CharSequence s) {
List<TableCell.Alignment> columns = new ArrayList<>();
private static List<TableCellInfo> parseSeparator(CharSequence s) {
List<TableCellInfo> columns = new ArrayList<>();
int pipes = 0;
boolean valid = false;
int i = 0;
int width = 0;
while (i < s.length()) {
char c = s.charAt(i);
switch (c) {
Expand All @@ -216,10 +219,12 @@ private static List<TableCell.Alignment> parseSeparator(CharSequence s) {
if (c == ':') {
left = true;
i++;
width++;
}
boolean haveDash = false;
while (i < s.length() && s.charAt(i) == '-') {
i++;
width++;
haveDash = true;
}
if (!haveDash) {
Expand All @@ -229,8 +234,10 @@ private static List<TableCell.Alignment> parseSeparator(CharSequence s) {
if (i < s.length() && s.charAt(i) == ':') {
right = true;
i++;
width++;
}
columns.add(getAlignment(left, right));
columns.add(new TableCellInfo(getAlignment(left, right), width));
width = 0;
// Next, need another pipe
pipes = 0;
break;
Expand Down Expand Up @@ -270,7 +277,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
if (paragraphLines.size() == 1 && Parsing.find('|', paragraphLines.get(0).getContent(), 0) != -1) {
SourceLine line = state.getLine();
SourceLine separatorLine = line.substring(state.getIndex(), line.getContent().length());
List<TableCell.Alignment> columns = parseSeparator(separatorLine.getContent());
List<TableCellInfo> columns = parseSeparator(separatorLine.getContent());
if (columns != null && !columns.isEmpty()) {
SourceLine paragraph = paragraphLines.get(0);
List<SourceLine> headerCells = split(paragraph);
Expand All @@ -284,4 +291,22 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
return BlockStart.none();
}
}

private static class TableCellInfo {
private final TableCell.Alignment alignment;
private final int width;

public TableCell.Alignment getAlignment() {
return alignment;
}

public int getWidth() {
return width;
}

public TableCellInfo(TableCell.Alignment alignment, int width) {
this.alignment = alignment;
this.width = width;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,42 @@ public void setAttributes(Node node, String tagName, Map<String, String> attribu
"</table>\n"));
}

@Test
public void columnWidthIsRecorded() {
AttributeProviderFactory factory = new AttributeProviderFactory() {
@Override
public AttributeProvider create(AttributeProviderContext context) {
return new AttributeProvider() {
@Override
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
if (node instanceof TableCell && "th".equals(tagName)) {
attributes.put("width", ((TableCell) node).getWidth() + "em");
}
}
};
}
};
HtmlRenderer renderer = HtmlRenderer.builder()
.attributeProviderFactory(factory)
.extensions(EXTENSIONS)
.build();
String rendered = renderer.render(PARSER.parse("Abc|Def\n-----|---\n1|2"));
assertThat(rendered, is("<table>\n" +
"<thead>\n" +
"<tr>\n" +
"<th width=\"5em\">Abc</th>\n" +
"<th width=\"3em\">Def</th>\n" +
"</tr>\n" +
"</thead>\n" +
"<tbody>\n" +
"<tr>\n" +
"<td>1</td>\n" +
"<td>2</td>\n" +
"</tr>\n" +
"</tbody>\n" +
"</table>\n"));
}

@Test
public void sourceSpans() {
Parser parser = Parser.builder()
Expand Down