Improve table parsing: support separator before header

This commit is contained in:
Niklas Fasching 2018-12-13 17:48:38 +01:00
parent cb81eb94de
commit c08119bbc8
4 changed files with 90 additions and 4 deletions

View file

@ -211,8 +211,9 @@ func (w *OrgWriter) writeTable(t Table) {
}
func (w *OrgWriter) writeTableHeader(th TableHeader) {
w.writeNodes(th.SeparatorBefore)
w.writeTableColumns(th.Columns)
w.writeNodes(th.Separator)
w.writeNodes(th.SeparatorAfter)
}
func (w *OrgWriter) writeTableRow(tr TableRow) {

View file

@ -13,8 +13,9 @@ type Table struct {
type TableSeparator struct{ Content string }
type TableHeader struct {
SeparatorBefore Node
Columns [][]Node
Separator TableSeparator
SeparatorAfter Node
}
type TableRow struct{ Columns [][]Node }
@ -40,13 +41,24 @@ func (d *Document) parseTable(i int, parentStop stopFn) (int, Node) {
}
consumed := i - start
if len(rows) >= 2 {
if row, ok := rows[0].(TableRow); ok {
if separator, ok := rows[1].(TableSeparator); ok {
return consumed, Table{TableHeader{row.Columns, separator}, rows[2:]}
return consumed, Table{TableHeader{nil, row.Columns, separator}, rows[2:]}
}
}
}
if len(rows) >= 3 {
if separatorBefore, ok := rows[0].(TableSeparator); ok {
if row, ok := rows[1].(TableRow); ok {
if separatorAfter, ok := rows[2].(TableSeparator); ok {
return consumed, Table{TableHeader{separatorBefore, row.Columns, separatorAfter}, rows[3:]}
}
}
}
}
return consumed, Table{nil, rows}
}

55
org/testdata/tables.html vendored Normal file
View file

@ -0,0 +1,55 @@
<figure>
<table>
<thead>
<th>a</th><th>b</th><th>c</th>
</thead>
<tbody>
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
</tbody>
</table>
<figcaption>
table with separator before and after header
</figcaption>
</figure>
<figure>
<table>
<thead>
<th>a</th><th>b</th><th>c</th>
</thead>
<tbody>
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
</tbody>
</table>
<figcaption>
table with separator after header
</figcaption>
</figure>
<figure>
<table>
<tbody>
<tr></tr>
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
</tbody>
</table>
<figcaption>
table without header (but separator before)
</figcaption>
</figure>
<figure>
<table>
<tbody>
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
</tbody>
</table>
<figcaption>
table without header
</figcaption>
</figure>

18
org/testdata/tables.org vendored Normal file
View file

@ -0,0 +1,18 @@
#+CAPTION: table with separator before and after header
|---+---+---|
| a | b | c |
|---+---+---|
| 1 | 2 | 3 |
#+CAPTION: table with separator after header
| a | b | c |
|---+---+---|
| 1 | 2 | 3 |
#+CAPTION: table without header (but separator before)
|---+---+---|
| 1 | 2 | 3 |
#+CAPTION: table without header
| 1 | 2 | 3 |