blockquote.go 1.5 KB

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