1package parser
2
3import (
4 "github.com/yuin/goldmark/ast"
5 "github.com/yuin/goldmark/text"
6 "github.com/yuin/goldmark/util"
7)
8
9type paragraphParser struct {
10}
11
12var defaultParagraphParser = ¶graphParser{}
13
14// NewParagraphParser returns a new BlockParser that
15// parses paragraphs.
16func NewParagraphParser() BlockParser {
17 return defaultParagraphParser
18}
19
20func (b *paragraphParser) Trigger() []byte {
21 return nil
22}
23
24func (b *paragraphParser) Open(parent ast.Node, reader text.Reader, pc Context) (ast.Node, State) {
25 line, segment := reader.PeekLine()
26 if util.IsBlank(line) {
27 return nil, NoChildren
28 }
29 node := ast.NewParagraph()
30 node.Lines().Append(segment)
31 reader.AdvanceToEOL()
32 return node, NoChildren
33}
34
35func (b *paragraphParser) Continue(node ast.Node, reader text.Reader, pc Context) State {
36 line, segment := reader.PeekLine()
37 if util.IsBlank(line) {
38 return Close
39 }
40 node.Lines().Append(segment)
41 reader.AdvanceToEOL()
42 return Continue | NoChildren
43}
44
45func (b *paragraphParser) Close(node ast.Node, reader text.Reader, pc Context) {
46 lines := node.Lines()
47 if lines.Len() != 0 {
48 // trim leading spaces
49 for i := range lines.Len() {
50 l := lines.At(i)
51 lines.Set(i, l.TrimLeftSpace(reader.Source()))
52 }
53
54 // trim trailing spaces
55 length := lines.Len()
56 lastLine := node.Lines().At(length - 1)
57 node.Lines().Set(length-1, lastLine.TrimRightSpace(reader.Source()))
58 }
59 if lines.Len() == 0 {
60 node.Parent().RemoveChild(node.Parent(), node)
61 return
62 }
63}
64
65func (b *paragraphParser) CanInterruptParagraph() bool {
66 return false
67}
68
69func (b *paragraphParser) CanAcceptIndentedLine() bool {
70 return false
71}