You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.6 KiB

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