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.

paragraph.go 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package org
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. type Paragraph struct{ Children []Node }
  7. type HorizontalRule struct{}
  8. var horizontalRuleRegexp = regexp.MustCompile(`^(\s*)-{5,}\s*$`)
  9. var plainTextRegexp = regexp.MustCompile(`^(\s*)(.*)`)
  10. func lexText(line string) (token, bool) {
  11. if m := plainTextRegexp.FindStringSubmatch(line); m != nil {
  12. return token{"text", len(m[1]), m[2], m}, true
  13. }
  14. return nilToken, false
  15. }
  16. func lexHorizontalRule(line string) (token, bool) {
  17. if m := horizontalRuleRegexp.FindStringSubmatch(line); m != nil {
  18. return token{"horizontalRule", len(m[1]), "", m}, true
  19. }
  20. return nilToken, false
  21. }
  22. func (d *Document) parseParagraph(i int, parentStop stopFn) (int, Node) {
  23. lines, start := []string{d.tokens[i].content}, i
  24. i++
  25. stop := func(d *Document, i int) bool {
  26. return parentStop(d, i) || d.tokens[i].kind != "text" || d.tokens[i].content == ""
  27. }
  28. for ; !stop(d, i); i++ {
  29. lines = append(lines, d.tokens[i].content)
  30. }
  31. consumed := i - start
  32. return consumed, Paragraph{d.parseInline(strings.Join(lines, "\n"))}
  33. }
  34. func (d *Document) parseHorizontalRule(i int, parentStop stopFn) (int, Node) {
  35. return 1, HorizontalRule{}
  36. }
  37. func (n Paragraph) String() string { return orgWriter.WriteNodesAsString(n) }
  38. func (n HorizontalRule) String() string { return orgWriter.WriteNodesAsString(n) }