diff --git a/doc/ox-hugo-manual.org b/doc/ox-hugo-manual.org index 71c461f7..a1ae184f 100644 --- a/doc/ox-hugo-manual.org +++ b/doc/ox-hugo-manual.org @@ -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 @@ -658,11 +658,23 @@ copied location inside =static=: | =~/temp/static/foo.png= | =/static/foo.png= | (same as above) | | =~/temp/static/articles/zoo.pdf= | =/static/articles/zoo.pdf= | (same as above) | |----------------------------------+-------------------------------------------+----------------------------------------------------------------------------------------------------------------------| -| =~/temp/bar/baz/foo.png= | =/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= | =/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 diff --git a/doc/ox-hugo.wiki b/doc/ox-hugo.wiki index 58f8f118..8cb633da 160000 --- a/doc/ox-hugo.wiki +++ b/doc/ox-hugo.wiki @@ -1 +1 @@ -Subproject commit 58f8f118abd5a6494344ec89bd86ee6976b42f44 +Subproject commit 8cb633dade80e406301c3fed9f26079edfbe906c diff --git a/example-site/content-org/all-posts.org b/example-site/content-org/all-posts.org index 76fd3452..d90eaede 100644 --- a/example-site/content-org/all-posts.org +++ b/example-site/content-org/all-posts.org @@ -104,8 +104,17 @@ copied location inside =static=: | =~/temp/static/img/foo.png= | =/static/img/foo.png= | (same as above) | | =~/temp/static/foo.png= | =/static/foo.png= | (same as above) | | =~/temp/static/articles/zoo.pdf= | =/static/articles/zoo.pdf= | (same as above) | -| =~/temp/bar/baz/foo.png= | =/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= | =/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 diff --git a/example-site/content/posts/image-links.md b/example-site/content/posts/image-links.md index 701db9d2..63865305 100644 --- a/example-site/content/posts/image-links.md +++ b/example-site/content/posts/image-links.md @@ -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` | `/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` | `/static/img/foo.png` | (same as above) `~/temp/static/foo.png` | `/static/foo.png` | (same as above) `~/temp/static/articles/zoo.pdf` | `/static/articles/zoo.pdf` | (same as above) -`~/temp/bar/baz/foo.png` | `/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} + +{{
}} + +Outside `static` | Copied-to location inside `static` | Explanation +-------------------------|------------------------------------------|------------------------------------------------------------------------------------------------------------------------ +`~/temp/bar/baz/foo.png` | `/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`. diff --git a/example-site/files-to-be-copied-to-static/foo/copy-2-of-unicorn-logo.png b/example-site/files-to-be-copied-to-static/foo/copy-2-of-unicorn-logo.png new file mode 100755 index 00000000..e76f136b Binary files /dev/null and b/example-site/files-to-be-copied-to-static/foo/copy-2-of-unicorn-logo.png differ diff --git a/example-site/static/ox-hugo/copy-2-of-unicorn-logo.png b/example-site/static/ox-hugo/copy-2-of-unicorn-logo.png new file mode 100755 index 00000000..e76f136b Binary files /dev/null and b/example-site/static/ox-hugo/copy-2-of-unicorn-logo.png differ diff --git a/ox-hugo.el b/ox-hugo.el index 1154f359..e625aaf7 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -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 @@ -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)) @@ -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 @@ -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)))