summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Okay <cbeok@protonmail.com>2020-04-26 19:19:55 -0500
committerColin Okay <cbeok@protonmail.com>2020-04-26 19:19:55 -0500
commit9beb6ee949824c3aa19dc458af0590d0389da680 (patch)
treef2e14a8b309175775e3fa0065adc5109e894207f
parentb41fe0e4892d7ab455a3348f29b504d96cb572ea (diff)
org mode toc
-rw-r--r--examples/org-mode-toc.lisp26
1 files changed, 26 insertions, 0 deletions
diff --git a/examples/org-mode-toc.lisp b/examples/org-mode-toc.lisp
new file mode 100644
index 0000000..5355b02
--- /dev/null
+++ b/examples/org-mode-toc.lisp
@@ -0,0 +1,26 @@
+(defpackage :org-toc
+ (:use :cl :parzival))
+
+(in-package :org-toc)
+
+(<<def <toc-line<
+ (<<bind (<<counting+ (<<char #\*))
+ (lambda (count)
+ (<<map (lambda (matched)
+ (let ((toc-entry (concatenate 'string matched)))
+ (format nil "~a- [[~a][~a]]"
+ (make-string (* 2 count) :initial-element #\Space)
+ toc-entry
+ toc-entry)))
+ (<<and <whitespace<+ (<<+ <item<)))))
+ "Accepts a valid Org Mode heading and results in an org mode
+ list item with a link to that heading.")
+
+(defun print-toc-for (file &optional (output *standard-output*))
+ "Prints a table of contents for a given org file to the supplied stream."
+ (with-open-file (input file)
+ (loop
+ :for line = (read-line input nil nil)
+ :while line
+ :do (multiple-value-bind (entry success-p) (parse line <toc-line< t)
+ (when success-p (format output "~a~%" entry))))))