Skip to content

Commit

Permalink
fix pipe tables (see #6) (#7)
Browse files Browse the repository at this point in the history
Fix detection of separator lines:
1. Always check that at least one dash is present
2. Analyze whole content between pipes including white spaces
  • Loading branch information
sivukhin authored Jun 26, 2024
1 parent 8e9588a commit c976456
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 deletions.
14 changes: 9 additions & 5 deletions djot_parser/djot_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,18 @@ func buildDjotAst(
for s := i + 1; s < i+openToken.JumpToPair; s++ {
if list[s].Type == djot_tokenizer.PipeTableSeparator {
columns++
content := bytes.TrimSpace(document[list[s].End:list[s+list[s].JumpToPair].Start])
if bytes.HasPrefix(content, []byte(":-")) && bytes.Count(content, []byte("-")) == len(content)-1 {
content := document[list[s].Start+1 : list[s+list[s].JumpToPair].End-1]
dashCount := bytes.Count(content, []byte("-"))
if dashCount == 0 {
continue
}
if bytes.HasPrefix(content, []byte(":-")) && dashCount == len(content)-1 {
alignments = append(alignments, LeftAlignment)
} else if bytes.HasSuffix(content, []byte("-:")) && bytes.Count(content, []byte("-")) == len(content)-1 {
} else if bytes.HasSuffix(content, []byte("-:")) && dashCount == len(content)-1 {
alignments = append(alignments, RightAlignment)
} else if bytes.HasPrefix(content, []byte(":-")) && bytes.HasSuffix(content, []byte("-:")) && bytes.Count(content, []byte("-")) == len(content)-2 {
} else if bytes.HasPrefix(content, []byte(":-")) && bytes.HasSuffix(content, []byte("-:")) && dashCount == len(content)-2 {
alignments = append(alignments, CenterAlignment)
} else if bytes.Count(content, []byte("-")) == len(content) {
} else if dashCount == len(content) {
alignments = append(alignments, DefaultAlignment)
}
}
Expand Down
14 changes: 14 additions & 0 deletions djot_parser/examples/complex_table.djot
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
| XXX, XXXXX | XXXXXX | XXXXXX |
|---------------------|-----------------|------------------|
| XXXXX XXXX | XXXX-XX XXXX | XXXXXXXXX, XX |
| XXXXX XXXXX (X) | XX:XXXX | X:XXXX |
| | | |
| XXX, XXXXX | XXXXXX | XXXXXX |
|---------------------|-----------------|------------------|
| XXXXX XXXX | XXXXXXXXX, XX | XXXXXX, XX |
| XXXXX XXXXXXX+ (X) | X:XXXX | XX:XXXX |
| | | |
| XXX, XXXXX | XXXXXX | XXXXXX |
|---------------------|-----------------|------------------|
| XXXXX XXXX | XXXXXX, XX | XXXX-XX XXXX |
| XXXXX XXXXX (X) | X:XXXX | X:XXXX |
57 changes: 57 additions & 0 deletions djot_parser/examples/complex_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<table>
<tr>
<th>XXX, XXXXX</th>
<th>XXXXXX</th>
<th>XXXXXX</th>
</tr>
<tr>
<td>XXXXX XXXX</td>
<td>XXXX-XX XXXX</td>
<td>XXXXXXXXX, XX</td>
</tr>
<tr>
<td>XXXXX XXXXX (X)</td>
<td>XX:XXXX</td>
<td>X:XXXX</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<th>XXX, XXXXX</th>
<th>XXXXXX</th>
<th>XXXXXX</th>
</tr>
<tr>
<td>XXXXX XXXX</td>
<td>XXXXXXXXX, XX</td>
<td>XXXXXX, XX</td>
</tr>
<tr>
<td>XXXXX XXXXXXX+ (X)</td>
<td>X:XXXX</td>
<td>XX:XXXX</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<th>XXX, XXXXX</th>
<th>XXXXXX</th>
<th>XXXXXX</th>
</tr>
<tr>
<td>XXXXX XXXX</td>
<td>XXXXXX, XX</td>
<td>XXXX-XX XXXX</td>
</tr>
<tr>
<td>XXXXX XXXXX (X)</td>
<td>X:XXXX</td>
<td>X:XXXX</td>
</tr>
</table>
3 changes: 3 additions & 0 deletions djot_parser/examples/complex_table2.djot
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| -- | -- | -- |
|--|--|--|
| - | -- | ---- |
12 changes: 12 additions & 0 deletions djot_parser/examples/complex_table2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<table>
<tr>
<th>&ndash;</th>
<th>&ndash;</th>
<th>&ndash;</th>
</tr>
<tr>
<td>-</td>
<td>&ndash;</td>
<td>&ndash;&ndash;</td>
</tr>
</table>

0 comments on commit c976456

Please sign in to comment.