Skip to content

Commit

Permalink
Support captions for inlined SVGs
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Jan 10, 2019
1 parent e57f417 commit 527e53d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
2 changes: 2 additions & 0 deletions doc/ox-hugo-manual.org
Expand Up @@ -2798,12 +2798,14 @@ Simply add ~#+attr_html: :inlined t~ above the SVG file link.
Here's an example of inlining an SVG:

#+begin_src org
,#+caption: An SVG with hyperlink
,#+attr_html: :inlined t
[[file:../test/site/content-org/svg-with-hyperlinks.svg]]
#+end_src

.. which results in:

#+caption: An SVG with hyperlink
#+attr_html: :inlined t
[[file:../test/site/content-org/svg-with-hyperlinks.svg]]
*** COMMENT Hugo Bundle
Expand Down
68 changes: 39 additions & 29 deletions ox-hugo.el
Expand Up @@ -2029,12 +2029,39 @@ and rewrite link paths to make blogging more seamless."
grand-parent
parent))
(attr (org-export-read-attribute :attr_html useful-parent))
(caption (or
;; Caption set using #+caption takes higher precedence.
(org-string-nw-p
(org-export-data ;Look for caption set using #+caption
(org-export-get-caption (org-export-get-parent-element link))
info))
(plist-get attr :caption)))
(caption (when (org-string-nw-p caption)
(format "%s%s%s%s"
;; Tue Feb 13 11:32:45 EST 2018 - kmodi
;; Add the span tag once
;; https://github.com/gohugoio/hugo/issues/4406
;; gets resolved.
"" ;"<span class=\\\"figure-number\\\">"
(format (org-html--translate
(concat
(cdr (assoc 'figure org-blackfriday--org-element-string))
" %d:")
info)
(org-export-get-ordinal
useful-parent info
nil #'org-html--has-caption-p))
" " ;" </span>"
;; Escape the double-quotes, if any,
;; present in the caption.
(replace-regexp-in-string "\"" "\\\\\"" caption))))
(extension (downcase (file-name-extension raw-path)))
(inlined-svg (and (stringp extension)
(string= "svg" extension)
(plist-get attr :inlined))))
;; (message "[ox-hugo-link DBG] Inline image: %s, extension: %s" raw-path extension)
;; (message "[ox-hugo-link DBG] inlined svg? %S" inlined-svg)
;; (message "[ox-hugo-link DBG] caption: %s" caption)
(if inlined-svg
(let* ((svg-contents (with-temp-buffer
(insert-file-contents raw-path)
Expand All @@ -2047,43 +2074,26 @@ and rewrite link paths to make blogging more seamless."
;; Remove the xml document tag as that cannot be inlined in-between
;; a Markdown (or even an HTML) file.
"<\\?xml version=\"1\\.0\" encoding=\"UTF-8\" standalone=\"no\"\\?>" ""
svg-contents))))
svg-contents)))
(caption-html (if (not caption)
""
;; (caption-str
;; (org-html-convert-special-strings ;Interpret em-dash, en-dash, etc.
;; (org-export-data-with-backend caption 'html info)))
(format (concat "\n\n<div class=\"figure-caption\">\n"
" %s\n"
"</div>")
caption))))
;; (message "[ox-hugo-link DBG] svg contents: %s" svg-contents)
;; (message "[ox-hugo-link DBG] svg contents sanitized: %s" svg-contents-sanitized)
svg-contents-sanitized)
(concat svg-contents-sanitized caption-html))
(let* ((path (org-hugo--attachment-rewrite-maybe raw-path info))
(inline-image (not (org-html-standalone-image-p useful-parent info)))
(source (if link-is-url
(concat type ":" path)
path))
(num-attr (/ (length attr) 2)) ;(:alt foo) -> num-attr = 1
(alt-text (plist-get attr :alt))
(caption (or
;;Caption set using #+caption takes higher precedence
(org-string-nw-p
(org-export-data ;Look for caption set using #+caption
(org-export-get-caption (org-export-get-parent-element link))
info))
(plist-get attr :caption)))
(caption (when (org-string-nw-p caption)
(format "%s%s%s%s"
;; Tue Feb 13 11:32:45 EST 2018 - kmodi
;; Add the span tag once
;; https://github.com/gohugoio/hugo/issues/4406
;; gets resolved.
"" ;"<span class=\\\"figure-number\\\">"
(format (org-html--translate
(concat
(cdr (assoc 'figure org-blackfriday--org-element-string))
" %d:")
info)
(org-export-get-ordinal
useful-parent info
nil #'org-html--has-caption-p))
" " ;" </span>"
;; Escape the double-quotes, if any,
;; present in the caption.
(replace-regexp-in-string "\"" "\\\\\"" caption)))))
(alt-text (plist-get attr :alt)))
;; (message "[ox-hugo-link DBG] path: %s" path)
;; (message "[ox-hugo-link DBG] inline image? %s" inline-image)
;; (message "[org-hugo-link DBG] attr: %s num of attr: %d"
Expand Down
1 change: 1 addition & 0 deletions test/site/content-org/all-posts.org
Expand Up @@ -364,6 +364,7 @@ start
:[[https://ox-hugo.scripter.co/ ox-hugo homepage]];
stop
#+end_src
#+caption: An SVG with hyperlinks generated using PlantUML
#+attr_html: :inlined t
#+RESULTS:
[[file:svg-with-hyperlinks.svg]]
Expand Down
4 changes: 4 additions & 0 deletions test/site/content/posts/inlined-svg.md
Expand Up @@ -49,3 +49,7 @@ x1="80.5" x2="80.5" y1="83.9688" y2="103.9688"/><polygon
fill="#A80036"
points="76.5,93.9688,80.5,103.9688,84.5,93.9688,80.5,97.9688"
style="stroke: #A80036; stroke-width: 1.0;"/></g></svg>

<div class="figure-caption">
Figure 1: An SVG with hyperlinks generated using PlantUML
</div>

0 comments on commit 527e53d

Please sign in to comment.