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.

133 lines
3.9 KiB

  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. (require 'xml)
  20. (require 'ein-core)
  21. ;;; XML/HTML utils
  22. (defun ein:xml-parse-html-string (html-string)
  23. "Parse HTML-STRING and return a dom object which
  24. can be handled by the xml module."
  25. (with-temp-buffer
  26. (erase-buffer)
  27. (insert html-string)
  28. (if (fboundp 'libxml-parse-html-region)
  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. (cl-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. :type '(sexp)
  73. :group 'ein)
  74. (defun ein:shr-insert-document (dom)
  75. "`shr-insert-document' with EIN setting."
  76. (eval `(let ,ein:shr-env (shr-insert-document dom))))
  77. (defun ein:insert-html-shr (html-string)
  78. "Render HTML-STRING using `shr-insert-document'.
  79. Usage::
  80. (ein:insert-html-shr \"<b>HTML</b> string\")
  81. "
  82. (let ((dom (ein:xml-parse-html-string html-string))
  83. (start (point))
  84. end)
  85. (ein:insert-html--fix-urls dom)
  86. (ein:shr-insert-document dom)
  87. (setq end (point))
  88. (put-text-property start end 'read-only t)
  89. (put-text-property start end 'front-sticky t)))
  90. (defun ein:insert-html--fix-urls (dom &optional url-or-port)
  91. "Destructively prepend notebook server URL to local URLs in DOM."
  92. (ein:and-let* ((url-or-port (or url-or-port (ein:get-url-or-port)))
  93. (replace-p (lambda (val) (string-match-p "^/?files/" val)))
  94. (replacer (lambda (val) (ein:url url-or-port val))))
  95. (ein:xml-replace-attributes dom 'a 'href replace-p replacer)
  96. (ein:xml-replace-attributes dom 'img 'src replace-p replacer)))
  97. (provide 'ein-output-area)
  98. ;;; ein-output-area.el ends here