263 lines
9.8 KiB
EmacsLisp
263 lines
9.8 KiB
EmacsLisp
;;; ronny/core.el --- Core reusable functions -*- lexical-binding: t; -*-
|
|
|
|
;; commands to export properly
|
|
;;
|
|
;; html: org-export-html-to-exports
|
|
;; pdf: org-export-pdf-to-exports
|
|
|
|
;; retrieve the top level path that contains _exports
|
|
(defun ronny-path-exports-root ()
|
|
"Find the nearest parent directory containing _exports."
|
|
(let ((root (locate-dominating-file default-directory "_exports")))
|
|
(unless root
|
|
(error "Could not find project root containing _exports/"))
|
|
(expand-file-name root)))
|
|
|
|
(defun ronny-path-depth-from-root ()
|
|
"Return how many directory levels current file is below project root."
|
|
(unless buffer-file-name
|
|
(error "Buffer is not visiting a file"))
|
|
(let* ((root (file-name-as-directory (ronny-path-exports-root)))
|
|
(file-dir (file-name-directory (expand-file-name buffer-file-name)))
|
|
(rel-dir (directory-file-name (file-relative-name file-dir root))))
|
|
(if (string-empty-p rel-dir)
|
|
0
|
|
(length (split-string rel-dir "/" t)))))
|
|
|
|
;; takes the current file and return its path relative to the project root
|
|
(defun ronny-path-exports-relative-path ()
|
|
"Return current Org file path relative to the project root."
|
|
|
|
;; check whether current buffer is currently attached to a file
|
|
;;
|
|
;; 1a. call the path-exports-root function to get the _export root
|
|
;; 1b. make sure it ends with a trailing slash
|
|
;;
|
|
;; 2a. get the full absolute path of the current file
|
|
;; 2b. put it in a variable called "file"
|
|
;;
|
|
;; 3a. make sure that the file is located somewhere under the root path that ALSO contains "_exports"
|
|
;; 3b strip away the root part and return the relative part
|
|
(unless buffer-file-name
|
|
(error "Buffer is not visiting a file"))
|
|
(let* ((root (file-name-as-directory (ronny-path-exports-root))) ;; 1a / 1b
|
|
(file (expand-file-name buffer-file-name))) ;; 2a, 2b
|
|
(unless (string-prefix-p root file) ;; 3a
|
|
(error "Current file is not under project root"))
|
|
(file-relative-name file root))) ;; 3b
|
|
|
|
;; compute the _exports path for the file
|
|
;;
|
|
;; args:
|
|
;; format-dir -> "html" / "pdf"
|
|
;; extension -> ".html" / ".pdf"
|
|
;;
|
|
;; 1a get the project root and store it in "root"
|
|
;; 2a get the relative path and store it in "relpath"
|
|
;; 2b remove the extension from the file and store in "rel-no-ext"
|
|
;; 3a create the filepath that will exist from _exports + the relative filepath and ext
|
|
;; 3b put that under the root path
|
|
;; 4a retrieve only the directory part of the path
|
|
;; 4b make the directory if it doesn't exist
|
|
;; 5a return the final path
|
|
(defun ronny-exports-build-path (format-dir extension)
|
|
"Build export path under _exports/FORMAT-DIR preserving source structure."
|
|
(let* ((root (ronny-path-exports-root)) ;; 1a
|
|
(relpath (ronny-path-exports-relative-path)) ;; 2a
|
|
(rel-no-ext (file-name-sans-extension relpath)) ;; 2b
|
|
(outfile (expand-file-name
|
|
(concat "_exports/" format-dir "/" rel-no-ext extension) ;; 3a
|
|
root)) ;; 3b
|
|
(outdir (file-name-directory outfile))) ;; 4a
|
|
(make-directory outdir t) ;; 4b
|
|
outfile)) ;; 5a
|
|
|
|
(defun ronny-util-repeat-string (s n)
|
|
"Repeat string S, N times."
|
|
(apply #'concat (make-list n s)))
|
|
|
|
(defun ronny-title-from-file-name ()
|
|
"Convert file name to title.
|
|
Example: product_strategy.org → Product Strategy"
|
|
(unless buffer-file-name
|
|
(error "Buffer is not visiting a file"))
|
|
(let* ((base (file-name-base buffer-file-name))
|
|
(spaced (replace-regexp-in-string "[-_]+" " " base)))
|
|
(string-join
|
|
(mapcar #'capitalize (split-string spaced " +" t))
|
|
" ")))
|
|
|
|
(defun ronny-title-from-parent-folder ()
|
|
"Return parent directory name as title."
|
|
(unless buffer-file-name
|
|
(error "Buffer is not visiting a file"))
|
|
(let* ((parent (file-name-nondirectory
|
|
(directory-file-name
|
|
(file-name-directory (expand-file-name buffer-file-name))))))
|
|
(string-join
|
|
(mapcar #'capitalize
|
|
(split-string (replace-regexp-in-string "[-_]+" " " parent) " +" t))
|
|
" ")))
|
|
|
|
(defun ronny-template-read-file (name)
|
|
"Read template NAME from ~/.doom.d/templates/notes/."
|
|
(let ((path (expand-file-name name "~/.doom.d/templates/notes/")))
|
|
(unless (file-exists-p path)
|
|
(error "Template not found: %s" path))
|
|
(with-temp-buffer
|
|
(insert-file-contents path)
|
|
(buffer-string))))
|
|
|
|
(defun ronny-template-fill (template replacements)
|
|
"Replace placeholders in TEMPLATE using REPLACEMENTS."
|
|
(dolist (pair replacements template)
|
|
(setq template
|
|
(replace-regexp-in-string
|
|
(regexp-quote (format "{{%s}}" (car pair)))
|
|
(cdr pair)
|
|
template t t))))
|
|
|
|
(defun ronny-template-insert-online-store ()
|
|
"Insert online store Org template based on current file path."
|
|
(interactive)
|
|
(let* ((template (ronny-template-read-file "store/default.org"))
|
|
(depth (ronny-util-repeat-string "../" (ronny-path-depth-from-root)))
|
|
(filled
|
|
(ronny-template-fill
|
|
template
|
|
`(("title" . ,(ronny-title-from-file-name))
|
|
("media_prefix" . ,depth)
|
|
("toc_prefix" . ,depth)
|
|
("parent_title" . ,(ronny-title-from-parent-folder))))))
|
|
(insert filled)))
|
|
|
|
;; command to export the current Org file to HTML (run interactively)
|
|
;;
|
|
;; 1a make the function callable as an emacs command
|
|
;; - can run with M-x
|
|
;; - can bind to a key
|
|
;; 2a get the full export path for an html export
|
|
;; 3a call the export to command to generate an html file based on the current org file
|
|
;; 3b confirmation message display in minibuffer
|
|
(defun ronny-org-export-html-to-exports ()
|
|
"Export current Org file to _exports/html preserving directory structure."
|
|
(interactive) ;; 1a
|
|
(let ((outfile (ronny-exports-build-path "html" ".html"))) ;; 2a
|
|
(org-export-to-file 'html outfile nil nil nil nil nil) ;; 3a
|
|
(message "Exported HTML to %s" outfile))) ;; 3b
|
|
|
|
;; same as above but for latex/pdf
|
|
;; (defun ronny-org-export-pdf-to-exports ()
|
|
;; "Export current Org file to _exports/pdf preserving directory structure."
|
|
;; (interactive)
|
|
;; (let ((outfile (ronny-exports-build-path "pdf" ".pdf")))
|
|
;; (org-export-to-file 'latex outfile nil nil nil nil nil)
|
|
;; (message "Exported PDF to %s" outfile)))
|
|
|
|
;; export to pdf
|
|
;;
|
|
;; generate the pdf in the normal way
|
|
;; file is created next to the org source file
|
|
;; move it to where it should be under _exports
|
|
;;
|
|
;; 1a make this funciton callable
|
|
;;
|
|
;; 2a put the final _export path as a string in "outfile"
|
|
;; 2b get the src dir and put it in srcdir
|
|
;; 2c get the src file name and put it in base
|
|
;; 2d replace the ext org with pdf and put this value in pdf-src
|
|
;;
|
|
;; 3a generate the pdf normally
|
|
;;
|
|
;; 4a use rename to move the pdf to _exports
|
|
;;
|
|
(defun ronny-org-export-pdf-to-exports ()
|
|
"Export current Org file to _exports/pdf and clean up LaTeX artifacts."
|
|
(interactive) ; 1a
|
|
(let* ((outfile (ronny-exports-build-path "pdf" ".pdf")) ;; 2a
|
|
(srcdir (file-name-directory buffer-file-name)) ;; 2b
|
|
(base (file-name-base buffer-file-name)) ;; 2c
|
|
(pdf-src (expand-file-name (concat base ".pdf") srcdir))) ;; 2d
|
|
|
|
;; Export PDF next to the source Org file
|
|
(org-latex-export-to-pdf) ; 3a
|
|
|
|
;; Move PDF to _exports
|
|
(unless (file-exists-p pdf-src)
|
|
(error "Expected PDF not found: %s" pdf-src))
|
|
(rename-file pdf-src outfile t) ;; 4a
|
|
|
|
;; Delete common LaTeX byproducts left next to the source Org file
|
|
(dolist (f (directory-files srcdir t
|
|
(concat "^" (regexp-quote base)
|
|
"\\.\\(tex\\|aux\\|log\\|out\\|toc\\|nav\\|snm\\|fls\\|fdb_latexmk\\)$")))
|
|
(delete-file f))
|
|
|
|
(message "Exported PDF to %s" outfile)))
|
|
|
|
;; exports to odt and docx
|
|
;; see above
|
|
(defun ronny-org-export-odt-to-exports ()
|
|
"Export current Org file to _exports/odt."
|
|
(interactive)
|
|
(let* ((outfile (ronny-exports-build-path "odt" ".odt"))
|
|
(srcdir (file-name-directory buffer-file-name))
|
|
(base (file-name-base buffer-file-name))
|
|
(odt-src (expand-file-name (concat base ".odt") srcdir)))
|
|
|
|
;; export
|
|
(org-odt-export-to-odt)
|
|
|
|
;; move file
|
|
(unless (file-exists-p odt-src)
|
|
(error "Expected ODT not found: %s" odt-src))
|
|
(rename-file odt-src outfile t)
|
|
|
|
(message "Exported ODT to %s" outfile)))
|
|
|
|
(defun ronny-org-export-docx-to-exports ()
|
|
"Export current Org file to _exports/docx via ODT."
|
|
(interactive)
|
|
(let* ((odt-out (ronny-exports-build-path "odt" ".odt"))
|
|
(docx-out (ronny-exports-build-path "docx" ".docx"))
|
|
(base (file-name-base buffer-file-name)))
|
|
|
|
;; first export ODT
|
|
(ronny-org-export-odt-to-exports)
|
|
|
|
;; convert to docx
|
|
(call-process "soffice" nil 0 nil
|
|
"--headless"
|
|
"--convert-to" "docx"
|
|
"--outdir" (file-name-directory docx-out)
|
|
odt-out)
|
|
|
|
;; rename to correct filename if needed
|
|
(let ((generated (expand-file-name (concat base ".docx")
|
|
(file-name-directory docx-out))))
|
|
(rename-file generated docx-out t))
|
|
|
|
(message "Exported DOCX to %s" docx-out)))
|
|
|
|
;; export + open helpers
|
|
|
|
;; use the osx system to open files
|
|
(defun ronny-open-file-in-macos (path)
|
|
"Open PATH in the default macOS application."
|
|
(interactive "fOpen file: ")
|
|
(call-process "open" nil 0 nil (expand-file-name path)))
|
|
|
|
(defun ronny-org-export-html-to-exports-and-open ()
|
|
"Export current Org file to _exports/html and open it."
|
|
(interactive)
|
|
(let ((outfile (ronny-exports-build-path "html" ".html")))
|
|
(ronny-org-export-html-to-exports)
|
|
(ronny-open-file-in-macos outfile)))
|
|
|
|
;; use osx to open
|
|
(defun ronny-org-export-pdf-to-exports-and-open ()
|
|
"Export current Org file to _exports/pdf and open it."
|
|
(interactive)
|
|
(let ((outfile (ronny-exports-build-path "pdf" ".pdf")))
|
|
(ronny-org-export-pdf-to-exports)
|
|
(ronny-open-file-in-macos outfile)))
|