Function: markdown-ts-convert

markdown-ts-convert is an autoloaded, interactive and byte-compiled function defined in markdown-ts-mode-x.el.gz.

Signature

(markdown-ts-convert &optional INPUT-FILE OUTPUT-FILE FORMAT DISPLAY OVERWRITE QUIET)

Documentation

Convert a markdown-ts-mode buffer or file to FORMAT.

Convert INPUT-FILE to OUTPUT-FILE.

If INPUT-FILE is nil, use the current buffer.

If OUTPUT-FILE is nil, prompt for an output file.

Optional FORMAT is a format/converter pair specified as a cons
'(format . converter) for example '(html . pandoc). See
markdown-ts-converters. If nil, prompt for the format and converter.

If optional DISPLAY is non-nil, show the output file in a buffer, if possible, using the function markdown-ts-convert-display-function.

With a prefix argument, DISPLAY will be non-nil.

If optional OVERWRITE is non-nil, silently overwrite OUTPUT-FILE if it exists.

If QUIET is non-nil, inhibit showing conversion warnings or errors.

The external executable specified by a converter must be installed and found; see the variable exec-path(var)/exec-path(fun).

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode-x.el.gz
;;;###autoload
(defun markdown-ts-convert (&optional
                            input-file output-file
                            format display overwrite quiet)
  "Convert a `markdown-ts-mode' buffer or file to FORMAT.
Convert INPUT-FILE to OUTPUT-FILE.

If INPUT-FILE is nil, use the current buffer.

If OUTPUT-FILE is nil, prompt for an output file.

Optional FORMAT is a format/converter pair specified as a cons
\\='(format . converter) for example \\='(html . pandoc).  See
`markdown-ts-converters'.  If nil, prompt for the format and converter.

If optional DISPLAY is non-nil, show the output file in a buffer, if
possible, using the function `markdown-ts-convert-display-function'.

With a prefix argument, DISPLAY will be non-nil.

If optional OVERWRITE is non-nil, silently overwrite OUTPUT-FILE if it
exists.

If QUIET is non-nil, inhibit showing conversion warnings or errors.

The external executable specified by a converter must be installed and
found; see the variable `exec-path'."
  (interactive)
  (when (and input-file
             (equal buffer-file-name (expand-file-name input-file))
             (buffer-modified-p))
    (when (yes-or-no-p "Save modified buffer before converting? ")
      (save-buffer)))
  (unless input-file
    (markdown-ts--barf-if-not-mode 'markdown-ts-convert))

  (setq display (or display current-prefix-arg))

  (let* ((converter-format
          (or (car format)
              (car markdown-ts-default-converter)
              (intern
               (completing-read
                "Select an output format: "
                (mapcar (lambda (elt) (symbol-name (car elt)))
                        markdown-ts-converters)
                nil
                t
                nil
                'markdown-ts-converter-format))))
         (converter
          (or (cdr format)
              (cdr markdown-ts-default-converter)
              (intern
               (completing-read
                "Select a converter: "
                (mapcar (lambda (elt) (symbol-name (car elt)))
                        (alist-get converter-format markdown-ts-converters))
                nil
                t
                nil
                'markdown-ts-converter))))
         (config (alist-get converter
                            (alist-get converter-format
                                       markdown-ts-converters)))
         (command (plist-get config :command))
         (command-input (plist-get config :input))
         (command-output (plist-get config :output))
         (stdin-only (equal command-input '(stdin)))
         (stdout-only (equal command-output '(stdout))))
    (unless output-file
      (setq output-file
            (read-file-name "Output file name: "
                            nil
                            nil
                            nil
                            (concat (file-name-sans-extension
                                     (file-relative-name
                                      (or input-file buffer-file-name)))
                                    "."
                                    (symbol-name converter-format))))
      (unless output-file
        (user-error "Output file name is required")))
    (when output-file
      (when (and (not overwrite)
                 (file-exists-p output-file)
                 (not (yes-or-no-p (format "%s exists; overwrite?" output-file))))
        (user-error "Not overwriting existing file: %s" output-file))
      (setq output-file (expand-file-name output-file)))


    (let ((command-args (funcall (plist-get config :arguments-function)
                                 (when input-file (expand-file-name input-file))
                                 output-file)))
      (unless (executable-find command)
        (user-error "`%s' executable not found" command))
      (let ((error-buffer (get-buffer-create
                           markdown-ts--convert-error-buffer))
            (error-temp-file (make-temp-file "markdown-ts-convert-stderr"
                                             nil ".log"))
            (stdout-buffer (when stdout-only
                             (get-buffer-create
                              markdown-ts--convert-output-buffer))))
        (unwind-protect
            (let (exit-status)
              (with-current-buffer error-buffer
                (special-mode)
                (setq buffer-read-only nil)
                (widen)
                (erase-buffer))
              (when stdout-buffer
                (with-current-buffer stdout-buffer
                  (special-mode)
                  (setq buffer-read-only nil)
                  (widen)
                  (erase-buffer)))
              (cond
               ((and input-file (not stdin-only))
                (setq exit-status
                      (apply #'call-process
                             command
                             nil
                             (list stdout-buffer error-temp-file)
                             nil
                             command-args)))
               ((and input-file stdin-only)
                (with-temp-buffer
                  (insert-file-contents input-file)
                  (setq exit-status
                        (apply #'call-process-region
                               nil
                               nil
                               command
                               nil
                               (list stdout-buffer error-temp-file)
                               nil
                               command-args))))
               (t
                (without-restriction
                  (setq exit-status
                        (apply #'call-process-region
                               nil
                               nil
                               command
                               nil
                               (list stdout-buffer error-temp-file)
                               nil
                               command-args)))))
              (let ((error-size (file-attribute-size (file-attributes error-temp-file))))
                (when (and error-size (> error-size 0))
                  (with-current-buffer error-buffer
                    (insert-file-contents error-temp-file)
                    (goto-char (point-min)))))
              (cond ((eq 0 exit-status) ; Integer or string.
                     (when stdout-buffer
                       (with-current-buffer stdout-buffer
                         (write-region (point-min) (point-max) output-file)))
                     (when display
                       (funcall markdown-ts-convert-display-function
                                output-file))
                     (when (and (not quiet)
                                (not (zerop (buffer-size error-buffer)))
                                (yes-or-no-p
                                 (format-message
                                  "Conversion file `%s' successfully created with warnings; display them?"
                                  output-file)))
                       (display-buffer error-buffer))
                     (message "Conversion file successfully created `%s'%s"
                              output-file
                              (if (zerop (buffer-size error-buffer))
                                  ""
                                (format " with warnings; see buffer `%s'" error-buffer))))
                    (t
                     (if quiet
                         (error
                          "Converter aborted with exit code %S; see buffer `%s' for errors"
                          exit-status
                          markdown-ts--convert-error-buffer)
                       (when (yes-or-no-p (format
                                           "Converter aborted with exit code %S; display errors?"
                                           exit-status))
                         (display-buffer error-buffer))))))
          (delete-file error-temp-file))))))