Merge branch 'insert-html-shr'

* Support HTML rendering using shr-insert-document.
* Output type preference can be configured now.
This commit is contained in:
Takafumi Arakaki 2012-08-31 00:35:12 +02:00
commit 6da1f729d7
4 changed files with 146 additions and 12 deletions

View file

@ -327,6 +327,8 @@ Notebook
.. el:variable:: ein:scratch-notebook-name-template
.. el:variable:: ein:iexec-delay
.. el:variable:: ein:complete-on-dot
.. el:variable:: ein:output-type-preference
.. el:variable:: ein:shr-env
Console
^^^^^^^
@ -497,6 +499,10 @@ Change Log
v0.2
----
* Preferred MIME types to be used can be configured using the variable
:el:symbol:`ein:output-type-preference`.
* HTML content is rendered SHR (Simple HTML Renderer) by default.
Use :el:symbol:`ein:shr-env` to tweak how HTML rendered.
* :el:symbol:`ein:notebook-discard-output-on-save` is obsolete now.
* Support execution history. Commands
:el:symbol:`ein:worksheet-previous-input-history` and

View file

@ -40,6 +40,7 @@
(require 'ein-log)
(require 'ein-node)
(require 'ein-kernel)
(require 'ein-output-area)
;;; Faces
@ -802,9 +803,43 @@ Called from ewoc pretty printer via `ein:cell-insert-output'."
(ein:cell-append-mime-type json (oref cell :dynamic))
(ein:insert-read-only "\n"))
(defcustom ein:output-type-preference
(if (and (fboundp 'shr-insert-document)
(fboundp 'libxml-parse-xml-region))
#'ein:output-type-prefer-pretty-text-over-html
'(emacs-lisp svg png jpeg text html latex javascript))
"Output types to be used in notebook.
First output-type found in this list will be used.
This variable can be a list or a function returning a list given
DATA plist.
See also `ein:output-type-prefer-pretty-text-over-html'.
**Example**:
If you prefer HTML type over text type, you can set it as::
(setq ein:output-type-preference
'(emacs-lisp svg png jpeg html text latex javascript))
Note that ``html`` comes before ``text``."
:group 'ein)
(defun ein:output-type-prefer-pretty-text-over-html (data)
"Use text type if it is a \"prettified\" text instead of HTML.
This is mostly for *not* using HTML table for pandas but using
HTML for other object.
If the text type output contains a newline, it is assumed be a
prettified text thus be used instead of HTML type."
(if (ein:aand (plist-get data :text) (string-match-p "\n" it))
'(emacs-lisp svg png jpeg text html latex javascript)
'(emacs-lisp svg png jpeg html text latex javascript)))
(defun ein:cell-append-mime-type (json dynamic)
(loop
for key in '(emacs-lisp svg png jpeg text latex html javascript)
for key in (cond
((functionp ein:output-type-preference)
(funcall ein:output-type-preference json))
(t ein:output-type-preference))
for type = (intern (format ":%s" key)) ; something like `:text'
for value = (plist-get json type) ; FIXME: optimize
when (plist-member json type)
@ -821,7 +856,9 @@ Called from ewoc pretty printer via `ein:cell-insert-output'."
(emacs-lisp
(when dynamic
(ein:cell-safe-read-eval-insert (plist-get json type))))
((html latex text)
(html
(funcall (ein:output-area-get-html-renderer) (plist-get json type)))
((latex text)
(ein:insert-read-only (plist-get json type)))
(svg
(insert-image (create-image value key t)))

89
lisp/ein-output-area.el Normal file
View file

@ -0,0 +1,89 @@
;;; ein-output-area.el --- Output area module
;; Copyright (C) 2012 Takafumi Arakaki
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
;; This file is NOT part of GNU Emacs.
;; ein-output-area.el is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;; ein-output-area.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with ein-output-area.el.
;; If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'ein-core)
;;; HTML renderer
(defun ein:output-area-get-html-renderer ()
;; FIXME: make this configurable
(cond
((and (fboundp 'shr-insert-document)
(fboundp 'libxml-parse-xml-region))
#'ein:insert-html-shr)
(t #'ein:insert-read-only)))
(defcustom ein:shr-env
'((shr-table-horizontal-line ?-)
(shr-table-vertical-line ?|)
(shr-table-corner ?+))
"Variables let-bound while calling `shr-insert-document'.
To use default shr setting::
(setq ein:shr-env nil)
Draw boundaries for table (default)::
(setq ein:shr-env
'((shr-table-horizontal-line ?-)
(shr-table-vertical-line ?|)
(shr-table-corner ?+)))
"
:group 'ein)
(defun ein:shr-insert-document (dom)
"`shr-insert-document' with EIN setting."
(eval `(let ,ein:shr-env (shr-insert-document dom))))
(defun ein:insert-html-shr (html-string)
"Render HTML-STRING using `shr-insert-document'.
Usage::
(ein:insert-html-shr \"<b>HTML</b> string\")
"
(let ((start (point))
end)
(ein:shr-insert-document
(with-temp-buffer
(erase-buffer)
(insert html-string)
(libxml-parse-html-region (point-min) (point-max))))
(setq end (point))
(put-text-property start end 'read-only t)
(put-text-property start end 'front-sticky t)))
(provide 'ein-output-area)
;;; ein-output-area.el ends here

View file

@ -91,18 +91,20 @@ some input
;; Insert pyout/display_data
(defun eintest:cell-insert-output (outputs regexp)
(ein:testing-with-one-cell
(ein:cell-from-json
(list :cell_type "code"
:outputs outputs
:input "some input"
:prompt_number 111)
:ewoc (oref ein:%worksheet% :ewoc))
(goto-char (ein:cell-location cell))
(should (looking-at (format "\
(let ((ein:output-type-preference
'(emacs-lisp svg png jpeg text html latex javascript)))
(ein:testing-with-one-cell
(ein:cell-from-json
(list :cell_type "code"
:outputs outputs
:input "some input"
:prompt_number 111)
:ewoc (oref ein:%worksheet% :ewoc))
(goto-char (ein:cell-location cell))
(should (looking-at (format "\
In \\[111\\]:
some input
%s" regexp)))))
%s" regexp))))))
(defmacro eintest:gene-test-cell-insert-output-pyout-and-display-data
(name regexps outputs)