Skip to content

Commit

Permalink
Refine the external file link handling
Browse files Browse the repository at this point in the history
Add org-hugo-external-file-extensions-allowed-for-copying,
org-hugo-default-static-subdirectory-for-externals.

Copy external files to Hugo static only if they are newer or don't
exist in the destination dir.
  • Loading branch information
kaushalmodi committed Sep 14, 2017
1 parent 1a2b8d9 commit 4620759
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 13 deletions.
22 changes: 17 additions & 5 deletions doc/ox-hugo-manual.org
Expand Up @@ -634,9 +634,9 @@ directory too.
This is a unique feature of =ox-hugo=.

If a reference is made to a file outside the Hugo /static/ directory
and if it has one of these extensions: ="jpg"=, ="jpeg"=, ="tiff"=,
="png"=, ="pdf"=, ="odt"= , then that file is copied by =ox-hugo= to
the /static/ directory.
and if it has one of these extensions listed in
=org-hugo-external-file-extensions-allowed-for-copying=, then that
file is copied by =ox-hugo= to the /static/ directory.

Here is an example link:
#+BEGIN_SRC org
Expand All @@ -658,11 +658,23 @@ copied location inside =static=:
| =~/temp/static/foo.png= | =<HUGO_BASE_DIR>/static/foo.png= | (same as above) |
| =~/temp/static/articles/zoo.pdf= | =<HUGO_BASE_DIR>/static/articles/zoo.pdf= | (same as above) |
|----------------------------------+-------------------------------------------+----------------------------------------------------------------------------------------------------------------------|
| =~/temp/bar/baz/foo.png= | =<HUGO_BASE_DIR>/static/foo.png= | Here, as the *outside* path does not have =/static/=, the file is copied directly into the Hugo =static/= directory. |
|----------------------------------+-------------------------------------------+----------------------------------------------------------------------------------------------------------------------|
**** Source path does not contain =/static/=
[[../files-to-be-copied-to-static/foo/copy-2-of-unicorn-logo.png]]
|--------------------------+------------------------------------------+-----------------------------------------------------------------------------------------------------------------------|
| Outside =static= | Copied-to location inside =static= | Explanation |
|--------------------------+------------------------------------------+-----------------------------------------------------------------------------------------------------------------------|
| =~/temp/bar/baz/foo.png= | =<HUGO_BASE_DIR>/static/ox-hugo/foo.png= | Here, as the *outside* path does not have =/static/=, the file is copied to the =ox-hugo/= dir in Hugo =static/= dir. |
|--------------------------+------------------------------------------+-----------------------------------------------------------------------------------------------------------------------|
- Note :: The =ox-hugo= sub-directory name is because of the default
value of
=org-hugo-default-static-subdirectory-for-externals=.

