[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/code-cells aeddd889c2 04/36: Add ipynb support via Jupy
From: |
ELPA Syncer |
Subject: |
[elpa] externals/code-cells aeddd889c2 04/36: Add ipynb support via Jupytext |
Date: |
Mon, 28 Feb 2022 15:57:41 -0500 (EST) |
branch: externals/code-cells
commit aeddd889c229e3b0af0a3322f7fa7e8a472e5143
Author: Augusto Stoffel <astoff@users.noreply.github.com>
Commit: Augusto Stoffel <astoff@users.noreply.github.com>
Add ipynb support via Jupytext
---
cells.el | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)
diff --git a/cells.el b/cells.el
index 23c3fa273b..fa09edccda 100644
--- a/cells.el
+++ b/cells.el
@@ -151,5 +151,84 @@ via `pulse-momentary-highlight-region'."
(font-lock-remove-keywords nil spec))
(font-lock-flush)))
+(defvar cells-jupytext-to-script-args '((t "--to" "auto:percent"))
+ "An alist mapping strings to lists of strings. The first entry
+ matching a notebook's metadata.kernelspec.language property is
+ used as additional arguments to jupytext when converting it
+ from ipynb format. The entry `t' serves as fallback.")
+
+(defvar cells-jupytext-to-ipynb-args '((prog-mode "--to" "ipynb"))
+ "An alist mapping major modes to lists of strings. The first
+ entry matching the current buffer's major mode is used when
+ converting it to ipynb format.")
+
+(defvar cells-convert-ipynb-hook '(cells-mode)
+ "Hook run when opening a ipynb file, after it has been
+ converted to a script and the appropriate major mode has been
+ activated.
+
+ An additional hook, named `cells-convert-ipynb-<language>-hook' is
+ run after this one.")
+
+(defun cells-convert-ipynb ()
+ "Pipe buffer through jupytext and set up the appropriate major mode."
+ (interactive)
+ (goto-char (point-min))
+ (let* ((file (buffer-file-name))
+ (nb (json-parse-buffer))
+ (pt (point))
+ (lang (map-nested-elt nb '("metadata" "kernelspec" "language")))
+ (mode (intern (concat lang "-mode")))
+ (args (or (map-elt cells-jupytext-to-script-args mode)
+ (map-elt cells-jupytext-to-script-args t)))
+ (logfile (make-temp-file "jupytext-"))
+ (exit (apply 'call-process-region
+ nil nil
+ "jupytext"
+ nil (list t logfile) nil args)))
+ (with-current-buffer (get-buffer-create "*jupytext log*")
+ (insert (current-time-string)
+ ": Converting \"" (or file "?") "\" to script.\n")
+ (insert-file-contents logfile)
+ (delete-file logfile))
+ (unless (eq 0 exit)
+ (delete-region pt (point-max))
+ (error "Error converting notebook: exit code %s" exit))
+ (delete-region (point-min) pt)
+ (goto-char (point-min))
+ (setq-local write-file-functions '(cells-write-ipynb))
+ (when (fboundp mode)
+ (funcall mode)
+ (run-hooks 'cells-convert-ipynb-hook
+ (intern (concat "cells-convert-ipynb-" lang "-hook"))))))
+
+(defun cells-write-ipynb (&optional file)
+ "Pipe buffer through jupytext and write to ipynb file.
+Interactively, asks for the file name. Called from Lisp, FILE
+defaults to the current buffer file name."
+ (interactive "F")
+ (let* ((file (or file buffer-file-name))
+ (temp (generate-new-buffer " *jupytext output*"))
+ (logfile (make-temp-file "jupytext-"))
+ (args (map-some (lambda (k v) (when (derived-mode-p k) v))
+ cells-jupytext-to-ipynb-args))
+ (exit (apply 'call-process-region nil nil "jupytext" nil
+ (list temp logfile) nil args)))
+ (with-current-buffer (get-buffer-create "*jupytext log*")
+ (insert (current-time-string)
+ ": Converting \"" (or file "?") "\" to ipynb.\n")
+ (insert-file-contents logfile)
+ (delete-file logfile))
+ (unless (eq 0 exit)
+ (error "Error converting notebook: exit code %s" exit))
+ (with-current-buffer temp
+ (write-region nil nil file))
+ (set-buffer-modified-p nil)
+ (set-visited-file-modtime)
+ 'job-done))
+
+;;;###autoload
+(add-to-list 'auto-mode-alist '("\\.ipynb\\'" . cells-convert-ipynb))
+
(provide 'cells)
;;; cells.el ends here
- [elpa] externals/code-cells 1661efd9ae 18/36: Add MELPA badge; mention EIN, (continued)
- [elpa] externals/code-cells 1661efd9ae 18/36: Add MELPA badge; mention EIN, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells d03621b103 19/36: Change screenshot, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 374edc6be7 20/36: Change package description, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 1bd650391a 25/36: Update README, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 4e973e0122 26/36: Do not assume Emacs has native JSON parsing available, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 185c33b8b6 27/36: Fix cell movement for files without newline at the end, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 031f726941 30/36: code-cells-eval: Fix check for a possibly active minor mode, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells ea7799c447 35/36: Release on ELPA, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 37dce1d908 02/36: Add existing code, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 30c0359c36 03/36: Simplify motion code, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells aeddd889c2 04/36: Add ipynb support via Jupytext,
ELPA Syncer <=
- [elpa] externals/code-cells e02bda5b68 09/36: Assorted refinements, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells aefabc4abf 10/36: Comments about ipynb conversion settings, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 7bad8f1cf6 11/36: Speed keys, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 2dc51b44a3 12/36: Correct outline level before first cell header, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 9500e07f83 15/36: Fix byte-compilation issue, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells adda62ec82 16/36: Address some packaging issues, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 67e1479a75 28/36: Add code-cells-mode-maybe, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells f93db2d65c 29/36: Refinement in Commentary section, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 47305f5439 31/36: Indicate that spaces are allowed before cell boundary marker, ELPA Syncer, 2022/02/28
- [elpa] externals/code-cells 68148cfc1f 33/36: Improve cell boundary face, ELPA Syncer, 2022/02/28