From 676d34ecd951bc46e8b3c8c7725e500b5e2d0b93 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Wed, 1 Jan 2020 17:45:06 -0500 Subject: [PATCH] add basic support for tables --- docs/changelog.txt | 2 ++ docs/honk.5 | 2 ++ markitzero.go | 22 ++++++++++++++++++++++ markitzero_test.go | 12 ++++++++++++ 4 files changed, 38 insertions(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 31fc77f..5acd20d 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -2,6 +2,8 @@ changelog === next ++ Tables + + Reduce retries talking to dumb servers. === 0.8.6 Sartorial Headpiece diff --git a/docs/honk.5 b/docs/honk.5 index 0f0cf8e..f0dc1de 100644 --- a/docs/honk.5 +++ b/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 diff --git a/markitzero.go b/markitzero.go index 464de15..0253a2f 100644 --- a/markitzero.go +++ b/markitzero.go @@ -34,6 +34,7 @@ var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)!]`) var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`) var re_imgfix = regexp.MustCompile(`]*)>`) 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 @@ func markitzero(s string) string { 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("") + for _, row := range rows { + r.WriteString("") + cells := strings.Split(row, "|") + for i, cell := range cells { + cell = strings.TrimSpace(cell) + if cell == "" && (i == 0 || i == len(cells)-1) { + continue + } + r.WriteString("
") + r.WriteString(cell) + } + } + r.WriteString("

") + return r.String() + }) // restore images s = strings.Replace(s, "<img x>", "", -1) @@ -122,6 +143,7 @@ func markitzero(s string) string { s = strings.Replace(s, "
", "", -1) s = strings.Replace(s, "

", "
", -1)
 	s = strings.Replace(s, "
    ", "
      ", -1) + s = strings.Replace(s, "
      ", "
      ", -1) s = strings.Replace(s, "


      ", "

      ", -1) return s } diff --git a/markitzero_test.go b/markitzero_test.go index 2f39fcb..208e159 100644 --- a/markitzero_test.go +++ b/markitzero_test.go @@ -108,6 +108,18 @@ para doonezerotest(t, input, output) } +func TestTables(t *testing.T) { + input := `hello + +| col1 | col 2 | +| row2 | cell4 | + +para +` + output := `hello

      col1col 2
      row2cell4

      para` + doonezerotest(t, input, output) +} + var benchData, simpleData string func init() {