Improve table parsing: support separator before header
This commit is contained in:
parent
cb81eb94de
commit
c08119bbc8
4 changed files with 90 additions and 4 deletions
|
@ -211,8 +211,9 @@ func (w *OrgWriter) writeTable(t Table) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OrgWriter) writeTableHeader(th TableHeader) {
|
func (w *OrgWriter) writeTableHeader(th TableHeader) {
|
||||||
|
w.writeNodes(th.SeparatorBefore)
|
||||||
w.writeTableColumns(th.Columns)
|
w.writeTableColumns(th.Columns)
|
||||||
w.writeNodes(th.Separator)
|
w.writeNodes(th.SeparatorAfter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OrgWriter) writeTableRow(tr TableRow) {
|
func (w *OrgWriter) writeTableRow(tr TableRow) {
|
||||||
|
|
16
org/table.go
16
org/table.go
|
@ -13,8 +13,9 @@ type Table struct {
|
||||||
type TableSeparator struct{ Content string }
|
type TableSeparator struct{ Content string }
|
||||||
|
|
||||||
type TableHeader struct {
|
type TableHeader struct {
|
||||||
|
SeparatorBefore Node
|
||||||
Columns [][]Node
|
Columns [][]Node
|
||||||
Separator TableSeparator
|
SeparatorAfter Node
|
||||||
}
|
}
|
||||||
|
|
||||||
type TableRow struct{ Columns [][]Node }
|
type TableRow struct{ Columns [][]Node }
|
||||||
|
@ -40,13 +41,24 @@ func (d *Document) parseTable(i int, parentStop stopFn) (int, Node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
consumed := i - start
|
consumed := i - start
|
||||||
|
|
||||||
if len(rows) >= 2 {
|
if len(rows) >= 2 {
|
||||||
if row, ok := rows[0].(TableRow); ok {
|
if row, ok := rows[0].(TableRow); ok {
|
||||||
if separator, ok := rows[1].(TableSeparator); 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}
|
return consumed, Table{nil, rows}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
55
org/testdata/tables.html
vendored
Normal file
55
org/testdata/tables.html
vendored
Normal 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
18
org/testdata/tables.org
vendored
Normal 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 |
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue