add basic support for tables
Ted Unangst tedu@tedunangst.com
Wed, 01 Jan 2020 17:45:06 -0500
4 files changed,
38 insertions(+),
0 deletions(-)
M
docs/changelog.txt
→
docs/changelog.txt
@@ -2,6 +2,8 @@ changelog
=== next ++ Tables + + Reduce retries talking to dumb servers. === 0.8.6 Sartorial Headpiece
M
docs/honk.5
→
docs/honk.5
@@ -51,6 +51,8 @@ Lists of items starting with either
.Sq + or .Sq - . +.It tables +Table cells separated by |. .It images Inline images with img tags. .Bd -literal
M
markitzero.go
→
markitzero.go
@@ -34,6 +34,7 @@ var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)!]`)
var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`) var re_imgfix = regexp.MustCompile(`<img ([^>]*)>`) var re_lister = regexp.MustCompile(`((^|\n)(\+|-).*)+\n?`) +var re_tabler = regexp.MustCompile(`((^|\n)\|.*)+\n?`) var lighter = synlight.New(synlight.Options{Format: synlight.HTML})@@ -92,6 +93,26 @@ }
r += "</ul><p>" 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 s = strings.Replace(s, "<img x>", "<img x>", -1)@@ -122,6 +143,7 @@ s = strings.Replace(s, "<br><blockquote>", "<blockquote>", -1)
s = strings.Replace(s, "<br><cite></cite>", "", -1) s = strings.Replace(s, "<br><pre>", "<pre>", -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) return s }
M
markitzero_test.go
→
markitzero_test.go
@@ -108,6 +108,18 @@ output := `hello<ul><li>a list<li>the list</ul><p>para<ul><li>singleton</ul><p>`
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 func init() {