help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: pandoc-mode /Asciidoc


From: Jean Louis
Subject: Re: pandoc-mode /Asciidoc
Date: Thu, 23 Feb 2023 20:48:16 +0300
User-agent: Mutt/2.2.9+54 (af2080d) (2022-11-21)

This is what you should put:

(defun string-to-file-force (string file)
  "Prints string into file, matters not if file exists. Return FILE as file
name."
  (with-temp-file file
    (insert string))
  file)

(defun rcd-command-output-from-input (program input &rest args)
  "Return output string from PROGRAM with given INPUT string and optional
ARGS."
  (let* ((output (with-temp-buffer
                   (insert input)
                   (apply #'call-process-region nil nil program t '(t nil) nil 
args)
                   (buffer-string))))
    output))

(defun rcd-asciidoctor (string &rest args)
  "Return HTML by using Asciidoctor markup STRING."
  (let ((asciidoctor (executable-find "asciidoctor")))
    (apply 'rcd-command-output-from-input "asciidoctor" string "-" args)))

(defun rcd-asciidoctor-preview ()
  "Preview asciidoctor"
  (interactive)
  (let ((asciidoctor (executable-find "asciidoctor")))
    (when asciidoctor
      (let* ((output (buffer-string))
             (output (rcd-asciidoctor output))
             (file (string-to-file-force output "asciidoctor-preview.html")))
        (browse-url file)))))

(require 'adoc-mode)
(define-key adoc-mode-map (kbd "C-c a") #'rcd-asciidoctor-preview)

;;Asciidoctor refresh output quickly

(defun rcd-asciidoctor-preview-buffer-as-html ()
  "Convert asciidoctor to file `/tmp/my.html'"
  (let* ((asciidoc (buffer-string))
         (tmp-file "/tmp/my.html")
         (html (rcd-asciidoctor asciidoc)))
    (with-temp-file tmp-file
      (insert html))))

;; Though I would not recommend putting this in init.el as that is
;; too many times preview. It is better turning on and off the timer
;; function only when you need it.
(run-with-timer 1 3 #'rcd-asciidoctor-preview-buffer-as-html)

(defun rcd-markdown (text &rest args)
  "Markdown processing"
  (let ((markdown (executable-find "markdown")))
    (cond (markdown
           (cond (text
                  (apply 'rcd-command-output-from-input markdown (append (list 
text) args)))
                 (t ""))
           (t (user-error "Command `markdown' not available"))))))

(defun rcd-markdown-preview ()
  "Preview Markdown."
  (interactive)
  (let* ((output (rcd-markdown (buffer-string)))
         (file (concat (buffer-file-name) "-MD.html")))
    (with-temp-file file (insert output))
    (browse-url file)))

;;(define-key KEYMAP KEY DEF &optional REMOVE)
(require 'markdown-mode)
(define-key markdown-mode-map (kbd "C-c a") #'rcd-markdown-preview)

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]