Skip to content

Instantly share code, notes, and snippets.

@PuercoPop
Last active August 29, 2015 14:14
Show Gist options
  • Save PuercoPop/939b2fbd22155bfae3f4 to your computer and use it in GitHub Desktop.
Save PuercoPop/939b2fbd22155bfae3f4 to your computer and use it in GitHub Desktop.
(require 'creole)
(require 'cl)
(defun creole-extract-heading-level (heading)
(string-to-number (save-match-data
(string-match
(rx "heading" (group (+ digit)))
heading)
(match-string 1 heading))))
(creole-extract-heading-level "heading2")text
;; Here
;; emacs -Q -L . -l creole-to-md --eval '(creole-md "ViaTodo MVP.wiki")' -f sly
(creole-structure (creole-tokenize (get-buffer "ViaTodo MVP.wiki")))
(defun creole/list->md (type items)
(cl-loop
for item in items
do
(cond
((listp item)
(creole/list->md (car item) (cdr item)))
(t (insert (format "-%s\n" item))))))
(defun creole/heading->md (heading-cons)
(let* ((h-str (symbol-name (car heading-cons)))
(level (save-match-data
(string-match
(rx "heading" (group (+ digit)))
h-str)
(match-string 1 h-str)))
(h-text (if (listp (cdr heading-cons))
(cadr heading-cons)
(cdr heading-cons))))
(dotimes (i (string-to-number level))
(insert "#"))
(insert h-text)
(insert "\n")))
(defun creole-md (docbuf &optional md-buffer)
(let ((result-buffer (or md-buffer
(get-buffer-create "*md-buffer*"))))
(with-current-buffer result-buffer)
(cl-loop for element in (creole-structure (creole-tokenize docbuf))
do
(let ((syntax (car element)))
(ecase syntax
((ul ol)
(creole/list->md syntax (cdr element)))
((heading1 heading2 heading3 heading4 heading5)
(creole/heading->md element))
(preformatted (format "```\n%s```\n" (cdr element)))
(hr (insert "\n"))
(para (insert (format "\n%s\n" (cdr element)))))))))
(provide 'creole-to-md)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment