Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.7 KiB

  1. ;;; ein-cell-output.el --- Cell module
  2. ;; (C) 2015- John M. Miller
  3. ;; Author: John M. Miller (millejoh at mac dot com)
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-cell-output.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-cell-output.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-cell-output.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Because wiriting cell outputs to nbformat v4.0 json is complicated
  17. ;; enought that it warrants a file all to its own.
  18. ;;; Code:
  19. (require 'ein-cell)
  20. (defvar ein:output-type-map
  21. '((:svg . :image/svg+xml) (:png . :image/png) (:jpeg . :image/jpeg)
  22. (:text . :text/plain)
  23. (:html . :text/html) (:latex . :text/latex) (:javascript . :text/javascript)))
  24. (defun ein:output-property (maybe-property)
  25. (cdr (assoc maybe-property ein:output-type-map)))
  26. ;;; Dealing with Code cell outputs
  27. (defun ein:cell-stream-output-to-json (output)
  28. `((output_type . "stream")
  29. (name . ,(plist-get output :stream))
  30. (text . ,(plist-get output :text))))
  31. (defun ein:cell-error-output-to-json (output)
  32. `((output_type . "error")
  33. (ename . ,(plist-get output :ename))
  34. (evalue . ,(plist-get output :evalue))
  35. (traceback . ,(plist-get output :traceback))))
  36. (defun ein:cell-execute-result-output-to-json (output)
  37. (let ((data (ein:aif (plist-get output :text)
  38. `("text/plain" . ,it)
  39. (plist-get output :data))))
  40. `((output_type . "execute_result")
  41. (metadata . ,(make-hash-table))
  42. (execution_count . ,(or (plist-get output :prompt_number)
  43. (plist-get output :execution_count)))
  44. (data . (,data)))))
  45. (defun ein:maybe-get-output-mime-data (output)
  46. (cl-loop for type in '(:svg :png :jpeg :html :latex :javascript :text)
  47. if (plist-get output type)
  48. collecting (cons (ein:output-property type) (plist-get output type))))
  49. (defun ein:cell-display-data-output-to-json (output)
  50. (let ((data (or (ein:maybe-get-output-mime-data output)
  51. (plist-get output :data))))
  52. `((output_type . "display_data")
  53. (data . ,data)
  54. (metadata . ,(make-hash-table)))))
  55. (defun ein:find-and-make-outputs (output-plist)
  56. (cl-loop for prop in ein:output-type-map
  57. when (plist-get output-plist (cdr prop))
  58. collect (list (cdr prop) (plist-get output-plist (cdr prop)))))
  59. (provide 'ein-cell-output)