add basic support for tables

This commit is contained in:
Ted Unangst 2020-01-01 17:45:06 -05:00
parent 049a15612d
commit 676d34ecd9
4 changed files with 38 additions and 0 deletions

View File

@ -2,6 +2,8 @@ changelog
=== next === next
+ Tables
+ Reduce retries talking to dumb servers. + Reduce retries talking to dumb servers.
=== 0.8.6 Sartorial Headpiece === 0.8.6 Sartorial Headpiece

View File

@ -51,6 +51,8 @@ Lists of items starting with either
.Sq + .Sq +
or or
.Sq - . .Sq - .
.It tables
Table cells separated by |.
.It images .It images
Inline images with img tags. Inline images with img tags.
.Bd -literal .Bd -literal

View File

@ -34,6 +34,7 @@ var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)!]`)
var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`) var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`)
var re_imgfix = regexp.MustCompile(`<img ([^>]*)>`) var re_imgfix = regexp.MustCompile(`<img ([^>]*)>`)
var re_lister = regexp.MustCompile(`((^|\n)(\+|-).*)+\n?`) var re_lister = regexp.MustCompile(`((^|\n)(\+|-).*)+\n?`)
var re_tabler = regexp.MustCompile(`((^|\n)\|.*)+\n?`)
var lighter = synlight.New(synlight.Options{Format: synlight.HTML}) var lighter = synlight.New(synlight.Options{Format: synlight.HTML})
@ -92,6 +93,26 @@ func markitzero(s string) string {
r += "</ul><p>" r += "</ul><p>"
return r return r
}) })
s = re_tabler.ReplaceAllStringFunc(s, func(m string) string {
m = strings.Trim(m, "\n")
rows := strings.Split(m, "\n")
var r strings.Builder
r.WriteString("<table>")
for _, row := range rows {
r.WriteString("<tr>")
cells := strings.Split(row, "|")
for i, cell := range cells {
cell = strings.TrimSpace(cell)
if cell == "" && (i == 0 || i == len(cells)-1) {
continue
}
r.WriteString("<td>")
r.WriteString(cell)
}
}
r.WriteString("</table><p>")
return r.String()
})
// restore images // restore images
s = strings.Replace(s, "&lt;img x&gt;", "<img x>", -1) s = strings.Replace(s, "&lt;img x&gt;", "<img x>", -1)
@ -122,6 +143,7 @@ func markitzero(s string) string {
s = strings.Replace(s, "<br><cite></cite>", "", -1) s = strings.Replace(s, "<br><cite></cite>", "", -1)
s = strings.Replace(s, "<br><pre>", "<pre>", -1) s = strings.Replace(s, "<br><pre>", "<pre>", -1)
s = strings.Replace(s, "<br><ul>", "<ul>", -1) s = strings.Replace(s, "<br><ul>", "<ul>", -1)
s = strings.Replace(s, "<br><table>", "<table>", -1)
s = strings.Replace(s, "<p><br>", "<p>", -1) s = strings.Replace(s, "<p><br>", "<p>", -1)
return s return s
} }

View File

@ -108,6 +108,18 @@ para
doonezerotest(t, input, output) doonezerotest(t, input, output)
} }
func TestTables(t *testing.T) {
input := `hello
| col1 | col 2 |
| row2 | cell4 |
para
`
output := `hello<table><tr><td>col1<td>col 2<tr><td>row2<td>cell4</table><p>para`
doonezerotest(t, input, output)
}
var benchData, simpleData string var benchData, simpleData string
func init() { func init() {