Add support for table column display width markers
see https://orgmode.org/manual/Column-Width-and-Alignment.html#Column-Width-and-Alignment
This commit is contained in:
parent
9f7e8a8fbd
commit
9ffcaa8e1c
4 changed files with 17 additions and 10 deletions
15
org/table.go
15
org/table.go
|
@ -23,14 +23,15 @@ type Column struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ColumnInfo struct {
|
type ColumnInfo struct {
|
||||||
Align string
|
Align string
|
||||||
Len int
|
Len int
|
||||||
|
DisplayLen int
|
||||||
}
|
}
|
||||||
|
|
||||||
var tableSeparatorRegexp = regexp.MustCompile(`^(\s*)(\|[+-|]*)\s*$`)
|
var tableSeparatorRegexp = regexp.MustCompile(`^(\s*)(\|[+-|]*)\s*$`)
|
||||||
var tableRowRegexp = regexp.MustCompile(`^(\s*)(\|.*)`)
|
var tableRowRegexp = regexp.MustCompile(`^(\s*)(\|.*)`)
|
||||||
|
|
||||||
var columnAlignRegexp = regexp.MustCompile(`^<(l|c|r)>$`)
|
var columnAlignAndLengthRegexp = regexp.MustCompile(`^<(l|c|r)?(\d+)?>$`)
|
||||||
|
|
||||||
func lexTable(line string) (token, bool) {
|
func lexTable(line string) (token, bool) {
|
||||||
if m := tableSeparatorRegexp.FindStringSubmatch(line); m != nil {
|
if m := tableSeparatorRegexp.FindStringSubmatch(line); m != nil {
|
||||||
|
@ -94,7 +95,7 @@ func getColumnInfos(rows [][]string) []ColumnInfo {
|
||||||
columnInfos[i].Len = n
|
columnInfos[i].Len = n
|
||||||
}
|
}
|
||||||
|
|
||||||
if m := columnAlignRegexp.FindStringSubmatch(columns[i]); m != nil && isSpecialRow(columns) {
|
if m := columnAlignAndLengthRegexp.FindStringSubmatch(columns[i]); m != nil && isSpecialRow(columns) {
|
||||||
switch m[1] {
|
switch m[1] {
|
||||||
case "l":
|
case "l":
|
||||||
columnInfos[i].Align = "left"
|
columnInfos[i].Align = "left"
|
||||||
|
@ -103,6 +104,10 @@ func getColumnInfos(rows [][]string) []ColumnInfo {
|
||||||
case "r":
|
case "r":
|
||||||
columnInfos[i].Align = "right"
|
columnInfos[i].Align = "right"
|
||||||
}
|
}
|
||||||
|
if m[2] != "" {
|
||||||
|
l, _ := strconv.Atoi(m[2])
|
||||||
|
columnInfos[i].DisplayLen = l
|
||||||
|
}
|
||||||
} else if _, err := strconv.ParseFloat(columns[i], 32); err == nil {
|
} else if _, err := strconv.ParseFloat(columns[i], 32); err == nil {
|
||||||
countNumeric++
|
countNumeric++
|
||||||
} else if strings.TrimSpace(columns[i]) != "" {
|
} else if strings.TrimSpace(columns[i]) != "" {
|
||||||
|
@ -120,7 +125,7 @@ func getColumnInfos(rows [][]string) []ColumnInfo {
|
||||||
func isSpecialRow(rawColumns []string) bool {
|
func isSpecialRow(rawColumns []string) bool {
|
||||||
isAlignRow := true
|
isAlignRow := true
|
||||||
for _, rawColumn := range rawColumns {
|
for _, rawColumn := range rawColumns {
|
||||||
if !columnAlignRegexp.MatchString(rawColumn) && rawColumn != "" {
|
if !columnAlignAndLengthRegexp.MatchString(rawColumn) && rawColumn != "" {
|
||||||
isAlignRow = false
|
isAlignRow = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
org/testdata/tables.html
vendored
2
org/testdata/tables.html
vendored
|
@ -127,7 +127,7 @@ table without header
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<figcaption>
|
<figcaption>
|
||||||
table with aligned columns
|
table with aligned and sized columns
|
||||||
</figcaption>
|
</figcaption>
|
||||||
</figure>
|
</figure>
|
||||||
<figure>
|
<figure>
|
||||||
|
|
5
org/testdata/tables.org
vendored
5
org/testdata/tables.org
vendored
|
@ -24,10 +24,11 @@
|
||||||
#+CAPTION: table without header
|
#+CAPTION: table without header
|
||||||
| 1 | 2 | 3 |
|
| 1 | 2 | 3 |
|
||||||
|
|
||||||
#+CAPTION: table with aligned columns
|
#+CAPTION: table with aligned and sized columns
|
||||||
| left aligned | right aligned | center aligned |
|
| left aligned | right aligned | center aligned |
|
||||||
|--------------+---------------+----------------|
|
|--------------+---------------+----------------|
|
||||||
| <l> | <r> | <c> |
|
| <l> | <r> | <c5> |
|
||||||
|
| | <1> | |
|
||||||
| 42 | 42 | 42 |
|
| 42 | 42 | 42 |
|
||||||
| foobar | foobar | foobar |
|
| foobar | foobar | foobar |
|
||||||
|
|
||||||
|
|
5
org/testdata/tables.pretty_org
vendored
5
org/testdata/tables.pretty_org
vendored
|
@ -24,10 +24,11 @@
|
||||||
#+CAPTION: table without header
|
#+CAPTION: table without header
|
||||||
| 1 | 2 | 3 |
|
| 1 | 2 | 3 |
|
||||||
|
|
||||||
#+CAPTION: table with aligned columns
|
#+CAPTION: table with aligned and sized columns
|
||||||
| left aligned | right aligned | center aligned |
|
| left aligned | right aligned | center aligned |
|
||||||
|--------------+---------------+----------------|
|
|--------------+---------------+----------------|
|
||||||
| <l> | <r> | <c> |
|
| <l> | <r> | <c5> |
|
||||||
|
| | <1> | |
|
||||||
| 42 | 42 | 42 |
|
| 42 | 42 | 42 |
|
||||||
| foobar | foobar | foobar |
|
| foobar | foobar | foobar |
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue