Skip to content

Commit

Permalink
Fixes #148
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Jul 30, 2020
1 parent bd58441 commit 91e5269
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
18 changes: 18 additions & 0 deletions extension/_test/table.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,21 @@ foo|bar
</tbody>
</table>
//= = = = = = = = = = = = = = = = = = = = = = = =//


11: Tables can interrupt paragraph
//- - - - - - - - -//
**xxx**
| hello | hi |
| :----: | :----:|
//- - - - - - - - -//
<p><strong>xxx</strong></p>
<table>
<thead>
<tr>
<th align="center">hello</th>
<th align="center">hi</th>
</tr>
</thead>
</table>
//= = = = = = = = = = = = = = = = = = = = = = = =//
51 changes: 34 additions & 17 deletions extension/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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*$`)
Expand All @@ -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 {
Expand Down Expand Up @@ -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{'|'})
Expand Down

0 comments on commit 91e5269

Please sign in to comment.