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.

block.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package org
  2. import (
  3. "regexp"
  4. "strings"
  5. "unicode"
  6. )
  7. type Block struct {
  8. Name string
  9. Parameters []string
  10. Children []Node
  11. }
  12. type Example struct {
  13. Children []Node
  14. }
  15. var exampleLineRegexp = regexp.MustCompile(`^(\s*):(\s(.*)|\s*$)`)
  16. var beginBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+BEGIN_(\w+)(.*)`)
  17. var endBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+END_(\w+)`)
  18. func lexBlock(line string) (token, bool) {
  19. if m := beginBlockRegexp.FindStringSubmatch(line); m != nil {
  20. return token{"beginBlock", len(m[1]), strings.ToUpper(m[2]), m}, true
  21. } else if m := endBlockRegexp.FindStringSubmatch(line); m != nil {
  22. return token{"endBlock", len(m[1]), strings.ToUpper(m[2]), m}, true
  23. }
  24. return nilToken, false
  25. }
  26. func lexExample(line string) (token, bool) {
  27. if m := exampleLineRegexp.FindStringSubmatch(line); m != nil {
  28. return token{"example", len(m[1]), m[3], m}, true
  29. }
  30. return nilToken, false
  31. }
  32. func isRawTextBlock(name string) bool { return name == "SRC" || name == "EXAMPLE" || name == "EXPORT" }
  33. func (d *Document) parseBlock(i int, parentStop stopFn) (int, Node) {
  34. t, start := d.tokens[i], i
  35. name, parameters := t.content, strings.Fields(t.matches[3])
  36. trim := trimIndentUpTo(d.tokens[i].lvl)
  37. stop := func(d *Document, i int) bool {
  38. return i >= len(d.tokens) || (d.tokens[i].kind == "endBlock" && d.tokens[i].content == name)
  39. }
  40. block, i := Block{name, parameters, nil}, i+1
  41. if isRawTextBlock(name) {
  42. rawText := ""
  43. for ; !stop(d, i); i++ {
  44. rawText += trim(d.tokens[i].matches[0]) + "\n"
  45. }
  46. block.Children = d.parseRawInline(rawText)
  47. } else {
  48. consumed, nodes := d.parseMany(i, stop)
  49. block.Children = nodes
  50. i += consumed
  51. }
  52. if i < len(d.tokens) && d.tokens[i].kind == "endBlock" && d.tokens[i].content == name {
  53. return i + 1 - start, block
  54. }
  55. return 0, nil
  56. }
  57. func (d *Document) parseExample(i int, parentStop stopFn) (int, Node) {
  58. example, start := Example{}, i
  59. for ; !parentStop(d, i) && d.tokens[i].kind == "example"; i++ {
  60. example.Children = append(example.Children, Text{d.tokens[i].content, true})
  61. }
  62. return i - start, example
  63. }
  64. func trimIndentUpTo(max int) func(string) string {
  65. return func(line string) string {
  66. i := 0
  67. for ; i < len(line) && i < max && unicode.IsSpace(rune(line[i])); i++ {
  68. }
  69. return line[i:]
  70. }
  71. }
  72. func (n Example) String() string { return orgWriter.WriteNodesAsString(n) }
  73. func (n Block) String() string { return orgWriter.WriteNodesAsString(n) }