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.

list.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package org
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "unicode"
  7. )
  8. type List struct {
  9. Kind string
  10. Items []Node
  11. }
  12. type ListItem struct {
  13. Bullet string
  14. Status string
  15. Children []Node
  16. }
  17. type DescriptiveListItem struct {
  18. Bullet string
  19. Status string
  20. Term []Node
  21. Details []Node
  22. }
  23. var unorderedListRegexp = regexp.MustCompile(`^(\s*)([+*-])(\s+(.*)|$)`)
  24. var orderedListRegexp = regexp.MustCompile(`^(\s*)(([0-9]+|[a-zA-Z])[.)])(\s+(.*)|$)`)
  25. var descriptiveListItemRegexp = regexp.MustCompile(`\s::(\s|$)`)
  26. var listItemStatusRegexp = regexp.MustCompile(`\[( |X|-)\]\s`)
  27. func lexList(line string) (token, bool) {
  28. if m := unorderedListRegexp.FindStringSubmatch(line); m != nil {
  29. return token{"unorderedList", len(m[1]), m[4], m}, true
  30. } else if m := orderedListRegexp.FindStringSubmatch(line); m != nil {
  31. return token{"orderedList", len(m[1]), m[5], m}, true
  32. }
  33. return nilToken, false
  34. }
  35. func isListToken(t token) bool {
  36. return t.kind == "unorderedList" || t.kind == "orderedList"
  37. }
  38. func listKind(t token) (string, string) {
  39. kind := ""
  40. switch bullet := t.matches[2]; {
  41. case bullet == "*" || bullet == "+" || bullet == "-":
  42. kind = "unordered"
  43. case unicode.IsLetter(rune(bullet[0])), unicode.IsDigit(rune(bullet[0])):
  44. kind = "ordered"
  45. default:
  46. panic(fmt.Sprintf("bad list bullet '%s': %#v", bullet, t))
  47. }
  48. if descriptiveListItemRegexp.MatchString(t.content) {
  49. return kind, "descriptive"
  50. }
  51. return kind, kind
  52. }
  53. func (d *Document) parseList(i int, parentStop stopFn) (int, Node) {
  54. start, lvl := i, d.tokens[i].lvl
  55. listMainKind, kind := listKind(d.tokens[i])
  56. list := List{Kind: kind}
  57. stop := func(*Document, int) bool {
  58. if parentStop(d, i) || d.tokens[i].lvl != lvl || !isListToken(d.tokens[i]) {
  59. return true
  60. }
  61. itemMainKind, _ := listKind(d.tokens[i])
  62. return itemMainKind != listMainKind
  63. }
  64. for !stop(d, i) {
  65. consumed, node := d.parseListItem(list, i, parentStop)
  66. i += consumed
  67. list.Items = append(list.Items, node)
  68. }
  69. return i - start, list
  70. }
  71. func (d *Document) parseListItem(l List, i int, parentStop stopFn) (int, Node) {
  72. start, nodes, bullet := i, []Node{}, d.tokens[i].matches[2]
  73. minIndent, dterm, content, status := d.tokens[i].lvl+len(bullet), "", d.tokens[i].content, ""
  74. if m := listItemStatusRegexp.FindStringSubmatch(content); m != nil {
  75. status, content = m[1], content[len("[ ] "):]
  76. }
  77. if l.Kind == "descriptive" {
  78. if m := descriptiveListItemRegexp.FindStringIndex(content); m != nil {
  79. dterm, content = content[:m[0]], content[m[1]:]
  80. }
  81. }
  82. d.tokens[i] = tokenize(strings.Repeat(" ", minIndent) + content)
  83. stop := func(d *Document, i int) bool {
  84. if parentStop(d, i) {
  85. return true
  86. }
  87. t := d.tokens[i]
  88. return t.lvl < minIndent && !(t.kind == "text" && t.content == "")
  89. }
  90. for !stop(d, i) && (i <= start+1 || !isSecondBlankLine(d, i)) {
  91. consumed, node := d.parseOne(i, stop)
  92. i += consumed
  93. nodes = append(nodes, node)
  94. }
  95. if l.Kind == "descriptive" {
  96. return i - start, DescriptiveListItem{bullet, status, d.parseInline(dterm), nodes}
  97. }
  98. return i - start, ListItem{bullet, status, nodes}
  99. }
  100. func (n List) String() string { return orgWriter.WriteNodesAsString(n) }
  101. func (n ListItem) String() string { return orgWriter.WriteNodesAsString(n) }
  102. func (n DescriptiveListItem) String() string { return orgWriter.WriteNodesAsString(n) }