From 97eed30380650d991ebe3bff3f73fc4a13df8459 Mon Sep 17 00:00:00 2001 From: kimim Date: Sun, 11 Feb 2024 10:00:04 +0800 Subject: [PATCH] add settings: :crop :nopdf --- chatu-drawio.el | 24 +++++++++++++++--------- chatu.el | 43 +++++++++++++++++++++++++++---------------- docs/README.md | 25 ++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 28 deletions(-) diff --git a/chatu-drawio.el b/chatu-drawio.el index fc63873..3ec7c49 100644 --- a/chatu-drawio.el +++ b/chatu-drawio.el @@ -42,16 +42,22 @@ KEYWORD-PLIST contains parameters from the chatu line." (output-path (plist-get keyword-plist :output-path)) (output-path-pdf (file-name-with-extension output-path "pdf")) (page (plist-get keyword-plist :page))) - (format "%s -x %s -p %s -o %s >/dev/null 2>&1 && \ + (if (plist-get keyword-plist :nopdf) + (format "draw.io %s -x %s -p %s -o %s >/dev/null 2>&1" + (if (plist-get keyword-plist :crop) "--crop" "") + (shell-quote-argument input-path) + (or page "0") + (shell-quote-argument output-path)) + (format "draw.io %s -x %s -p %s -o %s >/dev/null 2>&1 && \ %s %s %s >/dev/null 2>&1 && rm %s" - "draw.io" - (shell-quote-argument input-path) - (or page "0") - (shell-quote-argument output-path-pdf) - "pdf2svg" - (shell-quote-argument output-path-pdf) - (shell-quote-argument output-path) - (shell-quote-argument output-path-pdf)))) + (if (plist-get keyword-plist :crop) "--crop" "") + (shell-quote-argument input-path) + (or page "0") + (shell-quote-argument output-path-pdf) + "pdf2svg" + (shell-quote-argument output-path-pdf) + (shell-quote-argument output-path) + (shell-quote-argument output-path-pdf))))) (defun chatu-drawio-open (keyword-plist) "Open .drawio file. diff --git a/chatu.el b/chatu.el index 2d1e2ad..60ca481 100644 --- a/chatu.el +++ b/chatu.el @@ -92,19 +92,29 @@ (defun chatu-get-keyword (line) "Get chatu keyword from string LINE." - (when (string-match - "#\\+\\(\\w+\\): +" line) - (list :keyword - (substring-no-properties - (match-string 1 line))))) - -(defun chatu-get-type (line) - "Get chatu type from string LINE." - (when (string-match - ":\\(\\w+\\) +" line) - (list :type - (substring-no-properties - (match-string 1 line))))) + (when (and + (string-match + "#\\+\\(\\w+\\): +" line) + (string= "chatu" + (substring-no-properties + (match-string 1 line)))) + (list :chatu t))) + +(defun chatu-get-settings (line) + "Get all chatu settings from string LINE." + (save-match-data + (let ((pos 0) + settings) + (while (string-match "\\(:\\w+\\)\\( +:\\|$\\)" line pos) + (setq settings + (append settings + (list + (intern + (substring-no-properties + (match-string 1 line))) + t))) + (setq pos (match-end 1))) + settings))) (defun chatu-get-input (line) "Get chatu input file from string LINE." @@ -147,6 +157,7 @@ (defvar chatu-keyword-value-functions '(chatu-get-keyword + chatu-get-settings chatu-get-type chatu-get-input chatu-get-output @@ -156,7 +167,7 @@ (defun chatu-normalize-keyword-plist (keyword-plist) "Normalize KEYWORD-PLIST." - (when (plist-get keyword-plist :keyword) + (when (plist-get keyword-plist :chatu) (let* ((input-dir (or (plist-get keyword-plist :input-dir) chatu-input-dir)) (input (plist-get keyword-plist :input)) @@ -275,7 +286,7 @@ (defun chatu-ctrl-c-ctrl-c () "Hook function for `org-ctrl-c-ctrl-c-hook'." (let ((plist (chatu-keyword-plist))) - (if (string= "chatu" (plist-get plist :keyword)) + (if (plist-get plist :chatu) (progn (chatu-add) t) @@ -286,7 +297,7 @@ ARGS is ignored, required by `markdown-follow-thing-at-point'." (ignore args) ;; args are not used. (let ((plist (chatu-keyword-plist))) - (if (string= "chatu" (plist-get plist :keyword)) + (if (plist-get plist :chatu) (progn (chatu-open) t) diff --git a/docs/README.md b/docs/README.md index 7079f78..59ffd7e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -38,6 +38,23 @@ Or git submodule and use-package ``` # Usage +Add `chatu` line with `chatu-new` command: + +```org +#+chatu: :drawio "diagram.drawio" :page 0 :input-dir "./draws" :output-dir "./images" :output "diagram.svg" :crop :nopdf +``` +```markdown + +``` + +Options/Settings: +- `:drawio` specifies the backend, they can be `:planuml`, `:babashka`, `:curl` or others added in the future by you. +- file name after backend keyword is the input file name. +- `:page` species which page you want to import to buffer. +- `:input-dir` and `:output-dir` are the folders for input or output file. +- `:output` is the output file name. +- `:crop` if you want to crop the empty white in page. +- `:nopdf` if you do not want intermediate pdf file for drawio backend. Move cursor to `chatu` line, - `C-c C-c` will invoke `chatu-add` to add image in orgmode. @@ -65,17 +82,19 @@ Both method use a `keyword-plist` parameter, which contains the `chatu` settings from `chatu` line. ```org -#+chatu: :drawio "diagram.drawio" :page 0 :input-dir "./draws" :output-dir "./images" :output "diagram.svg" +#+chatu: :drawio "diagram.drawio" :page 0 :input-dir "./draws" :output-dir "./images" :output "diagram.svg" :crop :nopdf ``` For example, we can get following `keyword-plist` from above `chatu` line: ```emacs-lisp -(:keyword "chatu" :type "drawio" +(:chatu t :type "drawio" :input "diagram.drawio" :output "diagram.svg" :page "0" :input-dir "./draws" :output-dir "./images" :input-path "./draws/diagram.drawio" - :output-path "./images/diagram.svg") + :output-path "./images/diagram.svg" + :crop t + :nopdf t) ``` # Usage