See the [[https://github.com/kaushalmodi/ox-hugo/wiki/Usage#before-you-export][Usage → Before you export Wiki section]] to learn how to
set the *HUGO_BASE_DIR*.

- Note :: This auto-copying behavior can be disabled by setting
=org-hugo-external-file-extensions-allowed-for-copying= to
/nil/.
* COMMENT Local Variables :ARCHIVE:
# Local Variables:
# fill-column: 70
Expand Down
2 changes: 1 addition & 1 deletion doc/ox-hugo.wiki
11 changes: 10 additions & 1 deletion example-site/content-org/all-posts.org
Expand Up @@ -104,8 +104,17 @@ copied location inside =static=:
| =~/temp/static/img/foo.png= | =<HUGO_BASE_DIR>/static/img/foo.png= | (same as above) |
| =~/temp/static/foo.png= | =<HUGO_BASE_DIR>/static/foo.png= | (same as above) |
| =~/temp/static/articles/zoo.pdf= | =<HUGO_BASE_DIR>/static/articles/zoo.pdf= | (same as above) |
| =~/temp/bar/baz/foo.png= | =<HUGO_BASE_DIR>/static/foo.png= | Here, as the *outside* path does not have =/static/=, the file is copied directly into the Hugo =static/= directory. |
|----------------------------------+-------------------------------------------+----------------------------------------------------------------------------------------------------------------------|
**** Source path does not contain =/static/=
[[../files-to-be-copied-to-static/foo/copy-2-of-unicorn-logo.png]]
|--------------------------+------------------------------------------+-----------------------------------------------------------------------------------------------------------------------|
| Outside =static= | Copied-to location inside =static= | Explanation |
|--------------------------+------------------------------------------+-----------------------------------------------------------------------------------------------------------------------|
| =~/temp/bar/baz/foo.png= | =<HUGO_BASE_DIR>/static/ox-hugo/foo.png= | Here, as the *outside* path does not have =/static/=, the file is copied to the =ox-hugo/= dir in Hugo =static/= dir. |
|--------------------------+------------------------------------------+-----------------------------------------------------------------------------------------------------------------------|
- Note :: The =ox-hugo= sub-directory name is because of the default
value of
=org-hugo-default-static-subdirectory-for-externals=.
** Image captions
:PROPERTIES:
:EXPORT_DATE: 2017-07-19
Expand Down
16 changes: 14 additions & 2 deletions example-site/content/posts/image-links.md
Expand Up @@ -70,9 +70,21 @@ Example translations between outside `static` directory paths to the
copied location inside `static`:

Outside `static` | Copied-to location inside `static` | Explanation
---------------------------------|-------------------------------------------|-----------------------------------------------------------------------------------------------------------------------
---------------------------------|-------------------------------------------|-----------------------------------------------------------------------------------------------------------
`~/temp/static/images/foo.png` | `<HUGO_BASE_DIR>/static/images/foo.png` | If the **outside** path has `/static/` in it, the directory structure after that is preserved when copied.
`~/temp/static/img/foo.png` | `<HUGO_BASE_DIR>/static/img/foo.png` | (same as above)
`~/temp/static/foo.png` | `<HUGO_BASE_DIR>/static/foo.png` | (same as above)
`~/temp/static/articles/zoo.pdf` | `<HUGO_BASE_DIR>/static/articles/zoo.pdf` | (same as above)
`~/temp/bar/baz/foo.png` | `<HUGO_BASE_DIR>/static/foo.png` | Here, as the **outside** path does not have `/static/`, the file is copied directly into the Hugo `static/` directory.


### Source path does not contain `/static/` {#source-path-does-not-contain-static}

{{<figure src="/ox-hugo/copy-2-of-unicorn-logo.png">}}

Outside `static` | Copied-to location inside `static` | Explanation
-------------------------|------------------------------------------|------------------------------------------------------------------------------------------------------------------------
`~/temp/bar/baz/foo.png` | `<HUGO_BASE_DIR>/static/ox-hugo/foo.png` | Here, as the **outside** path does not have `/static/`, the file is copied to the `ox-hugo/` dir in Hugo `static/` dir.

- **Note:** The `ox-hugo` sub-directory name is because of the default
value of
`org-hugo-default-static-subdirectory-for-externals`.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 33 additions & 4 deletions ox-hugo.el
Expand Up @@ -397,6 +397,27 @@ Example value: (org)."
:type 'boolean
:safe #'booleanp)

(defcustom org-hugo-default-static-subdirectory-for-externals "ox-hugo"
"Default sub-directory in Hugo static directory for external files.
If the source path for external files does not contain
\"static\", `ox-hugo` cannot know what directory structure to
create inside the Hugo static directory. So all such files are
copied to this sub-directory inside the Hugo static directory."
:group 'org-export-hugo
:type 'string
:safe 'stringp)

(defcustom org-hugo-external-file-extensions-allowed-for-copying
'("jpg" "jpeg" "tiff" "png" "pdf" "odt")
"List of external file extensions allowed for copying to Hugo static dir.
If an Org link references a file with one of these extensions,
and if that file is not in the Hugo static directory, that file
is copied over to the static directory.
The auto-copying behavior is disabled if this variable is set to nil."
:group 'org-export-hugo
:type '(repeat string))


;;; Define Back-End

Expand Down Expand Up @@ -801,13 +822,14 @@ and rewrite link paths to make blogging more seamless."
Also rewrite image links.
PATH is the path to the image or pdf attachment. If the PATH
already exists in the Hugo \"static\" directory, just return the PATH.
already exists in the Hugo \"static\" directory, just return the
PATH.
INFO is a plist used as a communication channel."
;; (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* ((path-true (file-truename path))
(exportables '("jpg" "jpeg" "tiff" "png" "pdf" "odt"))
(exportables org-hugo-external-file-extensions-allowed-for-copying)
(static-dir (file-truename
(concat
(file-name-as-directory (plist-get info :hugo-base-dir))
Expand All @@ -827,7 +849,9 @@ INFO is a plist used as a communication channel."
(concat "/" (substring path-true (match-end 0))))
(let* ((file-name-sans-static (if (string-match "/static/" path-true)
(substring path-true (match-end 0))
(file-name-nondirectory path)))
(concat
(file-name-as-directory org-hugo-default-static-subdirectory-for-externals)
(file-name-nondirectory path))))
(static-path (concat static-dir file-name-sans-static))
(static-path-dir (file-name-directory static-path)))
;; The `static-dir' would already exist. But if
Expand All @@ -840,7 +864,12 @@ INFO is a plist used as a communication channel."
;; (message "[ox-hugo DBG attch rewrite] file-name: %s" file-name-sans-static)
;; (message "[ox-hugo DBG attch rewrite] static-path: %s" static-path)
;; (message "[ox-hugo DBG attch rewrite] static-path-dir: %s" static-path-dir)
(copy-file path-true static-path :ok-if-already-exists)

;; Do the copy only if the file to be copied is newer or
;; doesn't exist in the static dir.
(when (file-newer-than-file-p path-true static-path)
(message "[ox-hugo] Copied %S to %S" path-true static-path)
(copy-file path-true static-path :ok-if-already-exists))
(concat "/" file-name-sans-static))))
path)))

Expand Down

0 comments on commit 4620759

Please sign in to comment.