blockquote.go (1499B)
1 package parser 2 3 import ( 4 "github.com/yuin/goldmark/ast" 5 "github.com/yuin/goldmark/text" 6 "github.com/yuin/goldmark/util" 7 ) 8 9 type blockquoteParser struct { 10 } 11 12 var defaultBlockquoteParser = &blockquoteParser{} 13 14 // NewBlockquoteParser returns a new BlockParser that 15 // parses blockquotes. 16 func NewBlockquoteParser() BlockParser { 17 return defaultBlockquoteParser 18 } 19 20 func (b *blockquoteParser) process(reader text.Reader) bool { 21 line, _ := reader.PeekLine() 22 w, pos := util.IndentWidth(line, reader.LineOffset()) 23 if w > 3 || pos >= len(line) || line[pos] != '>' { 24 return false 25 } 26 pos++ 27 if pos >= len(line) || line[pos] == '\n' { 28 reader.Advance(pos) 29 return true 30 } 31 if line[pos] == ' ' || line[pos] == '\t' { 32 pos++ 33 } 34 reader.Advance(pos) 35 if line[pos-1] == '\t' { 36 reader.SetPadding(2) 37 } 38 return true 39 } 40 41 func (b *blockquoteParser) Trigger() []byte { 42 return []byte{'>'} 43 } 44 45 func (b *blockquoteParser) Open(parent ast.Node, reader text.Reader, pc Context) (ast.Node, State) { 46 if b.process(reader) { 47 return ast.NewBlockquote(), HasChildren 48 } 49 return nil, NoChildren 50 } 51 52 func (b *blockquoteParser) Continue(node ast.Node, reader text.Reader, pc Context) State { 53 if b.process(reader) { 54 return Continue | HasChildren 55 } 56 return Close 57 } 58 59 func (b *blockquoteParser) Close(node ast.Node, reader text.Reader, pc Context) { 60 // nothing to do 61 } 62 63 func (b *blockquoteParser) CanInterruptParagraph() bool { 64 return true 65 } 66 67 func (b *blockquoteParser) CanAcceptIndentedLine() bool { 68 return false 69 }