From 91e5269fb0f873f91c9a01e98f32de15ca49440f Mon Sep 17 00:00:00 2001 From: yuin Date: Thu, 30 Jul 2020 22:33:42 +0900 Subject: [PATCH] Fixes #148 --- extension/_test/table.txt | 18 ++++++++++++++ extension/table.go | 51 ++++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/extension/_test/table.txt b/extension/_test/table.txt index 946d38a..42e977a 100644 --- a/extension/_test/table.txt +++ b/extension/_test/table.txt @@ -235,3 +235,21 @@ foo|bar //= = = = = = = = = = = = = = = = = = = = = = = =// + + +11: Tables can interrupt paragraph +//- - - - - - - - -// +**xxx** +| hello | hi | +| :----: | :----:| +//- - - - - - - - -// +

xxx

+ + + + + + + +
hellohi
+//= = = = = = = = = = = = = = = = = = = = = = = =// diff --git a/extension/table.go b/extension/table.go index 081c3c8..f0e994e 100644 --- a/extension/table.go +++ b/extension/table.go @@ -113,7 +113,15 @@ func WithTableCellAlignMethod(a TableCellAlignMethod) TableOption { return &withTableCellAlignMethod{a} } -var tableDelimRegexp = regexp.MustCompile(`^[\s\-\|\:]+$`) +func isTableDelim(bs []byte) bool { + for _, b := range bs { + if !(util.IsSpace(b) || b == '-' || b == '|' || b == ':') { + return false + } + } + return true +} + var tableDelimLeft = regexp.MustCompile(`^\s*\:\-+\s*$`) var tableDelimRight = regexp.MustCompile(`^\s*\-+\:\s*$`) var tableDelimCenter = regexp.MustCompile(`^\s*\:\-+\:\s*$`) @@ -135,22 +143,31 @@ func (b *tableParagraphTransformer) Transform(node *gast.Paragraph, reader text. if lines.Len() < 2 { return } - alignments := b.parseDelimiter(lines.At(1), reader) - if alignments == nil { - return - } - header := b.parseRow(lines.At(0), alignments, true, reader) - if header == nil || len(alignments) != header.ChildCount() { - return - } - table := ast.NewTable() - table.Alignments = alignments - table.AppendChild(table, ast.NewTableHeader(header)) - for i := 2; i < lines.Len(); i++ { - table.AppendChild(table, b.parseRow(lines.At(i), alignments, false, reader)) + for i := 1; i < lines.Len(); i++ { + alignments := b.parseDelimiter(lines.At(i), reader) + if alignments == nil { + continue + } + header := b.parseRow(lines.At(i-1), alignments, true, reader) + if header == nil || len(alignments) != header.ChildCount() { + return + } + table := ast.NewTable() + table.Alignments = alignments + table.AppendChild(table, ast.NewTableHeader(header)) + for j := i + 1; j < lines.Len(); j++ { + table.AppendChild(table, b.parseRow(lines.At(j), alignments, false, reader)) + } + node.Lines().SetSliced(0, i-1) + node.Parent().InsertAfter(node.Parent(), node, table) + if node.Lines().Len() == 0 { + node.Parent().RemoveChild(node.Parent(), node) + } else { + last := node.Lines().At(i - 2) + last.Stop = last.Stop - 1 // trim last newline(\n) + node.Lines().Set(i-2, last) + } } - node.Parent().InsertBefore(node.Parent(), node, table) - node.Parent().RemoveChild(node.Parent(), node) } func (b *tableParagraphTransformer) parseRow(segment text.Segment, alignments []ast.Alignment, isHeader bool, reader text.Reader) *ast.TableRow { @@ -198,7 +215,7 @@ func (b *tableParagraphTransformer) parseRow(segment text.Segment, alignments [] func (b *tableParagraphTransformer) parseDelimiter(segment text.Segment, reader text.Reader) []ast.Alignment { line := segment.Value(reader.Source()) - if !tableDelimRegexp.Match(line) { + if !isTableDelim(line) { return nil } cols := bytes.Split(line, []byte{'|'})