Created
June 10, 2016 01:43
-
-
Save badri/4d1bf1f0a24e8db5065e31946355cfe3 to your computer and use it in GitHub Desktop.
leanpub-export
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun leanpub-export () | |
"Export buffer to a Leanpub book." | |
(interactive) | |
(if (file-exists-p "./Book.txt") | |
(delete-file "./Book.txt")) | |
(if (file-exists-p "./Sample.txt") | |
(delete-file "./Sample.txt")) | |
(org-map-entries | |
(lambda () | |
(let* ((level (nth 1 (org-heading-components))) | |
(tags (org-get-tags)) | |
(title (or (nth 4 (org-heading-components)) "")) | |
(book-slug (org-entry-get (point) "TITLE")) | |
(filename | |
(or (org-entry-get (point) "EXPORT_FILE_NAME") (concat (replace-regexp-in-string " " "-" (downcase title)) ".md")))) | |
(when (= level 1) ;; export only first level entries | |
;; add to Sample book if "sample" tag is found. | |
(when (or (member "sample" tags) (string-prefix-p "frontmatter" filename) (string-prefix-p "mainmatter" filename)) | |
(append-to-file (concat filename "\n\n") nil "./Sample.txt")) | |
(append-to-file (concat filename "\n\n") nil "./Book.txt") | |
;; set filename only if the property is missing | |
(or (org-entry-get (point) "EXPORT_FILE_NAME") (org-entry-put (point) "EXPORT_FILE_NAME" filename)) | |
(org-leanpub-export-to-markdown nil 1 nil)))) "-noexport") (org-save-all-org-buffers) | |
nil nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment