emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Standalone hyperlinked images in HTML export
@ 2013-06-30 20:07 Kodi Arfer
  2013-06-30 22:08 ` Bastien
  2013-07-01 21:01 ` Nicolas Goaziou
  0 siblings, 2 replies; 6+ messages in thread
From: Kodi Arfer @ 2013-06-30 20:07 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1709 bytes --]

The manual explains in "Images in HTML export" that you can make an 
image a hyperlink like this:

[[file:highres.jpg][file:thumb.jpg]]

where thumb.jpg becomes the <img> 'src' and highres.jpg becomes the <a> 
'href'. One might infer it should also be possible to link to something 
other than an image, like this:

[[http://gnu.org][http://example.com/gnu-head.jpg]]

For example, try exporting this file:

#+begin_src org
Some initial text.

[[http://example.com/a.png]]

Some text between images 1 and 2.

[[http://eeyup.com][http://example.com/b.png]]

Some text between images 2 and 3.

http://example.com/c.png

Some trailing text.
#+end_src

You do indeed get

<a href="http://eeyup.com"><img  src="http://example.com/b.png" 
alt="b.png" /></a>

in the output, but the exporter doesn't regard the image as standalone, 
so it doesn't get put in a <div> (or, in HTML5 mode, <figure>) like the 
others, and if you add a #+CAPTION, no caption will be included.

The attached patch to master shows how this can be fixed, but I hesitate 
to recommend applying it because two new bugs are immediately apparent:

1. Figure numbers are screwed up. If you add #+CAPTIONs to each image in 
the above file, the figure numbers go Figure 1, Figure 3, Figure 4.

2. The <a ...> ... </a> gets wrapped around the whole <div> or <figure>, 
not just the <img>. This breaks markup validity (<div> isn't allowed in 
<a>) and makes the caption, if you have one, into a giant hyperlink.

I think I'm going to stop working on this issue for now, but at least 
what I've done could be helpful for anybody else who wants to go further 
down the rabbit hole.

P.S. I sent a request for a copyright-assignment form earlier today.

[-- Attachment #2: 0001-ox-html-Allow-standalone-images-to-be-hyperlinked.patch --]
[-- Type: text/x-patch, Size: 3340 bytes --]

From fe74b3507795f2664291250250bc24b943f8f12b Mon Sep 17 00:00:00 2001
From: Kodi Arfer <git@arfer.net>
Date: Sun, 30 Jun 2013 15:40:33 -0400
Subject: [PATCH] ox-html: Allow standalone images to be hyperlinked

* lisp/ox-html.el (org-html-standalone-image-p): If the link is
  the description of another link, look one more element up the
  tree to find `paragraph'.
* lisp/ox.el (org-export-inline-image-p): When the description
  is a link, test the description instead of the path.

TINYCHANGE
---
 lisp/ox-html.el |  9 ++++++---
 lisp/ox.el      | 38 ++++++++++++++++++++++++--------------
 2 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index 4753e66..4091bcc 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -2579,9 +2579,12 @@ standalone images, do the following.
 	  \(org-element-property :caption paragraph\)\)\)"
   (let ((paragraph (case (org-element-type element)
 		     (paragraph element)
-		     (link (and (org-export-inline-image-p
-				 element org-html-inline-image-rules)
-				(org-export-get-parent element)))
+		     (link (let ((x (and (org-export-inline-image-p
+					  element org-html-inline-image-rules)
+					 (org-export-get-parent element))))
+			     (if (eq (org-element-type x) 'link)
+				 (org-export-get-parent x)
+			     x)))
 		     (t nil))))
     (when (eq (org-element-type paragraph) 'paragraph)
       (when (or (not (and (boundp 'org-html-standalone-image-predicate)
diff --git a/lisp/ox.el b/lisp/ox.el
index 08fbddd..9179576 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -3919,20 +3919,30 @@ type is TYPE.  The function will return a non-nil value if any of
 the provided rules is non-nil.  The default rule is
 `org-export-default-inline-image-rule'.
 
-This only applies to links without a description."
-  (and (not (org-element-contents link))
-       (let ((case-fold-search t)
-	     (rules (or rules org-export-default-inline-image-rule)))
-	 (catch 'exit
-	   (mapc
-	    (lambda (rule)
-	      (and (string= (org-element-property :type link) (car rule))
-		   (string-match (cdr rule)
-				 (org-element-property :path link))
-		   (throw 'exit t)))
-	    rules)
-	   ;; Return nil if no rule matched.
-	   nil))))
+This only applies to links without a description, unless the
+description is itself a link, as for hyperlinked images in HTML.
+In this case, the test is applied to the description instead of
+the path."
+  (let* ((cs (org-element-contents link))
+         (dlink (and (= (length cs) 1)
+                     (eq (org-element-type (car cs)) 'link)
+                     (car cs))))
+    (when (or dlink (not (org-element-contents link)))
+      (message "dlink: %s" (show-org-element dlink))
+      (when dlink
+        (setq link dlink))
+      (let ((case-fold-search t)
+            (rules (or rules org-export-default-inline-image-rule)))
+        (catch 'exit
+          (mapc
+           (lambda (rule)
+             (and (string= (org-element-property :type link) (car rule))
+                  (string-match (cdr rule)
+                                (org-element-property :path link))
+                  (throw 'exit t)))
+           rules)
+          ;; Return nil if no rule matched.
+          nil)))))
 
 (defun org-export-resolve-coderef (ref info)
   "Resolve a code reference REF.
-- 
1.8.1.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-07-02 19:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-30 20:07 Standalone hyperlinked images in HTML export Kodi Arfer
2013-06-30 22:08 ` Bastien
2013-07-01 11:50   ` Nicolas Goaziou
2013-07-01 21:01 ` Nicolas Goaziou
2013-07-01 21:26   ` Kodi Arfer
2013-07-02 19:13     ` Nicolas Goaziou

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).