diff options
author | Colin Okay <cbeok@protonmail.com> | 2020-04-26 19:19:55 -0500 |
---|---|---|
committer | Colin Okay <cbeok@protonmail.com> | 2020-04-26 19:19:55 -0500 |
commit | 9beb6ee949824c3aa19dc458af0590d0389da680 (patch) | |
tree | f2e14a8b309175775e3fa0065adc5109e894207f | |
parent | b41fe0e4892d7ab455a3348f29b504d96cb572ea (diff) |
org mode toc
-rw-r--r-- | examples/org-mode-toc.lisp | 26 |
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)))))) |