ein-output-area.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ;;; ein-output-area.el --- Output area module
  2. ;; Copyright (C) 2012 Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-output-area.el is free software: you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation, either version 3 of the
  8. ;; License, or (at your option) any later version.
  9. ;; ein-output-area.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-output-area.el.
  15. ;; If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;;; Code:
  19. (eval-when-compile (require 'cl))
  20. (require 'xml)
  21. (require 'ein-core)
  22. ;;; XML/HTML utils
  23. (defun ein:xml-parse-html-string (html-string)
  24. "Parse HTML-STRING and return a dom object which
  25. can be handled by the xml module."
  26. (with-temp-buffer
  27. (erase-buffer)
  28. (insert html-string)
  29. (libxml-parse-html-region (point-min) (point-max))))
  30. (defalias 'ein:xml-node-p 'listp)
  31. (defun ein:xml-tree-apply (dom operation)
  32. "Apply OPERATION on nodes in DOM. Apply the same OPERATION on
  33. the next level children when it returns `nil'."
  34. (loop for child in (xml-node-children dom)
  35. if (and (not (funcall operation child))
  36. (ein:xml-node-p child))
  37. do (ein:xml-tree-apply child operation)))
  38. (defun ein:xml-replace-attributes (dom tag attr replace-p replacer)
  39. "Replace value of ATTR of TAG in DOM using REPLACER
  40. when REPLACE-P returns non-`nil'."
  41. (ein:xml-tree-apply
  42. dom
  43. (lambda (node)
  44. (ein:and-let* (((ein:xml-node-p node))
  45. ((eq (xml-node-name node) tag))
  46. (attr-cell (assoc attr (xml-node-attributes node)))
  47. (val (cdr attr-cell))
  48. ((funcall replace-p val)))
  49. (setcdr attr-cell (funcall replacer val))
  50. t))))
  51. ;;; HTML renderer
  52. (defun ein:output-area-get-html-renderer ()
  53. ;; FIXME: make this configurable
  54. (cond
  55. ((and (fboundp 'shr-insert-document)
  56. (fboundp 'libxml-parse-xml-region))
  57. #'ein:insert-html-shr)
  58. (t #'ein:insert-read-only)))
  59. (defcustom ein:shr-env
  60. '((shr-table-horizontal-line ?-)
  61. (shr-table-vertical-line ?|)
  62. (shr-table-corner ?+))
  63. "Variables let-bound while calling `shr-insert-document'.
  64. To use default shr setting::
  65. (setq ein:shr-env nil)
  66. Draw boundaries for table (default)::
  67. (setq ein:shr-env
  68. '((shr-table-horizontal-line ?-)
  69. (shr-table-vertical-line ?|)
  70. (shr-table-corner ?+)))
  71. "
  72. :group 'ein)
  73. (defun ein:shr-insert-document (dom)
  74. "`shr-insert-document' with EIN setting."
  75. (eval `(let ,ein:shr-env (shr-insert-document dom))))
  76. (defun ein:insert-html-shr (html-string)
  77. "Render HTML-STRING using `shr-insert-document'.
  78. Usage::
  79. (ein:insert-html-shr \"<b>HTML</b> string\")
  80. "
  81. (let ((dom (ein:xml-parse-html-string html-string))
  82. (start (point))
  83. end)
  84. (ein:insert-html--fix-urls dom)
  85. (ein:shr-insert-document dom)
  86. (setq end (point))
  87. (put-text-property start end 'read-only t)
  88. (put-text-property start end 'front-sticky t)))
  89. (defun ein:insert-html--fix-urls (dom &optional url-or-port)
  90. "Destructively prepend notebook server URL to local URLs in DOM."
  91. (ein:and-let* ((url-or-port (or url-or-port (ein:get-url-or-port)))
  92. (replace-p (lambda (val) (string-match-p "^/?files/" val)))
  93. (replacer (lambda (val) (ein:url url-or-port val))))
  94. (ein:xml-replace-attributes dom 'a 'href replace-p replacer)
  95. (ein:xml-replace-attributes dom 'img 'src replace-p replacer)))
  96. (provide 'ein-output-area)
  97. ;;; ein-output-area.el ends here