Fix the image hyperlinks

* ox-hugo.el (org-hugo-link): Update the type=="file" case in the
  final t case of the cond.  Now the file:// prefix is not added if
  the raw-path begins with "/images/" (or "/HUGO_STATIC_IMAGES/").

Cleanup:

- Move raw-path to the first let form in org-hugo-link; no need to get
  that value multiple times in the fn.
- Get value of :hugo-static-images from info
- Replace occurrences of:

    (if (not foo) bar
      baz)

  with

    (if foo
        baz
      bar)
- Misc code consistency edits
master
Kaushal Modi 9 years ago
parent 44a3a0d3b3
commit f02b4808a0
  1. 36
      example-site/content-org/all-posts.org
  2. 46
      example-site/content/posts/image.md
  3. 71
      ox-hugo.el

@ -33,10 +33,7 @@ H H=.
/Need to add smarts that determines which subtree contains the whole
post.. like look for =EXPORT_FILE_NAME=?/
** Unclickable image
*This works!*
** Unclickable image (works!)
[[/images/org-mode-unicorn-logo.png]]
*To be fixed*: The sub-headings in a post get exported as /Heading 1/
@ -53,38 +50,11 @@ instead of
So the sub-heading title and the post title both get the /Heading 1/
tag and look the same size.
** TODO Clickable link that opens the image
** Clickable link that opens the image (works!)
[[/images/org-mode-unicorn-logo.png][Click here to see the unicorn]]
*** FIXME
Above:
#+BEGIN_EXAMPLE
[[/images/org-mode-unicorn-logo.png][Click here to see the unicorn]]
#+END_EXAMPLE
exports to:
#+BEGIN_EXAMPLE
[Click here to see the unicorn](file:///images/org-mode-unicorn-logo.png)
#+END_EXAMPLE
instead of:
#+BEGIN_EXAMPLE
[Click here to see the unicorn](/images/org-mode-unicorn-logo.png)
#+END_EXAMPLE
** TODO Clickable image that opens the image
** Clickable image that opens the image (works!)
Click below image to jump to the unicorn image.
[[file:/images/org-mode-unicorn-logo.png][file:/images/org-mode-unicorn-logo.png]]
*** FIXME
Above:
#+BEGIN_EXAMPLE
[[file:/images/org-mode-unicorn-logo.png][file:/images/org-mode-unicorn-logo.png]]
#+END_EXAMPLE
exports to:
#+BEGIN_EXAMPLE
[![img](/images/org-mode-unicorn-logo.png)](file:///images/org-mode-unicorn-logo.png)
#+END_EXAMPLE
instead of:
#+BEGIN_EXAMPLE
[![img](/images/org-mode-unicorn-logo.png)](/images/org-mode-unicorn-logo.png)
#+END_EXAMPLE
- NOTE :: =file:= has to be used in both Link and Description components
of the Org link.

@ -1,6 +1,6 @@
+++
title = "Image"
date = 2017-07-07T15:33:35-04:00
date = 2017-07-07T18:44:51-04:00
tags = []
+++
@ -9,9 +9,7 @@ tags = []
*Need to add smarts that determines which subtree contains the whole post.. like look for `EXPORT_FILE_NAME`?*
# Unclickable image
**This works!**
# Unclickable image (works!)
![img](/images/org-mode-unicorn-logo.png)
@ -28,46 +26,14 @@ instead of
So the sub-heading title and the post title both get the *Heading 1* tag and look the same size.
# Clickable link that opens the image
[Click here to see the unicorn](file:///images/org-mode-unicorn-logo.png)
## FIXME
Above:
[[/images/org-mode-unicorn-logo.png][Click here to see the unicorn]]
exports to:
[Click here to see the unicorn](file:///images/org-mode-unicorn-logo.png)
instead of:
[Click here to see the unicorn](/images/org-mode-unicorn-logo.png)
# Clickable image that opens the image
Click below image to jump to the unicorn image.
[![img](/images/org-mode-unicorn-logo.png)](file:///images/org-mode-unicorn-logo.png)
## FIXME
Above:
[[file:/images/org-mode-unicorn-logo.png][file:/images/org-mode-unicorn-logo.png]]
# Clickable link that opens the image (works!)
exports to:
[Click here to see the unicorn](/images/org-mode-unicorn-logo.png)
[![img](/images/org-mode-unicorn-logo.png)](file:///images/org-mode-unicorn-logo.png)
instead of:
# Clickable image that opens the image (works!)
[![img](/images/org-mode-unicorn-logo.png)](/images/org-mode-unicorn-logo.png)
Click below image to jump to the unicorn image. [![img](/images/org-mode-unicorn-logo.png)](/images/org-mode-unicorn-logo.png)
- **NOTE:** `file:` has to be used in both Link and Description components of the Org link.

@ -140,10 +140,11 @@ and rewrite link paths to make blogging more seamless."
(if (string= ".org" (downcase (file-name-extension raw-path ".")))
(concat (file-name-sans-extension raw-path) ".md")
raw-path)))
(raw-path (org-element-property :path link))
(images-dir (org-string-nw-p (plist-get info :hugo-static-images)))
(type (org-element-property :type link)))
(message "[ox-hugo DBG] link filename is : %s" (expand-file-name (plist-get (car (cdr link)) :path)))
(message "[ox-hugo DBG] link type is %s" type)
(message "[ox-hugo DBG] link ")
(message "[ox-hugo-link DBG] link filename: %s" (expand-file-name (plist-get (car (cdr link)) :path)))
(message "[ox-hugo-link DBG] link type: %s" type)
(cond
;; Link type is handled by a special function.
((org-export-custom-protocol-maybe link contents 'md))
@ -154,8 +155,9 @@ and rewrite link paths to make blogging more seamless."
(pcase (org-element-type destination)
(`plain-text ;External file
(let ((path (funcall link-org-files-as-md destination)))
(if (not contents) (format "<%s>" path)
(format "[%s](%s)" contents path))))
(if contents
(format "[%s](%s)" contents path)
(format "<%s>" path))))
(`headline
(format
"[%s](#%s)"
@ -183,38 +185,44 @@ and rewrite link paths to make blogging more seamless."
description
(org-export-get-reference destination info))))))))
((org-export-inline-image-p link org-html-inline-image-rules)
(message "org-hugo-link processing an image %s" contents)
(message "[org-hugo-link DBG] processing an image: %s" contents)
(let ((path (org-hugo--attachment-rewrite
(let ((raw-path (org-element-property :path link)))
(if (not (file-name-absolute-p raw-path)) raw-path
(expand-file-name raw-path)))
(if (file-name-absolute-p raw-path)
(expand-file-name raw-path)
raw-path)
info))
(caption (org-export-data
(org-export-get-caption
(org-export-get-parent-element link))
info)))
(format "![img](%s)"
(if (not (org-string-nw-p caption)) path
(format "%s \"%s\"" path caption)))))
(format "![img](%s)" (if (org-string-nw-p caption)
(format "%s \"%s\"" path caption)
path))))
((string= type "coderef")
(let ((ref (org-element-property :path link)))
(format (org-export-get-coderef-format ref contents)
(org-export-resolve-coderef ref info))))
((equal type "radio") contents)
(t (let* ((raw-path (org-element-property :path link))
(path
(cond
((member type '("http" "https" "ftp"))
(concat type ":" raw-path))
((string= type "file")
(org-hugo--attachment-rewrite
(org-export-file-uri
(funcall link-org-files-as-md raw-path))
info))
(t raw-path))))
(if (not contents) (format "<%s>" path)
(format "[%s](%s)" contents path)))))))
((equal type "radio")
contents)
(t
(let ((path (cond
((member type '("http" "https" "ftp"))
(concat type ":" raw-path))
((and (string= type "file")
(or (null images-dir)
;; Do not add the "file://" prefix if the
;; raw-path begins with the HUGO_STATIC_IMAGES
;; dir name.
(not (string-match-p (concat "\\`/" images-dir "/") raw-path))))
(org-hugo--attachment-rewrite
(org-export-file-uri
(funcall link-org-files-as-md raw-path))
info))
(t
raw-path))))
(if contents
(format "[%s](%s)" contents path)
(format "<%s>" path)))))))
;;;;; Helpers
@ -224,9 +232,9 @@ Also rewrite image links.
PATH is the path to the image or pdf attachment.
INFO is a plist used as a communication channel."
(message "[ox-hugo DBG] The Hugo sectioimage dir is: %s" (plist-get info :hugo-static-images) )
(message "[ox-hugo DBG] The Hugo section is: %s" (plist-get info :hugo-section) )
(message "[ox-hugo DBG] The Hugo base dir is: %s" (plist-get info :hugo-base-dir) )
(message "[ox-hugo attachment DBG] The Hugo images dir is: %s" (plist-get info :hugo-static-images))
(message "[ox-hugo attachment DBG] The Hugo section is: %s" (plist-get info :hugo-section))
(message "[ox-hugo attachment DBG] The Hugo base dir is: %s" (plist-get info :hugo-base-dir))
(let* ((full-path (file-truename path))
(exportables '("jpg" "jpeg" "tiff" "png" "pdf" "odt" ))
@ -458,8 +466,7 @@ Return output file's name."
level tags commentedp
(and (eq 1 level)
(not (member "noexport" tags))
(not commentedp))
)
(not commentedp)))
(if (and (eq 1 level)
(not (member "noexport" tags))
(not commentedp))

Loading…
Cancel
Save