|
| 1 | +(in-package :cl-user) |
| 2 | +(defpackage quickdocs-updater.readme |
| 3 | + (:use :cl) |
| 4 | + (:import-from :quickdocs-updater.release |
| 5 | + :readme-file) |
| 6 | + (:import-from :alexandria |
| 7 | + :ignore-some-conditions) |
| 8 | + (:export :convert-readme)) |
| 9 | +(in-package :quickdocs-updater.readme) |
| 10 | + |
| 11 | +(defparameter *timeout-command* |
| 12 | + #+darwin "gtimeout" ;; of coreutils |
| 13 | + #-darwin "timeout") |
| 14 | + |
| 15 | +(defparameter *pandoc-path* (or (uiop:getenv "PANDOC_PATH") |
| 16 | + "pandoc")) |
| 17 | + |
| 18 | +(defparameter *emacs-path* (or (uiop:getenv "EMACS_PATH") |
| 19 | + "emacs")) |
| 20 | + |
| 21 | +(defun which (command) |
| 22 | + (handler-case |
| 23 | + (let* ((result (with-output-to-string (s) |
| 24 | + (uiop:run-program `("which" ,command) |
| 25 | + :output s |
| 26 | + :error-output *error-output*))) |
| 27 | + (newline-pos |
| 28 | + (position-if (lambda (char) |
| 29 | + (or (char= char #\Newline) |
| 30 | + (char= char #\Return))) |
| 31 | + result))) |
| 32 | + (if newline-pos |
| 33 | + (subseq result 0 newline-pos) |
| 34 | + result)) |
| 35 | + (uiop:subprocess-error () |
| 36 | + nil))) |
| 37 | + |
| 38 | +(defun check-if-command-installed (&rest commands) |
| 39 | + (dolist (command commands) |
| 40 | + (or (which command) |
| 41 | + (error "Requiement ~S does not exist. Ensure if it's installed and check your PATH." command)))) |
| 42 | + |
| 43 | +(defun convert-readme (file) |
| 44 | + (check-type file pathname) |
| 45 | + (assert (uiop:file-exists-p file)) |
| 46 | + (let ((type (pathname-type file))) |
| 47 | + (cond |
| 48 | + ((or (null type) |
| 49 | + (string= type "txt")) |
| 50 | + (convert-plaintext-into-html file)) |
| 51 | + ((or (string= type "markdown") |
| 52 | + (string= type "md")) |
| 53 | + (convert-markdown-into-html file)) |
| 54 | + ((string= type "org") |
| 55 | + (convert-org-into-html file))))) |
| 56 | + |
| 57 | +(defun convert-plaintext-into-html (file) |
| 58 | + (concatenate 'string "<pre>" (uiop:read-file-string file) "</pre>")) |
| 59 | + |
| 60 | +(defun convert-markdown-into-html (file) |
| 61 | + (check-if-command-installed *timeout-command* *pandoc-path*) |
| 62 | + (ignore-some-conditions (uiop:subprocess-error) |
| 63 | + (with-output-to-string (s) |
| 64 | + (uiop:run-program `(,*timeout-command* 10 ,*pandoc-path* ,(namestring file)) |
| 65 | + :force-shell t |
| 66 | + :output s |
| 67 | + :error-output *error-output*)))) |
| 68 | + |
| 69 | +(defun convert-org-into-html (file) |
| 70 | + (check-if-command-installed *timeout-command* *emacs-path*) |
| 71 | + (ignore-some-conditions (uiop:subprocess-error) |
| 72 | + (with-output-to-string (s) |
| 73 | + (uiop:run-program `(,*timeout-command* |
| 74 | + 10 |
| 75 | + ,*emacs-path* "-Q" "-batch" "--file" ,(namestring file) |
| 76 | + "--eval" "(setq org-export-with-toc nil)" |
| 77 | + "--eval" "(princ (org-export-as-html 3 nil 'string t))") |
| 78 | + :force-shell t |
| 79 | + :output s |
| 80 | + :error-output *error-output*)))) |
0 commit comments