-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdtest.go_
157 lines (125 loc) · 3.77 KB
/
mdtest.go_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Package main は、ドキュメント内にある qiitan スクリプトをコードブロックごとにテスト実行します。
1. 引数で渡されたディレクトリ以下にある Markdown ドキュメントの一覧を取得し、各ドキュメントごとに走査します。
2. コードブロックの言語シンタックスが `go qiitan` の場合に実行され、ステータス `0`(ゼロ)で終了した場合にパスします。
3. コードブロック内に `Output:` コメント行があった場合、続くコメント行と実際の標準出力の内容を比較し、同じであった場合にパスします。
*/
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"github.com/KEINOS/go-utiles/util"
"github.com/pkg/errors"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
)
func main() {
flag.Parse()
listDirSearch := flag.Args()
util.ExitOnErr(Run(listDirSearch))
}
// GetListPathFile returns a file path list of the markdown files to test.
func GetListPathFile(listDirSearch []string) ([]string, error) {
lenDir := len(listDirSearch)
if lenDir == 0 {
return []string{}, errors.New("the directory to search is not specified")
}
pathFileHolder := make([]string, lenDir)
for _, pathDir := range listDirSearch {
listFile, err := SearchDir(pathDir)
if err != nil {
return []string{}, errors.Wrap(err, "failed to search directory")
}
pathFileHolder = append(pathFileHolder, listFile...)
}
return pathFileHolder, nil
}
// GetListScript returns a list of markdown code blocks found in pathFile.
func GetListScript(pathFile string) (result []string, err error) {
fileByte, err := os.ReadFile(pathFile)
if err != nil {
return []string{}, errors.Wrap(err, "failed to read file")
}
markdown := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(
parser.WithBlockParsers(),
parser.WithInlineParsers(),
),
)
markdownParser := markdown.Parser()
textReader := text.NewReader(fileByte)
astNodes := markdownParser.Parse(textReader)
level := int(0)
ast.Walk(astNodes, func(currNode ast.Node, entering bool) (ast.WalkStatus, error) {
var (
err error
typeDesc string
enteringStats string
)
status := ast.WalkStatus(ast.WalkContinue)
nodeKind := currNode.Kind().String()
if entering {
enteringStats = "IN :"
level++
} else {
enteringStats = "OUT:"
level--
}
//textFound := util.FmtStructPretty(currNode)
typeDesc = fmt.Sprintf("%T", currNode)
textFound := util.FmtStructPretty(currNode)
switch currNode.(type) {
case *ast.Document:
typeDesc = "it is a doc"
case *ast.Text:
typeDesc = "it is a text"
case *ast.Paragraph:
typeDesc = "it is a Paragraph"
case *ast.FencedCodeBlock:
typeDesc = "it is a FencedCodeBlock"
case *ast.CodeBlock:
typeDesc = "it is a inline"
default:
typeDesc = fmt.Sprintf("Unused Type: %T", currNode)
}
msg := fmt.Sprintf(
"%v:%v Kind: %v, Desc: %v, Text: %v",
level,
enteringStats,
nodeKind,
typeDesc,
textFound,
)
result = append(result, msg)
return status, err
})
return result, nil
}
// Run is the actual main function of this package.
func Run(listDirSearch []string) error {
// Get lists of markdown files
ListPathFile, err := GetListPathFile(listDirSearch)
if err != nil {
return err
}
sort.Strings(ListPathFile)
fmt.Println(ListPathFile)
return nil
}
func RunScript(script string) error {
return nil
}
func SearchDir(pathDir string) ([]string, error) {
pathDir, err := filepath.Abs(pathDir)
if err != nil || !util.IsDir(pathDir) {
return []string{}, errors.New("dir not exists: " + pathDir)
}
return []string{pathDir}, nil
}