Skip to content

Commit 6385869

Browse files
authored
Merge pull request #296 from dwyand/main
add column width to TableCell nodes
2 parents 1209160 + b134389 commit 6385869

File tree

3 files changed

+80
-7
lines changed

3 files changed

+80
-7
lines changed

commonmark-ext-gfm-tables/src/main/java/org/commonmark/ext/gfm/tables/TableCell.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class TableCell extends CustomNode {
99

1010
private boolean header;
1111
private Alignment alignment;
12+
private int width;
1213

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

36+
/**
37+
* @return the cell width (the number of dash and colon characters in the delimiter row of the table for this column)
38+
*/
39+
public int getWidth() {
40+
return width;
41+
}
42+
43+
public void setWidth(int width) {
44+
this.width = width;
45+
}
46+
3547
/**
3648
* How the cell is aligned horizontally.
3749
*/

commonmark-ext-gfm-tables/src/main/java/org/commonmark/ext/gfm/tables/internal/TableBlockParser.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public class TableBlockParser extends AbstractBlockParser {
1717

1818
private final TableBlock block = new TableBlock();
1919
private final List<SourceLine> rowLines = new ArrayList<>();
20-
private final List<TableCell.Alignment> columns;
20+
private final List<TableCellInfo> columns;
2121

2222
private boolean canHaveLazyContinuationLines = true;
2323

24-
private TableBlockParser(List<TableCell.Alignment> columns, SourceLine headerLine) {
24+
private TableBlockParser(List<TableCellInfo> columns, SourceLine headerLine) {
2525
this.columns = columns;
2626
this.rowLines.add(headerLine);
2727
}
@@ -120,7 +120,9 @@ private TableCell parseCell(SourceLine cell, int column, InlineParser inlinePars
120120
}
121121

122122
if (column < columns.size()) {
123-
tableCell.setAlignment(columns.get(column));
123+
TableCellInfo cellInfo = columns.get(column);
124+
tableCell.setAlignment(cellInfo.getAlignment());
125+
tableCell.setWidth(cellInfo.getWidth());
124126
}
125127

126128
CharSequence content = cell.getContent();
@@ -187,11 +189,12 @@ private static List<SourceLine> split(SourceLine line) {
187189
// -|-
188190
// |-|-|
189191
// --- | ---
190-
private static List<TableCell.Alignment> parseSeparator(CharSequence s) {
191-
List<TableCell.Alignment> columns = new ArrayList<>();
192+
private static List<TableCellInfo> parseSeparator(CharSequence s) {
193+
List<TableCellInfo> columns = new ArrayList<>();
192194
int pipes = 0;
193195
boolean valid = false;
194196
int i = 0;
197+
int width = 0;
195198
while (i < s.length()) {
196199
char c = s.charAt(i);
197200
switch (c) {
@@ -216,10 +219,12 @@ private static List<TableCell.Alignment> parseSeparator(CharSequence s) {
216219
if (c == ':') {
217220
left = true;
218221
i++;
222+
width++;
219223
}
220224
boolean haveDash = false;
221225
while (i < s.length() && s.charAt(i) == '-') {
222226
i++;
227+
width++;
223228
haveDash = true;
224229
}
225230
if (!haveDash) {
@@ -229,8 +234,10 @@ private static List<TableCell.Alignment> parseSeparator(CharSequence s) {
229234
if (i < s.length() && s.charAt(i) == ':') {
230235
right = true;
231236
i++;
237+
width++;
232238
}
233-
columns.add(getAlignment(left, right));
239+
columns.add(new TableCellInfo(getAlignment(left, right), width));
240+
width = 0;
234241
// Next, need another pipe
235242
pipes = 0;
236243
break;
@@ -270,7 +277,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
270277
if (paragraphLines.size() == 1 && Characters.find('|', paragraphLines.get(0).getContent(), 0) != -1) {
271278
SourceLine line = state.getLine();
272279
SourceLine separatorLine = line.substring(state.getIndex(), line.getContent().length());
273-
List<TableCell.Alignment> columns = parseSeparator(separatorLine.getContent());
280+
List<TableCellInfo> columns = parseSeparator(separatorLine.getContent());
274281
if (columns != null && !columns.isEmpty()) {
275282
SourceLine paragraph = paragraphLines.get(0);
276283
List<SourceLine> headerCells = split(paragraph);
@@ -284,4 +291,22 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
284291
return BlockStart.none();
285292
}
286293
}
294+
295+
private static class TableCellInfo {
296+
private final TableCell.Alignment alignment;
297+
private final int width;
298+
299+
public TableCell.Alignment getAlignment() {
300+
return alignment;
301+
}
302+
303+
public int getWidth() {
304+
return width;
305+
}
306+
307+
public TableCellInfo(TableCell.Alignment alignment, int width) {
308+
this.alignment = alignment;
309+
this.width = width;
310+
}
311+
}
287312
}

commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,42 @@ public void setAttributes(Node node, String tagName, Map<String, String> attribu
749749
"</table>\n"));
750750
}
751751

752+
@Test
753+
public void columnWidthIsRecorded() {
754+
AttributeProviderFactory factory = new AttributeProviderFactory() {
755+
@Override
756+
public AttributeProvider create(AttributeProviderContext context) {
757+
return new AttributeProvider() {
758+
@Override
759+
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
760+
if (node instanceof TableCell && "th".equals(tagName)) {
761+
attributes.put("width", ((TableCell) node).getWidth() + "em");
762+
}
763+
}
764+
};
765+
}
766+
};
767+
HtmlRenderer renderer = HtmlRenderer.builder()
768+
.attributeProviderFactory(factory)
769+
.extensions(EXTENSIONS)
770+
.build();
771+
String rendered = renderer.render(PARSER.parse("Abc|Def\n-----|---\n1|2"));
772+
assertThat(rendered, is("<table>\n" +
773+
"<thead>\n" +
774+
"<tr>\n" +
775+
"<th width=\"5em\">Abc</th>\n" +
776+
"<th width=\"3em\">Def</th>\n" +
777+
"</tr>\n" +
778+
"</thead>\n" +
779+
"<tbody>\n" +
780+
"<tr>\n" +
781+
"<td>1</td>\n" +
782+
"<td>2</td>\n" +
783+
"</tr>\n" +
784+
"</tbody>\n" +
785+
"</table>\n"));
786+
}
787+
752788
@Test
753789
public void sourceSpans() {
754790
Parser parser = Parser.builder()

0 commit comments

Comments
 (0)