Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mmdc's ability to read from STDIN #24

Open
ayorgo opened this issue Dec 2, 2023 · 1 comment · May be fixed by #25
Open

Use mmdc's ability to read from STDIN #24

ayorgo opened this issue Dec 2, 2023 · 1 comment · May be fixed by #25

Comments

@ayorgo
Copy link

ayorgo commented Dec 2, 2023

As mermaid-cli allows piping from STDIN it would be nice to bypass the creation of a temporary file in this mode.

This would make ob-mermaid work with containerized installations without affecting current usage scenarios.

For example, with the following script at /some_path/mmdc:

#!/usr/bin/env sh
docker run --rm -i -u `id -u`:`id -g` -v $(pwd):/data minlag/mermaid-cli:10.6.1 mmdc "$@"

I am able to generate diagrams as follows:

$ cat test.mmd | ./mmdc -o test.png

So if this behaviour was reflected in ob-mermaid.el the following configuration would be enough to make it work with containers:

(setq ob-mermaid-cli-path "/some_path/mmdc")
@ayorgo ayorgo changed the title Use mmdc's ability to read from STDIN instead of a temporary file Use mmdc's ability to read from STDIN Dec 2, 2023
@bradprob
Copy link

bradprob commented Dec 27, 2023

A workaround of the suggested request might be:

(use-package ob-mermaid
  :ensure t
  :config
  (setq ob-mermaid-use-wrapper t)
  (setq ob-mermaid-cli-path "/PATH/TO/WRAPPER/SCRIPT.sh")
  (defun org-babel-execute:mermaid (body params)
    (let* ((out-file (or (cdr (assoc :file params))
                         (error "mermaid requires a \":file\" header argument")))
	   (theme (cdr (assoc :theme params)))
	   (width (cdr (assoc :width params)))
	   (height (cdr (assoc :height params)))
	   (background-color (cdr (assoc :background-color params)))
	   (mermaid-config-file (cdr (assoc :mermaid-config-file params)))
	   (css-file (cdr (assoc :css-file params)))
	   (pupeteer-config-file (cdr (assoc :pupeteer-config-file params)))
           (temp-file (org-babel-temp-file "mermaid-"))
           (mmdc (or ob-mermaid-cli-path
                     (executable-find "mmdc")
                     (error "`ob-mermaid-cli-path' is not set and mmdc is not in `exec-path'")))
           (cmd (concat (unless ob-mermaid-use-wrapper
                          (concat (shell-quote-argument (expand-file-name mmdc))
                                  " -i "
                                  (org-babel-process-file-name temp-file)
                                  " -o "
                                  (org-babel-process-file-name out-file)))
		        (when theme
			  (concat " -t " theme))
		        (when background-color
			  (concat " -b " background-color))
		        (when width
			  (concat " -w " width))
		        (when height
			  (concat " -H " height))
		        (when mermaid-config-file
			  (concat " -c " (org-babel-process-file-name mermaid-config-file)))
		        (when css-file
			  (concat " -C " (org-babel-process-file-name css-file)))
                        (when pupeteer-config-file
                          (concat " -p " (org-babel-process-file-name pupeteer-config-file))))))
      (unless (file-executable-p mmdc)
        ;; cannot happen with `executable-find', so we complain about
        ;; `ob-mermaid-cli-path'
        (error "Cannot find or execute %s, please check `ob-mermaid-cli-path'" mmdc))
      (with-temp-file temp-file (insert body))
      (message "%s" cmd)
      (if ob-mermaid-use-wrapper
          (shell-command (format "cat %s | %s %s -o %s" temp-file mmdc cmd out-file))
        (org-babel-eval cmd ""))
      nil)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants