-
Notifications
You must be signed in to change notification settings - Fork 8
/
node_generator.go
56 lines (46 loc) · 969 Bytes
/
node_generator.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
package gtree
import (
"errors"
"fmt"
md "github.com/ddddddO/gtree/markdown"
)
type nodeGenerator struct {
parser *md.Parser
}
func newNodeGenerator() *nodeGenerator {
return &nodeGenerator{
parser: md.NewParser(),
}
}
var (
errEmptyText = errors.New("empty text")
)
type inputFormatError struct {
// n uint64 // markdownの行数もあとで
row string
}
func (ie *inputFormatError) Error() string {
return fmt.Sprintf("incorrect input format: %s", ie.row)
}
func (ng *nodeGenerator) generate(row string, idx uint) (*Node, error) {
markdown, err := ng.parser.Parse(row)
if err != nil {
return nil, ng.handleErr(err, row)
}
return newNode(
markdown.Text(),
markdown.Hierarchy(),
idx,
), nil
}
func (*nodeGenerator) handleErr(err error, row string) error {
switch err {
case md.ErrEmptyText:
return errEmptyText
case md.ErrIncorrectFormat:
return &inputFormatError{row: row}
case md.ErrBlankLine:
return nil
}
return err
}