Skip to content

Commit

Permalink
Add simple table parser
Browse files Browse the repository at this point in the history
  • Loading branch information
chonla committed Nov 4, 2018
1 parent 9a7f257 commit 4b21466
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Go Markdown Parser

Markdown parser engine written in go.

## Supports

* H1 (#)
* H2 (##)
* H3 (###)
* H4 (####)
* H5 (#####)
* H6 (######)
* Code block starting with ``` and ~~~
* Paragraph

## To Do

* Table
* Blockquote
62 changes: 62 additions & 0 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"regexp"
"strings"
)

// ElementHierarchy provides hierachical structure
Expand Down Expand Up @@ -117,3 +118,64 @@ func tryCode(block string) (string, bool) {
}
return "", false
}

func tryTable(block string) ([][]string, bool) {
output := [][]string{}

lines := strings.Split(block, "\n")
if len(lines) < 3 {
return nil, false
}

// check header separator
// header separator for one column
patSep := "^|\\s+:?---+?\\s+|$"
reSep := regexp.MustCompile(patSep)
if !reSep.MatchString(lines[1]) {
// header separator does not present
return nil, false
}

colCount := columnCount(lines[1])

if colCount != columnCount(lines[0]) {
// header and column count does not match
return nil, false
}

row := []string{}
for i := 0; i < colCount; i++ {
row = append(row, getCellValue(i, lines[0]))
}
output = append(output, row)

for i, n := 2, len(lines); i < n; i++ {
row = []string{}
for j := 0; j < colCount; j++ {
row = append(row, getCellValue(j, lines[i]))
}
output = append(output, row)
}

return output, true
}

func getCellValue(index int, line string) string {
cols := strings.Split(line, "|")
if cols[0] == "" { // detect left boundary pipe
cols = cols[1:]
}
if cols[len(cols)-1] == "" { // detect right boundary pipe
cols = cols[0 : len(cols)-1]
}

return strings.TrimSpace(cols[index])
}

func columnCount(line string) int {
colCount := strings.Count(line, "|") - 1
if line[0] != '|' || line[len(line)-1] != '|' {
colCount++
}
return colCount
}
16 changes: 16 additions & 0 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,19 @@ func TestTryAlternativeCodeBlock(t *testing.T) {
assert.True(t, success)
assert.Equal(t, "Content\nWith\nNew Line", text)
}

func TestTry1ColumnTable(t *testing.T) {
content := "| Header |\n| --- |\n| Body |"

text, success := tryTable(content)

assert.True(t, success)
assert.Equal(t, [][]string{
[]string{
"Header",
},
[]string{
"Body",
},
}, text)
}
10 changes: 10 additions & 0 deletions tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,13 @@ func TestTokenizeSimpleDataWith1AlternativeCodeBlock(t *testing.T) {

assert.Equal(t, expected, result)
}

func TestTokenizeTableData(t *testing.T) {
content := "| Header |\n| - |\n| Body |"
expected := []string{"| Header |\n| - |\n| Body |"}
tokenizer := NewTokenizer()

result := tokenizer.Tokenize(content)

assert.Equal(t, expected, result)
}

0 comments on commit 4b21466

Please sign in to comment.