edit-server-htmlize.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;;; edit-server-htmlize.el --- (de)HTMLization hooks for edit-server.el
  2. ;;; Copyright (C) 2013 Roland McGrath
  3. ;; Author: Roland McGrath <roland@hack.frob.com>
  4. ;; Maintainer: Roland McGrath <roland@hack.frob.com>
  5. ;; Version: 0.2
  6. ;; This file is not part of GNU Emacs.
  7. ;; This file is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 3, or (at your option)
  10. ;; any later version.
  11. ;; This file is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This is intended for use with edit-server.el and the corresponding
  19. ;; "Edit with Emacs" extension for the Chromium/Google Chrome Web browser.
  20. ;; See https://github.com/stsquad/emacs_chrome
  21. ;;
  22. ;; This provides some simple functions for deHTMLizing and reHTMLizing
  23. ;; "plain text" as encoded by e.g. GMail composition boxes.
  24. ;;; Code:
  25. ;; (add-hook 'edit-server-start-hook 'edit-server-maybe-dehtmlize-buffer)
  26. ;; (add-hook 'edit-server-done-hook 'edit-server-maybe-htmlize-buffer)
  27. (require 'edit-server)
  28. (require 'html2text)
  29. (defconst edit-server-htmlize-entity-alist
  30. '(("<" . "&lt;")
  31. (">" . "&gt;")
  32. ("&" . "&amp;"))
  33. "Alist of strings that cannot safely appear inside an HTML <pre> element.
  34. This maps a string to its HTML entity string.")
  35. (defconst edit-server-htmlize-regexp
  36. (mapconcat (lambda (pair) (concat "\\(" (regexp-quote (car pair)) "\\)"))
  37. edit-server-htmlize-entity-alist
  38. "\\|"))
  39. (defconst edit-server-htmlize-replacements
  40. (apply 'vector (mapcar 'cdr edit-server-htmlize-entity-alist)))
  41. (defun edit-server-htmlize-replace (regexp replacements)
  42. (save-match-data
  43. (while (re-search-forward regexp nil t)
  44. (let ((i 0))
  45. (while (and (< i (length replacements))
  46. (null (match-beginning (1+ i))))
  47. (setq i (1+ i)))
  48. (replace-match (aref replacements i) t t)))))
  49. (defun edit-server-htmlize-buffer ()
  50. "Do a simple HTMLification of the buffer as plain text.
  51. This produces HTML intended to reproduce the original plain text contents
  52. of the buffer."
  53. (interactive)
  54. (save-excursion
  55. (goto-char (point-min))
  56. (insert "<pre>")
  57. (edit-server-htmlize-replace edit-server-htmlize-regexp
  58. edit-server-htmlize-replacements)
  59. (goto-char (point-max))
  60. (insert "</pre>")))
  61. ;; XXX Modified from 23.3.1's gnus/html2text.el html2text,
  62. ;; which wants to eat <br> and refill paragraphs.
  63. (defun edit-server-dehtmlize-buffer ()
  64. "Convert HTML to plain text in the current buffer.
  65. This differs from \\[html2text] in that it doesn't refill paragraphs,
  66. but only turns <br> tags into line breaks."
  67. (interactive)
  68. (save-excursion
  69. (let ((case-fold-search t)
  70. (buffer-read-only nil)
  71. (edit-server-remove-list (append (list "span") html2text-remove-tag-list)))
  72. (html2text-replace-string "<br>" "\n" (point-min) (point-max))
  73. (html2text-replace-string "</div>" "</div>\n" (point-min) (point-max))
  74. (html2text-remove-tags edit-server-remove-list)
  75. (html2text-format-tags)
  76. (html2text-remove-tags html2text-remove-tag-list2)
  77. (edit-server-html2text-substitute)
  78. (html2text-format-single-elements))))
  79. (defconst edit-server-html2text-substitute-regexp
  80. (mapconcat (lambda (pair) (concat "\\(" (regexp-quote (car pair)) "\\)"))
  81. html2text-replace-list
  82. "\\|"))
  83. (defconst edit-server-html2text-substitute-replacements
  84. (apply 'vector (mapcar 'cdr html2text-replace-list)))
  85. ;; XXX Replacement for 23.3.1's gnus/html2text.el html2text-substitute,
  86. ;; which is confounded by "&amp;lt;" and the like.
  87. (defun edit-server-html2text-substitute ()
  88. "See the variable `html2text-replace-list' for documentation."
  89. (interactive)
  90. (goto-char (point-min))
  91. (edit-server-htmlize-replace edit-server-html2text-substitute-regexp
  92. edit-server-html2text-substitute-replacements))
  93. ;;;###autoload
  94. (defcustom edit-server-htmlize-url-regexp
  95. (concat "^" (regexp-quote "mail.google.com/mail/"))
  96. "*Regexp matching `edit-server-url' in a buffer that should be HTMLized.
  97. See `edit-server-maybe-htmlize-buffer'."
  98. :type 'regexp
  99. :group 'edit-server
  100. :require 'edit-server-htmlize)
  101. ;;;###autoload
  102. (defun edit-server-maybe-htmlize-buffer ()
  103. "Possibly HTMLize the current buffer of plain text.
  104. This calls `edit-server-htmlize-buffer' if `edit-server-url'
  105. matches `edit-server-htmlize-url-regexp'.
  106. This is intended for use on `edit-server-done-hook'."
  107. (interactive)
  108. (if (string-match edit-server-htmlize-url-regexp edit-server-url)
  109. (edit-server-htmlize-buffer)))
  110. ;;;###autoload
  111. (defun edit-server-maybe-dehtmlize-buffer ()
  112. "Possibly deHTMLize the current buffer into plain text.
  113. This calls `edit-server-dehtmlize-buffer' if `edit-server-url'
  114. matches `edit-server-htmlize-url-regexp'.
  115. This is intended for use on `edit-server-start-hook'."
  116. (interactive)
  117. (if (string-match edit-server-htmlize-url-regexp edit-server-url)
  118. (edit-server-dehtmlize-buffer)))
  119. (provide 'edit-server-htmlize)
  120. ;;; edit-server-htmlize.el ends here