Permalink
Browse files

Support all Hugo figure shortcode parameters

- Reference: https://gohugo.io/content-management/shortcodes/#figure
- Add a new test for "Image with Hugo figure shortcode parameters set
  using ATTR_HTML"
- Move the test for setting the figure shortcode "class" parameter
  from Image Links test to this new one.

Fixes #79
  • Loading branch information...
1 parent 2b09c2f commit 00e20ccb57bbf0bae1c84ad85ecee5cd47749d9f @kaushalmodi committed Sep 25, 2017
View
@@ -806,20 +806,33 @@ and rewrite link paths to make blogging more seamless."
((org-export-inline-image-p link org-html-inline-image-rules)
;; (message "[org-hugo-link DBG] processing an image: %s" contents)
(let* ((path (org-hugo--attachment-rewrite-maybe raw-path info))
- (caption (org-export-data
- (org-export-get-caption (org-export-get-parent-element link))
- info))
(parent (org-export-get-parent link))
(attr (org-export-read-attribute :attr_html parent))
- (class (plist-get attr :class)))
- (format "{{<figure src=\"%s\"%s%s>}}"
- path
- (if (org-string-nw-p caption)
- (format " caption=\"%s\"" caption)
- "")
- (if (org-string-nw-p class)
- (format " class=\"%s\"" class)
- ""))))
+ ;; Hugo `figure' shortcode named parameters
+ ;; https://gohugo.io/content-management/shortcodes/#figure
+ (caption (org-string-nw-p
+ (org-export-data ;Look for caption set using #+CAPTION
+ (org-export-get-caption (org-export-get-parent-element link))
+ info)))
+ (figure-params `((src . ,path)
+ (link . ,(plist-get attr :link))
+ (title . ,(plist-get attr :title))
+ (caption . ,(if caption
+ caption ;Caption set using #+CAPTION takes higher precedence
+ (plist-get attr :caption)))
+ (class . ,(plist-get attr :class))
+ (attr . ,(plist-get attr :attr))
+ (attrlink . ,(plist-get attr :attrlink))
+ (alt . ,(plist-get attr :alt))))
+ (figure-param-str ""))
+ (dolist (param figure-params)
+ (let ((name (car param))
+ (val (cdr param)))
+ (when val
+ (setq figure-param-str (concat figure-param-str
+ (format "%s=\"%s\" "
+ name val))))))
+ (format "{{<figure %s>}}" (org-trim figure-param-str))))
((string= type "coderef")
(let ((ref (org-element-property :path link)))
(format (org-export-get-coderef-format ref contents)
@@ -65,29 +65,6 @@ Click below image to jump to the unicorn image.
- NOTE :: =file:= has to be used in both Link and Description components
of the Org link.
-*** Image with =ATTR_HTML= [[https://github.com/kaushalmodi/ox-hugo/issues/17][Issue # 17]]
-#+ATTR_HTML: :class inset
-[[/images/org-mode-unicorn-logo.png]]
-
-[[https://github.com/kaushalmodi/ox-hugo/issues/17#issuecomment-313627728][Discussion]]
-**** COMMENT Below will not work!
-You cannot wrap markdown code inside HTML.
-
-As /rdwatters/ says [[https://discourse.gohugo.io/t/is-it-possible-to-insert-html-code-in-markdown-content/4867/4?u=kaushalmodi][here]],
-#+BEGIN_QUOTE
-HTML can be part of markdown because HTML-inside-markdown is part of
-the spec. That said, remember that the spec disallows markdown nested
-inside of HTML. So if you create a div, just make sure everything
-inside that div is valid HTML.
-#+END_QUOTE
-
-#+BEGIN_EXPORT md
-<div class="inset">
-#+END_EXPORT
-[[/images/org-mode-unicorn-logo.png]]
-#+BEGIN_EXPORT md
-</div>
-#+END_EXPORT
*** Link to image outside of standard Hugo =static= directory
[[../files-to-be-copied-to-static/static/images/copy-of-unicorn-logo.png]]
@@ -119,11 +96,67 @@ copied location inside =static=:
:PROPERTIES:
:EXPORT_DATE: 2017-07-19
:EXPORT_FILE_NAME: image-captions
+:CUSTOM_ID: image-captions
:END:
Some text before image.
#+CAPTION: A unicorn!
[[/images/org-mode-unicorn-logo.png]]
Some more text, after image.
+** Image with Hugo =figure= shortcode parameters set using =ATTR_HTML=
+:PROPERTIES:
+:EXPORT_FILE_NAME: figure-shortcode-and-attr-html
+:END:
+[[https://github.com/kaushalmodi/ox-hugo/issues/17][Issue # 17]]
+*** Setting =class= parameter
+#+ATTR_HTML: :class inset
+[[/images/org-mode-unicorn-logo.png]]
+
+[[https://github.com/kaushalmodi/ox-hugo/issues/17#issuecomment-313627728][Discussion]]
+**** COMMENT Below will not work!
+You cannot wrap markdown code inside HTML.
+
+As /rdwatters/ says [[https://discourse.gohugo.io/t/is-it-possible-to-insert-html-code-in-markdown-content/4867/4?u=kaushalmodi][here]],
+#+BEGIN_QUOTE
+HTML can be part of markdown because HTML-inside-markdown is part of
+the spec. That said, remember that the spec disallows markdown nested
+inside of HTML. So if you create a div, just make sure everything
+inside that div is valid HTML.
+#+END_QUOTE
+
+#+BEGIN_EXPORT md
+<div class="inset">
+#+END_EXPORT
+[[/images/org-mode-unicorn-logo.png]]
+#+BEGIN_EXPORT md
+</div>
+#+END_EXPORT
+*** Setting =alt= parameter
+[[https://www.reddit.com/r/emacs/comments/71wy6n/orgmode_as_a_markup_language_does_make_sense_even/dnhqudn/][Reference]]
+#+ATTR_HTML: :alt Org-mode Unicorn Logo
+[[/images/org-mode-unicorn-logo.png]]
+*** Setting =title= parameter
+#+ATTR_HTML: :title Logo
+[[/images/org-mode-unicorn-logo.png]]
+*** Setting image caption
+The image caption can be set in two ways.
+1. Using the Org =#+CAPTION= keyword
+2. Using =#+ATTR_HTML: :caption my caption=
+
+The =#+CAPTION= is available will get the higher precedence. In the
+below image, caption is set using that:
+
+#+INCLUDE: "./all-posts.org::#image-captions" :only-contents t
+
+Below, the same caption is set using the =#+ATTR_HTML= method instead:
+
+Some text before image.
+#+ATTR_HTML: :caption A unicorn!
+[[/images/org-mode-unicorn-logo.png]]
+Some more text, after image.
+*** Other
+Similarly, =:link=, =:attr=, =:attrlink= parameters in =#+ATTR_HTML=
+are also supported to set the corresponding parameter in the Hugo
+=figure= shortcode.
* Setting heading anchors
:PROPERTIES:
:EXPORT_FILE_NAME: setting-heading-anchors
@@ -0,0 +1,56 @@
++++
+title = "Image with Hugo figure shortcode parameters set using ATTR_HTML"
+tags = ["image"]
+draft = false
++++
+
+[Issue # 17](https://github.com/kaushalmodi/ox-hugo/issues/17)
+
+
+## Setting `class` parameter {#setting-class-parameter}
+
+{{<figure src="/images/org-mode-unicorn-logo.png" class="inset">}}
+
+[Discussion](https://github.com/kaushalmodi/ox-hugo/issues/17#issuecomment-313627728)
+
+
+## Setting `alt` parameter {#setting-alt-parameter}
+
+[Reference](https://www.reddit.com/r/emacs/comments/71wy6n/orgmode_as_a_markup_language_does_make_sense_even/dnhqudn/)
+
+{{<figure src="/images/org-mode-unicorn-logo.png" alt="Org-mode Unicorn Logo">}}
+
+
+## Setting `title` parameter {#setting-title-parameter}
+
+{{<figure src="/images/org-mode-unicorn-logo.png" title="Logo">}}
+
+
+## Setting image caption {#setting-image-caption}
+
+The image caption can be set in two ways.
+
+1. Using the Org `#+CAPTION` keyword
+2. Using `#+ATTR_HTML: :caption my caption`
+
+The `#+CAPTION` is available will get the higher precedence. In the
+below image, caption is set using that:
+
+Some text before image.
+
+{{<figure src="/images/org-mode-unicorn-logo.png" caption="A unicorn!">}}
+Some more text, after image.
+
+Below, the same caption is set using the `#+ATTR_HTML` method instead:
+
+Some text before image.
+
+{{<figure src="/images/org-mode-unicorn-logo.png" caption="A unicorn!">}}
+Some more text, after image.
+
+
+## Other {#other}
+
+Similarly, `:link`, `:attr`, `:attrlink` parameters in `#+ATTR_HTML`
+are also supported to set the corresponding parameter in the Hugo
+`figure` shortcode.
@@ -51,13 +51,6 @@ Click below image to jump to the unicorn image.
of the Org link.
-## Image with `ATTR_HTML` [Issue # 17](https://github.com/kaushalmodi/ox-hugo/issues/17) {#image-with-attr-html-issue-17}
-
-{{<figure src="/images/org-mode-unicorn-logo.png" class="inset">}}
-
-[Discussion](https://github.com/kaushalmodi/ox-hugo/issues/17#issuecomment-313627728)
-
-
## Link to image outside of standard Hugo `static` directory {#link-to-image-outside-of-standard-hugo-static-directory}
{{<figure src="/images/copy-of-unicorn-logo.png">}}

0 comments on commit 00e20cc

Please sign in to comment.