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.

document.go 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Package org is an Org mode syntax processor.
  2. //
  3. // It parses plain text into an AST and can export it as HTML or pretty printed Org mode syntax.
  4. // Further export formats can be defined using the Writer interface.
  5. //
  6. // You probably want to start with something like this:
  7. // input := strings.NewReader("Your Org mode input")
  8. // html, err := org.New().Parse(input, "./").Write(org.NewHTMLWriter())
  9. // if err != nil {
  10. // log.Fatalf("Something went wrong: %s", err)
  11. // }
  12. // log.Print(html)
  13. package org
  14. import (
  15. "bufio"
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "log"
  20. "os"
  21. "strings"
  22. )
  23. type Configuration struct {
  24. MaxEmphasisNewLines int // Maximum number of newlines inside an emphasis. See org-emphasis-regexp-components newline.
  25. AutoLink bool // Try to convert text passages that look like hyperlinks into hyperlinks.
  26. DefaultSettings map[string]string // Default values for settings that are overriden by setting the same key in BufferSettings.
  27. Log *log.Logger // Log is used to print warnings during parsing.
  28. ReadFile func(filename string) ([]byte, error) // ReadFile is used to read e.g. #+INCLUDE files.
  29. }
  30. // Document contains the parsing results and a pointer to the Configuration.
  31. type Document struct {
  32. *Configuration
  33. Path string // Path of the file containing the parse input - used to resolve relative paths during parsing (e.g. INCLUDE).
  34. tokens []token
  35. Nodes []Node
  36. NamedNodes map[string]Node
  37. Outline Outline // Outline is a Table Of Contents for the document and contains all sections (headline + content).
  38. BufferSettings map[string]string // Settings contains all settings that were parsed from keywords.
  39. Error error
  40. }
  41. // Node represents a parsed node of the document.
  42. type Node interface {
  43. String() string // String returns the pretty printed Org mode string for the node (see OrgWriter).
  44. }
  45. type lexFn = func(line string) (t token, ok bool)
  46. type parseFn = func(*Document, int, stopFn) (int, Node)
  47. type stopFn = func(*Document, int) bool
  48. type token struct {
  49. kind string
  50. lvl int
  51. content string
  52. matches []string
  53. }
  54. var lexFns = []lexFn{
  55. lexHeadline,
  56. lexDrawer,
  57. lexBlock,
  58. lexList,
  59. lexTable,
  60. lexHorizontalRule,
  61. lexKeywordOrComment,
  62. lexFootnoteDefinition,
  63. lexExample,
  64. lexText,
  65. }
  66. var nilToken = token{"nil", -1, "", nil}
  67. var orgWriter = NewOrgWriter()
  68. // New returns a new Configuration with (hopefully) sane defaults.
  69. func New() *Configuration {
  70. return &Configuration{
  71. AutoLink: true,
  72. MaxEmphasisNewLines: 1,
  73. DefaultSettings: map[string]string{
  74. "TODO": "TODO | DONE",
  75. "EXCLUDE_TAGS": "noexport",
  76. "OPTIONS": "toc:t <:t e:t f:t pri:t todo:t tags:t",
  77. },
  78. Log: log.New(os.Stderr, "go-org: ", 0),
  79. ReadFile: ioutil.ReadFile,
  80. }
  81. }
  82. // String returns the pretty printed Org mode string for the given nodes (see OrgWriter).
  83. func String(nodes []Node) string { return orgWriter.WriteNodesAsString(nodes...) }
  84. // Write is called after with an instance of the Writer interface to export a parsed Document into another format.
  85. func (d *Document) Write(w Writer) (out string, err error) {
  86. defer func() {
  87. if recovered := recover(); recovered != nil {
  88. err = fmt.Errorf("could not write output: %s", recovered)
  89. }
  90. }()
  91. if d.Error != nil {
  92. return "", d.Error
  93. } else if d.Nodes == nil {
  94. return "", fmt.Errorf("could not write output: parse was not called")
  95. }
  96. w.Before(d)
  97. WriteNodes(w, d.Nodes...)
  98. w.After(d)
  99. return w.String(), err
  100. }
  101. // Parse parses the input into an AST (and some other helpful fields like Outline).
  102. // To allow method chaining, errors are stored in document.Error rather than being returned.
  103. func (c *Configuration) Parse(input io.Reader, path string) (d *Document) {
  104. outlineSection := &Section{}
  105. d = &Document{
  106. Configuration: c,
  107. Outline: Outline{outlineSection, outlineSection, 0},
  108. BufferSettings: map[string]string{},
  109. NamedNodes: map[string]Node{},
  110. Path: path,
  111. }
  112. defer func() {
  113. if recovered := recover(); recovered != nil {
  114. d.Error = fmt.Errorf("could not parse input: %v", recovered)
  115. }
  116. }()
  117. if d.tokens != nil {
  118. d.Error = fmt.Errorf("parse was called multiple times")
  119. }
  120. d.tokenize(input)
  121. _, nodes := d.parseMany(0, func(d *Document, i int) bool { return i >= len(d.tokens) })
  122. d.Nodes = nodes
  123. return d
  124. }
  125. // Silent disables all logging of warnings during parsing.
  126. func (c *Configuration) Silent() *Configuration {
  127. c.Log = log.New(ioutil.Discard, "", 0)
  128. return c
  129. }
  130. func (d *Document) tokenize(input io.Reader) {
  131. d.tokens = []token{}
  132. scanner := bufio.NewScanner(input)
  133. for scanner.Scan() {
  134. d.tokens = append(d.tokens, tokenize(scanner.Text()))
  135. }
  136. if err := scanner.Err(); err != nil {
  137. d.Error = fmt.Errorf("could not tokenize input: %s", err)
  138. }
  139. }
  140. // Get returns the value for key in BufferSettings or DefaultSettings if key does not exist in the former
  141. func (d *Document) Get(key string) string {
  142. if v, ok := d.BufferSettings[key]; ok {
  143. return v
  144. }
  145. if v, ok := d.DefaultSettings[key]; ok {
  146. return v
  147. }
  148. return ""
  149. }
  150. // GetOption returns the value associated to the export option key
  151. // Currently supported options:
  152. // - < (export timestamps)
  153. // - e (export org entities)
  154. // - f (export footnotes)
  155. // - toc (export table of content)
  156. // - todo (export headline todo status)
  157. // - pri (export headline priority)
  158. // - tags (export headline tags)
  159. // see https://orgmode.org/manual/Export-settings.html for more information
  160. func (d *Document) GetOption(key string) bool {
  161. get := func(settings map[string]string) string {
  162. for _, field := range strings.Fields(settings["OPTIONS"]) {
  163. if strings.HasPrefix(field, key+":") {
  164. return field[len(key)+1:]
  165. }
  166. }
  167. return ""
  168. }
  169. value := get(d.BufferSettings)
  170. if value == "" {
  171. value = get(d.DefaultSettings)
  172. }
  173. switch value {
  174. case "t":
  175. return true
  176. case "nil":
  177. return false
  178. default:
  179. d.Log.Printf("Bad value for export option %s (%s)", key, value)
  180. return false
  181. }
  182. }
  183. func (d *Document) parseOne(i int, stop stopFn) (consumed int, node Node) {
  184. switch d.tokens[i].kind {
  185. case "unorderedList", "orderedList":
  186. consumed, node = d.parseList(i, stop)
  187. case "tableRow", "tableSeparator":
  188. consumed, node = d.parseTable(i, stop)
  189. case "beginBlock":
  190. consumed, node = d.parseBlock(i, stop)
  191. case "beginDrawer":
  192. consumed, node = d.parseDrawer(i, stop)
  193. case "text":
  194. consumed, node = d.parseParagraph(i, stop)
  195. case "example":
  196. consumed, node = d.parseExample(i, stop)
  197. case "horizontalRule":
  198. consumed, node = d.parseHorizontalRule(i, stop)
  199. case "comment":
  200. consumed, node = d.parseComment(i, stop)
  201. case "keyword":
  202. consumed, node = d.parseKeyword(i, stop)
  203. case "headline":
  204. consumed, node = d.parseHeadline(i, stop)
  205. case "footnoteDefinition":
  206. consumed, node = d.parseFootnoteDefinition(i, stop)
  207. }
  208. if consumed != 0 {
  209. return consumed, node
  210. }
  211. d.Log.Printf("Could not parse token %#v: Falling back to treating it as plain text.", d.tokens[i])
  212. m := plainTextRegexp.FindStringSubmatch(d.tokens[i].matches[0])
  213. d.tokens[i] = token{"text", len(m[1]), m[2], m}
  214. return d.parseOne(i, stop)
  215. }
  216. func (d *Document) parseMany(i int, stop stopFn) (int, []Node) {
  217. start, nodes := i, []Node{}
  218. for i < len(d.tokens) && !stop(d, i) {
  219. consumed, node := d.parseOne(i, stop)
  220. i += consumed
  221. nodes = append(nodes, node)
  222. }
  223. return i - start, nodes
  224. }
  225. func (d *Document) addHeadline(headline *Headline) int {
  226. current := &Section{Headline: headline}
  227. d.Outline.last.add(current)
  228. d.Outline.count++
  229. d.Outline.last = current
  230. return d.Outline.count
  231. }
  232. func tokenize(line string) token {
  233. for _, lexFn := range lexFns {
  234. if token, ok := lexFn(line); ok {
  235. return token
  236. }
  237. }
  238. panic(fmt.Sprintf("could not lex line: %s", line))
  239. }