1
1
package com .vladsch .flexmark .ext .gfm .tables .internal ;
2
2
3
3
import com .vladsch .flexmark .ext .gfm .tables .*;
4
+ import com .vladsch .flexmark .internal .BlockContent ;
4
5
import com .vladsch .flexmark .internal .util .BasedSequence ;
5
6
import com .vladsch .flexmark .internal .util .SubSequence ;
6
7
import com .vladsch .flexmark .node .Block ;
@@ -22,18 +23,17 @@ public class TableBlockParser extends AbstractBlockParser {
22
23
"\\ |?" + "(?:" + COL + "\\ |)+" + COL + "\\ |?\\ s*" );
23
24
24
25
private final TableBlock block = new TableBlock ();
25
- private final List < BasedSequence > rowLines = new ArrayList <> ();
26
+ private final BlockContent content = new BlockContent ();
26
27
27
- private boolean nextIsSeparatorLine = true ;
28
+ private boolean nextIsSeparatorLine = false ;
28
29
private BasedSequence separatorLine = SubSequence .NULL ;
29
30
private int separatorLineNumber = 0 ;
30
31
31
32
public static boolean bodyColumnsFilledToHead = true ;
32
33
public static boolean bodyColumnsTruncatedToHead = true ;
33
34
public static int maxHeaderRows = 1 ;
34
35
35
- private TableBlockParser (BasedSequence headerLine ) {
36
- rowLines .add (headerLine );
36
+ private TableBlockParser () {
37
37
}
38
38
39
39
@ Override
@@ -55,16 +55,14 @@ public void addLine(BasedSequence line, int eolLength) {
55
55
if (nextIsSeparatorLine ) {
56
56
nextIsSeparatorLine = false ;
57
57
separatorLine = line ;
58
- separatorLineNumber = rowLines .size ();
59
- rowLines .add (line );
60
- } else {
61
- rowLines .add (line );
58
+ separatorLineNumber = content .getLineCount ();
62
59
}
60
+ content .add (line , eolLength );
63
61
}
64
62
65
63
@ Override
66
64
public void closeBlock (ParserState parserState ) {
67
- block .setCharsFromContent ( );
65
+ block .setContent ( content );
68
66
}
69
67
70
68
@ Override
@@ -75,8 +73,9 @@ public void parseInlines(InlineParser inlineParser) {
75
73
List <TableCell .Alignment > alignments = parseAlignment (separatorLine );
76
74
77
75
int rowNumber = 0 ;
78
- int headerColumns = -1 ;
79
- for (BasedSequence rowLine : rowLines ) {
76
+ int separatorColumns = alignments .size ();
77
+
78
+ for (BasedSequence rowLine : content .getLines ()) {
80
79
if (rowNumber == separatorLineNumber ) {
81
80
section .setCharsFromContent ();
82
81
section = new TableSeparator ();
@@ -88,23 +87,15 @@ public void parseInlines(InlineParser inlineParser) {
88
87
}
89
88
90
89
List <BasedSequence > cells = split (rowLine );
91
- TableRow tableRow = new TableRow ();
90
+ TableRow tableRow = new TableRow (rowLine . subSequence ( 0 , rowLine . length () - content . getEolLength ()) );
92
91
93
92
int rowCells = countCells (cells );
94
93
int maxColumns = rowCells ;
95
94
96
- if (rowNumber < separatorLineNumber || headerColumns == -1 ) {
97
- if (headerColumns == -1 ) {
98
- headerColumns = rowCells ;
99
- } else if (headerColumns < rowCells ) {
100
- headerColumns = rowCells ;
101
- }
102
- }
103
-
104
- if (bodyColumnsTruncatedToHead && maxColumns > headerColumns ) maxColumns = headerColumns ;
95
+ if (bodyColumnsTruncatedToHead && maxColumns > separatorColumns ) maxColumns = separatorColumns ;
105
96
if (rowNumber >= separatorLineNumber ) {
106
97
if (!bodyColumnsFilledToHead && rowCells < maxColumns ) maxColumns = rowCells ;
107
- else if (bodyColumnsFilledToHead && maxColumns < headerColumns ) maxColumns = headerColumns ;
98
+ else if (bodyColumnsFilledToHead && maxColumns < separatorColumns ) maxColumns = separatorColumns ;
108
99
}
109
100
110
101
int segmentOffset = 0 ;
@@ -185,7 +176,7 @@ private static List<BasedSequence> split(BasedSequence input) {
185
176
List <BasedSequence > segments = new ArrayList <>();
186
177
187
178
if (line .startsWith ("|" )) {
188
- segments .add (line .subSequence (0 , 1 ));
179
+ // segments.add(line.subSequence(0, 1));
189
180
line = line .subSequence (1 , lineLength );
190
181
lineLength --;
191
182
}
@@ -207,7 +198,7 @@ private static List<BasedSequence> split(BasedSequence input) {
207
198
break ;
208
199
case '|' :
209
200
segments .add (line .subSequence (lastPos , i ));
210
- segments .add (line .subSequence (i , i + 1 ));
201
+ // segments.add(line.subSequence(i, i + 1));
211
202
lastPos = i + 1 ;
212
203
cellChars = 0 ;
213
204
break ;
@@ -248,7 +239,11 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
248
239
List <BasedSequence > headParts = split (paragraph );
249
240
List <BasedSequence > separatorParts = split (separatorLine );
250
241
if (separatorParts .size () >= headParts .size ()) {
251
- return BlockStart .of (new TableBlockParser (paragraph ))
242
+ TableBlockParser tableBlockParser = new TableBlockParser ();
243
+ tableBlockParser .addLine (paragraph , matchedBlockParser .getParagraphEolLengths ().get (0 ));
244
+ tableBlockParser .nextIsSeparatorLine = true ;
245
+
246
+ return BlockStart .of (tableBlockParser )
252
247
.atIndex (state .getIndex ())
253
248
.replaceActiveBlockParser ();
254
249
}
0 commit comments