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

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