[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/htmlize 61760b4 075/134: Implement the `htmlize-link' prop
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/htmlize 61760b4 075/134: Implement the `htmlize-link' property to embed links in the HTML. |
Date: |
Sat, 7 Aug 2021 09:17:10 -0400 (EDT) |
branch: elpa/htmlize
commit 61760b4714aa35461d519b9a9e8175058a95bed3
Author: Hrvoje Niksic <hniksic@gmail.com>
Commit: Hrvoje Niksic <hniksic@gmail.com>
Implement the `htmlize-link' property to embed links in the HTML.
---
htmlize.el | 90 ++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 59 insertions(+), 31 deletions(-)
diff --git a/htmlize.el b/htmlize.el
index 9399e22..6fa5978 100644
--- a/htmlize.el
+++ b/htmlize.el
@@ -468,38 +468,62 @@ next-single-char-property-change")))
(car list)
(apply #'concat list)))
+(defun htmlize-format-link (linkprops text)
+ (let ((uri (if (stringp linkprops)
+ linkprops
+ (plist-get linkprops :uri))))
+ (if uri
+ (format "<a href=\"%s\">%s</a>" (htmlize-attr-escape uri) text)
+ (htmlize-protect-string text))))
+
+(defun htmlize-escape-or-link (string)
+ ;; Escape STRING and/or add hyperlinks.
+ (let ((pos 0) (end (length string)) outlist)
+ (while (< pos end)
+ (let* ((link (get-char-property pos 'htmlize-link string))
+ (next-link-change (next-single-property-change
+ pos 'htmlize-link string end))
+ (chunk (substring string pos next-link-change)))
+ (push
+ (if link
+ (htmlize-format-link link chunk)
+ (htmlize-protect-string chunk))
+ outlist)
+ (setq pos next-link-change)))
+ (htmlize-concat (nreverse outlist))))
+
(defun htmlize-display-prop-to-html (display text)
(let (desc)
(cond ((stringp display)
;; Emacs ignores recursive display properties.
- (htmlize-protect-string display))
+ (htmlize-escape-or-link display))
((not (eq (car-safe display) 'image))
(htmlize-protect-string text))
((null (setq desc (funcall htmlize-transform-image
(cdr display) text)))
- (htmlize-protect-string text))
+ (htmlize-escape-or-link text))
((stringp desc)
- (htmlize-protect-string desc))
+ (htmlize-escape-or-link desc))
(t
(htmlize-generate-image desc text)))))
(defun htmlize-string-to-html (string)
;; Convert the string to HTML, including images attached as
- ;; `display' property. In a string without `display', this is
- ;; equivalent to `htmlize-protect-string'.
- (let ((pos 0) (end (length string))
- chunk display outlist next-display-change)
+ ;; `display' property and links as `htmlize-link' property. In a
+ ;; string without images or links, this is equivalent to
+ ;; `htmlize-protect-string'.
+ (let ((pos 0) (end (length string)) outlist)
(while (< pos end)
- (setq display (get-char-property pos 'display string)
- next-display-change (next-single-property-change
- pos 'display string end)
- chunk (substring string pos next-display-change))
- (push
- (if display
- (htmlize-display-prop-to-html display chunk)
- (htmlize-protect-string chunk))
- outlist)
- (setq pos next-display-change))
+ (let* ((display (get-char-property pos 'display string))
+ (next-display-change (next-single-property-change
+ pos 'display string end))
+ (chunk (substring string pos next-display-change)))
+ (push
+ (if display
+ (htmlize-display-prop-to-html display chunk)
+ (htmlize-escape-or-link chunk))
+ outlist)
+ (setq pos next-display-change)))
(htmlize-concat (nreverse outlist))))
(defun htmlize-default-transform-image (imgprops _text)
@@ -617,22 +641,26 @@ list."
(apply #'concat (nreverse textlist)))
text)))
+(defun htmlize-copy-prop (prop beg end string)
+ ;; Copy the specified property from the specified region of the
+ ;; buffer to the target string. We cannot rely on Emacs to copy the
+ ;; property because we want to handle properties coming from both
+ ;; text properties and overlays.
+ (let ((pos beg))
+ (while (< pos end)
+ (let ((value (get-char-property pos prop))
+ (next-change (htmlize-next-change pos 'display end)))
+ (when value
+ (put-text-property (- pos beg) (- next-change beg)
+ prop value string))
+ (setq pos next-change)))))
+
(defun htmlize-get-text-with-display (beg end)
;; Like buffer-substring-no-properties, except it copies the
- ;; `display' property from the buffer, if found. We cannot rely on
- ;; Emacs to copy the property because it can come from either text
- ;; properties or overlays, and the latter can't be attached to
- ;; strings.
- (let ((pos beg)
- (text (buffer-substring-no-properties beg end))
- display next-change)
- (while (< pos end)
- (setq display (get-char-property pos 'display)
- next-change (htmlize-next-change pos 'display end))
- (when display
- (put-text-property (- pos beg) (- next-change beg)
- 'display display text))
- (setq pos next-change))
+ ;; `display' property from the buffer, if found.
+ (let ((text (buffer-substring-no-properties beg end)))
+ (htmlize-copy-prop 'display beg end text)
+ (htmlize-copy-prop 'htmlize-link beg end text)
(unless running-xemacs
(setq text (htmlize-add-before-after-strings beg end text)))
text))
- [nongnu] elpa/htmlize bb3bda0 030/134: Trim comment., (continued)
- [nongnu] elpa/htmlize bb3bda0 030/134: Trim comment., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize c19d29c 012/134: Version 0.67., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 9ccd74f 010/134: Version 0.64., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 9349d4b 014/134: Version 1.4., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 2e7cb2c 021/134: Version 1.37., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 1a14031 022/134: New file: .gitignore, ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 665abf9 024/134: Use `put' instead of `plist-put'., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 814e537 026/134: Update instructions for git., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize bcf1d44 025/134: Update copyright and compress individual years to ranges., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 3477a7d 027/134: Bump version., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 61760b4 075/134: Implement the `htmlize-link' property to embed links in the HTML.,
ELPA Syncer <=
- [nongnu] elpa/htmlize 296b18e 033/134: Extract a bit of code outside buffer-substring-no-invisible., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize e3d020e 059/134: Remove explicit references to MULE., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 5dbea0b 079/134: Convert the auto-link-creation machinery to use the `htmlize-link' property., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize bad6391 082/134: Look for PROP., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 380ba4b 084/134: Bumped version., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 5b29cde 090/134: Update NEWS., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 7eb326a 099/134: Fix package header format, ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 4570d0a 103/134: Add README., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 8f46e10 105/134: Use htmlpreview., ELPA Syncer, 2021/08/07
- [nongnu] elpa/htmlize 78318a2 114/134: Support face-remapping-alist. (#13), ELPA Syncer, 2021/08/07