|
| 1 | +;;; lint/org.el --- Run org-lint on Org files -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;;; Commentary: |
| 4 | +;; |
| 5 | +;; Commmand use to run `org-lint' for all files |
| 6 | +;; |
| 7 | +;; $ eask lint org [files..] |
| 8 | +;; |
| 9 | +;; |
| 10 | +;; Positionals: |
| 11 | +;; |
| 12 | +;; [files..] files you want org-lint to run on |
| 13 | +;; |
| 14 | + |
| 15 | +;;; Code: |
| 16 | + |
| 17 | +(let ((dir (file-name-directory (nth 1 (member "-scriptload" command-line-args))))) |
| 18 | + (load (expand-file-name "_prepare.el" |
| 19 | + (locate-dominating-file dir "_prepare.el")) |
| 20 | + nil t)) |
| 21 | + |
| 22 | +;; |
| 23 | +;;; Flags |
| 24 | + |
| 25 | +(advice-add #'eask-allow-error-p :override #'eask-always) |
| 26 | + |
| 27 | +;; |
| 28 | +;;; Core |
| 29 | + |
| 30 | +(defun eask-lint-org--print-error (file result) |
| 31 | + "Print the error RESULT from FILE." |
| 32 | + (let* ((data (cl-second result)) |
| 33 | + (filename (file-name-nondirectory file)) |
| 34 | + (line (elt data 0)) |
| 35 | + (text (elt data 2)) |
| 36 | + (msg (concat filename ":" line ": " text))) |
| 37 | + (if (eask-strict-p) (error msg) (warn msg)))) |
| 38 | + |
| 39 | +(defun eask-lint-org--file (file) |
| 40 | + "Run `org-lint' on FILE." |
| 41 | + (eask-msg "`%s` with org-lint" (ansi-green file)) |
| 42 | + (with-temp-buffer |
| 43 | + (insert-file-contents file) |
| 44 | + (org-mode) |
| 45 | + (if-let* ((results (org-lint))) |
| 46 | + (mapc (lambda (result) |
| 47 | + (eask-lint-org--print-error file result)) |
| 48 | + results) |
| 49 | + (eask-msg "No issues found")))) |
| 50 | + |
| 51 | +(eask-start |
| 52 | + ;; Preparation |
| 53 | + (eask-archive-install-packages '("gnu") |
| 54 | + 'org) |
| 55 | + |
| 56 | + ;; Start Linting |
| 57 | + (require 'org-lint) |
| 58 | + (let* ((patterns (eask-args)) |
| 59 | + (files (eask-expand-file-specs patterns))) |
| 60 | + (cond |
| 61 | + ;; Files found, do the action! |
| 62 | + (files |
| 63 | + (mapcar #'eask-lint-org--file files) |
| 64 | + (eask-msg "") |
| 65 | + (eask-info "(Total of %s file%s linted)" (length files) |
| 66 | + (eask--sinr files "" "s"))) |
| 67 | + ;; Pattern defined, but no file found! |
| 68 | + (patterns |
| 69 | + (eask-msg "") |
| 70 | + (eask-info "(No files match wildcard: %s)" |
| 71 | + (mapconcat #'identity patterns " "))) |
| 72 | + ;; Default, print help! |
| 73 | + (t |
| 74 | + (eask-msg "") |
| 75 | + (eask-info "(No files have been linted)") |
| 76 | + (eask-help "lint/org"))))) |
| 77 | + |
| 78 | +;;; lint/org.el ends here |
0 commit comments