Skip to content

Commit

Permalink
Fix for edge cases
Browse files Browse the repository at this point in the history
fix for header

fix for thematic block

fix for code fence
  • Loading branch information
szktkfm committed Mar 31, 2024
1 parent fe80112 commit 7551d1a
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dist/

debug.log
.vscode/
./test.md
test.md
87 changes: 71 additions & 16 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,46 +78,64 @@ func (t *TableWriter) replaceTable(fp *os.File) []byte {
return b
}

// (?<=\|?\s*)-+
// ^\s*\|?\s*\-+

// TODO: delimeterの左寄せとか
// var tableDelimLeft = regexp.MustCompile(`^\s*\:\-+\s*$`)
// var tableDelimRight = regexp.MustCompile(`^\s*\-+\:\s*$`)
// var tableDelimCenter = regexp.MustCompile(`^\s*\:\-+\:\s*$`)
// var tableDelimNone = regexp.MustCompile(`^\s*\-+\s*$`)
var tableDelim = regexp.MustCompile(`^\s*\|?\s*\-+(\s*|\|?|\-+)*$`)
var (
tableDelimLeft = regexp.MustCompile(`^\s*\:\-+\s*$`)
tableDelimRight = regexp.MustCompile(`^\s*\-+\:\s*$`)
tableDelimCenter = regexp.MustCompile(`^\s*\:\-+\:\s*$`)
tableDelimNone = regexp.MustCompile(`^\s*\-+\s*$`)
thematicBreak = regexp.MustCompile(`^\s{0,3}((-\s*){3,}|(\*\s*){3,}|(_\s*){3,})\s*$`)
prefixSpace = regexp.MustCompile(`^\s{0,3}`)
// prefixSpace = regexp.MustCompile(`^\s{0,3}`)
fencedCodeBlock = regexp.MustCompile("^```|~~~.*$")
)

func (t *TableWriter) findSegment(fp io.Reader) {
// fmt.Println([]byte(fmt.Sprint(fp)))
scanner := bufio.NewScanner(fp)

var (
prevlen int
prevline string
pos int
inTable bool
start int
end int
)

var (
inTable bool
inCodeBlock bool
)

for scanner.Scan() {
l := scanner.Text()

// TODO: ```で始まって ~~~で閉じている場合
if isCodeFence(l) {
inCodeBlock = !inCodeBlock
}

if inTable {
if l == "" {
if isNewLine(l) || isThematicBreak(l) {
inTable = false
end = pos
}
}

if inCodeBlock {
pos += len(l) + 1
prevline = l
prevlen = len(l) + 1
continue
}

pos += len(l) + 1
if tableDelim.MatchString(l) {
if matchDelimiter(l) {
// header check
log.Debug("line", l)
if prevline == "" {
if isNewLine(prevline) {
continue
}
if len(strings.Split(trimPipe(prevline), "|")) <= len(strings.Split(trimPipe(l), "|")) {

// spaceがprefixに四つ以上ある場合はfalse
if isTableHeader(prevline, l) {
inTable = true
start = pos - len(l) - prevlen
} else {
Expand All @@ -138,7 +156,44 @@ func (t *TableWriter) findSegment(fp io.Reader) {
log.Debugf("start: %d, end: %d", start, end)
}

func isTableHeader(header string, delim string) bool {
return len(strings.Split(trimPipe(header), "|")) <= len(strings.Split(trimPipe(delim), "|"))
}

func isCodeFence(s string) bool {
return fencedCodeBlock.MatchString(s)
}

func isThematicBreak(s string) bool {
return thematicBreak.MatchString(s)
}

func isNewLine(s string) bool {
return s == ""
}
func matchDelimiter(s string) bool {
// TODO: prefixのスペース問題
// スペースが4つ以上の場合は捨てる
delim, _, _ := strings.Cut(
trimPipe(prefixSpace.ReplaceAllString(s, "")), "|")

if tableDelimLeft.MatchString(delim) ||
tableDelimRight.MatchString(delim) ||
tableDelimCenter.MatchString(delim) {
return true
}
if tableDelimNone.MatchString(delim) && strings.Contains(s, "|") {
return true
}

return false
}

func trimPipe(l string) string {
if len(l) == 0 {
return l
}
// spaceをtrimする
if l[0] == '|' {
l = l[1:]
}
Expand Down
12 changes: 7 additions & 5 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"testing"

"github.com/charmbracelet/log"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -40,13 +39,17 @@ func TestReplaceTable(t *testing.T) {
{
name: "Test Case 1",
src: "testdata/replace01.md",
wnt: "testdata/replace_want01.md",
wnt: "testdata/replace01_want.md",
},
// Add more test cases here
{
name: "Test Case 2",
src: "testdata/replace02.md",
wnt: "testdata/replace_want02.md",
wnt: "testdata/replace02_want.md",
},
{
name: "Test Case 3",
src: "testdata/replace03.md",
wnt: "testdata/replace03_want.md",
},
}

Expand All @@ -73,7 +76,6 @@ func testUtilReplaceTable(src, wnt string) ([]byte, []byte) {
tw.render(m.table)
got := tw.replaceTable(fp)

log.Debug(string(got))
fp2, _ := os.Open(wnt)
defer fp2.Close()
want, _ := io.ReadAll(fp2)
Expand Down
File renamed without changes.
19 changes: 11 additions & 8 deletions testdata/replace02.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

```bash
echo 'foobar' \
| grep \
∙ -E \
∙ --color 'foo'
| grep \
-E \
--color 'foo'
```

```bash
fzf --height 40% --layout reverse --info inline --border
```

| foo | bar |
| --- | --- |
| baz | bim |
----

```bash
echo 'foobar' \
| grep \
∙ --color 'foo'
```
Header
---------
foo
22 changes: 22 additions & 0 deletions testdata/replace02_want.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Title

```bash
echo 'foobar' \
| grep \
-E \
--color 'foo'
```

```bash
fzf --height 40% --layout reverse --info inline --border
```

| foo | bar | zoo |
| ----- | ---- | --- |
| bazoo | bim | |
| | TEST | |
----

Header
---------
foo
9 changes: 9 additions & 0 deletions testdata/replace03.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
| foo1 | bar |
| ---- | --- |
| baz | bim |

```
| foo | bar |
| --- | --- |
| baz | bim |
```
9 changes: 9 additions & 0 deletions testdata/replace03_want.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
| foo1 | bar |
| ---- | --- |
| bazz | bim |

```
| foo | bar |
| --- | --- |
| baz | bim |
```
17 changes: 0 additions & 17 deletions testdata/replace_want02.md

This file was deleted.

0 comments on commit 7551d1a

Please sign in to comment.