Skip to content

Commit

Permalink
Adjustment to path translation when path contains "/static/"
Browse files Browse the repository at this point in the history
If an attachment path contains "/static/", that attachment is copied
to the site's static/ dir *even in page bundles*.

This allows one to have links to files in the static/ dir as well as
within the bundle dir, in the case of page bundles.
  • Loading branch information
kaushalmodi committed Feb 15, 2018
1 parent 6679abd commit 458d654
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 41 deletions.
9 changes: 8 additions & 1 deletion ox-hugo.el
Expand Up @@ -1883,6 +1883,8 @@ INFO is a plist used as a communication channel."
"static/")))
(dest-dir (or bundle-dir static-dir))
ret)
(unless (file-directory-p static-dir)
(user-error "Please create the %s directory" static-dir))
;; (message "[ox-hugo DBG attch rewrite] Image export dir is: %s" static-dir)
;; (message "[ox-hugo DBG attch rewrite] path: %s" path)
;; (message "[ox-hugo DBG attch rewrite] path-true: %s" path-true)
Expand All @@ -1905,6 +1907,10 @@ INFO is a plist used as a communication channel."
;; `path-true' is "/foo/static/bar/baz.png",
;; return "bar/baz.png".
;; (message "[ox-hugo DBG attch rewrite] path contains static")
;; If path-true contains "/static/", set the
;; `dest-dir' to `static-dir' (even if this is a
;; page bundle).
(setq dest-dir static-dir)
(substring path-true (match-end 0)))
(bundle-dir
(cond
Expand Down Expand Up @@ -1958,7 +1964,8 @@ INFO is a plist used as a communication channel."
(when (file-newer-than-file-p path-true dest-path)
(message "[ox-hugo] Copied %S to %S" path-true dest-path)
(copy-file path-true dest-path :ok-if-already-exists))
(setq ret (if bundle-dir
(setq ret (if (and bundle-dir
(string= bundle-dir dest-dir))
;; If attachments are copied to the bundle
;; directory, don't prefix the path as "/"
;; as those paths won't exist at the site
Expand Down
40 changes: 21 additions & 19 deletions test/site/content-org/all-posts.org
Expand Up @@ -93,6 +93,9 @@ Click below image to jump to the unicorn image.

[[file:/images/org-mode-unicorn-logo.png][file:/images/org-mode-unicorn-logo.png]]
*** Link to image outside of standard Hugo =static= directory
:PROPERTIES:
:CUSTOM_ID: path-containing-static
:END:
[[../files-to-be-copied-to-static/static/images/copy-of-unicorn-logo.png]]

If you link to files outside of the Hugo =static= directory, ensure
Expand All @@ -109,6 +112,9 @@ 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) |
|----------------------------------+-------------------------------------------+----------------------------------------------------------------------------------------------------------------------|

The above path translations (when path contains =static=) hold true
even when inside page bundle pages.
**** Source path does not contain =/static/=
[[../files-to-be-copied-to-static/foo/copy-2-of-unicorn-logo.png]]
|--------------------------+------------------------------------------+-----------------------------------------------------------------------------------------------------------------------|
Expand Down Expand Up @@ -1878,37 +1884,33 @@ This is the content for the "bundles" section landing page.
Index page of /Page Bundle A/.
**** Link to images not in the current directory
***** Source path contains =/static/=
[[../files-to-be-copied-to-static/static/images/copy-of-unicorn-logo.png]]
When path contains =/static/=, the path translations are the exact
same as those for non-bundle cases.

[[/posts/image-links/#path-containing-static][More details]]

If you link to files not in the *current directory*, ensure that the
path contains =/static/= if you would like to preserve the directory
structure.

Example translations between the =static=-containing image paths to
the copied location inside the bundle:
|----------------------------------+---------------------------------------------------------------+----------------------------------------------------------------------------------------------------------|
| Outside =static= | Copied-to location inside BUNDLE | Explanation |
|----------------------------------+---------------------------------------------------------------+----------------------------------------------------------------------------------------------------------|
| =~/temp/static/images/foo.png= | =<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/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>/content/<SECTION>/<BUNDLE>/img/foo.png= | (same as above) |
| =~/temp/static/foo.png= | =<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/foo.png= | (same as above) |
| =~/temp/static/articles/zoo.pdf= | =<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/articles/zoo.pdf= | (same as above) |
|----------------------------------+---------------------------------------------------------------+----------------------------------------------------------------------------------------------------------|
***** Source path does not contain =/static/=
[[../files-to-be-copied-to-static/static/images/copy-of-unicorn-logo-page-bundle.png]]
***** Source path contains the *bundle name*
|-------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------|
| Inside =<ORG_FILE_DIR>= | Copied-to location inside BUNDLE | Explanation |
|-------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------|
| =<ORG_FILE_DIR>/bar/<BUNDLE>/baz/foo.png= | =<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/baz/foo.png= | If the file directory path contains ="/<BUNDLE>/"=, the directory structure following that ="/<BUNDLE>/"= is preserved. |
|-------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------|
See [[/images-in-content/page-bundle-images-in-same-dir/][this other test]] for an example.
***** Source path contains neither =/static/= nor the *bundle name*
[[../files-to-be-copied-to-static/foo/copy-2-of-unicorn-logo.png]]
|----------------------------------+--------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------|
| Outside =static= | Copied-to location inside BUNDLE | Explanation |
|----------------------------------+--------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------|
| =~/temp/bar/baz/foo.png= | =<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/foo.png= | Here, as the *outside* path does not have =/static/=, the file is copied directly to the BUNDLE dir. |
|----------------------------------+--------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------|
****** Same image, but hyperlinked to itself
****** Same image, but hyperlinked
[[../files-to-be-copied-to-static/foo/copy-2-of-unicorn-logo.png][file:../files-to-be-copied-to-static/foo/copy-2-of-unicorn-logo.png]]
****** Page Bundles with images in the same dir as content Org file
***** Page Bundles with images in the same dir as content Org file
|-------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------|
| Inside =<ORG_FILE_DIR>= | Copied-to location inside BUNDLE | Explanation |
|-------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------|
| =<ORG_FILE_DIR>/bar/baz/foo.png= | =<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/bar/baz/foo.png= | Even if the *outside* path does not have =/static/=, it is still inside the same dir as the Org file, so the directory structure is preserved. |
| =<ORG_FILE_DIR>/bar/<BUNDLE>/baz/foo.png= | =<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/baz/foo.png= | If the file directory path contains ="/<BUNDLE>/"=, the directory structure following that ="/<BUNDLE>/"= is preserved. |
|-------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------|
See [[/images-in-content/page-bundle-images-in-same-dir/][this other test]] for an example.
*** Bundled page foo
Expand Down
37 changes: 18 additions & 19 deletions test/site/content/bundles/page-bundle-a/index.md
Expand Up @@ -18,24 +18,24 @@ Index page of _Page Bundle A_.

### Source path contains `/static/` {#source-path-contains-static}

{{< figure src="images/copy-of-unicorn-logo.png" >}}
When path contains `/static/`, the path translations are the exact
same as those for non-bundle cases.

If you link to files not in the **current directory**, ensure that the
path contains `/static/` if you would like to preserve the directory
structure.
[More details](/posts/image-links/#path-containing-static)

Example translations between the `static`-containing image paths to
the copied location inside the bundle:
{{< figure src="/images/copy-of-unicorn-logo-page-bundle.png" >}}

| Outside `static` | Copied-to location inside BUNDLE | Explanation |
|----------------------------------|---------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| `~/temp/static/images/foo.png` | `<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/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>/content/<SECTION>/<BUNDLE>/img/foo.png` | (same as above) |
| `~/temp/static/foo.png` | `<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/foo.png` | (same as above) |
| `~/temp/static/articles/zoo.pdf` | `<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/articles/zoo.pdf` | (same as above) |

### Source path contains the **bundle name** {#source-path-contains-the-bundle-name}

### Source path does not contain `/static/` {#source-path-does-not-contain-static}
| Inside `<ORG_FILE_DIR>` | Copied-to location inside BUNDLE | Explanation |
|-------------------------------------------|----------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------|
| `<ORG_FILE_DIR>/bar/<BUNDLE>/baz/foo.png` | `<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/baz/foo.png` | If the file directory path contains `"/<BUNDLE>/"`, the directory structure following that `"/<BUNDLE>/"` is preserved. |

See [this other test](/images-in-content/page-bundle-images-in-same-dir/) for an example.


### Source path contains neither `/static/` nor the **bundle name** {#source-path-contains-neither-static-nor-the-bundle-name}

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

Expand All @@ -44,16 +44,15 @@ the copied location inside the bundle:
| `~/temp/bar/baz/foo.png` | `<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/foo.png` | Here, as the **outside** path does not have `/static/`, the file is copied directly to the BUNDLE dir. |


#### Same image, but hyperlinked to itself {#same-image-but-hyperlinked-to-itself}
#### Same image, but hyperlinked {#same-image-but-hyperlinked}

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


#### Page Bundles with images in the same dir as content Org file {#page-bundles-with-images-in-the-same-dir-as-content-org-file}
### Page Bundles with images in the same dir as content Org file {#page-bundles-with-images-in-the-same-dir-as-content-org-file}

| Inside `<ORG_FILE_DIR>` | Copied-to location inside BUNDLE | Explanation |
|-------------------------------------------|--------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| `<ORG_FILE_DIR>/bar/baz/foo.png` | `<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/bar/baz/foo.png` | Even if the **outside** path does not have `/static/`, it is still inside the same dir as the Org file, so the directory structure is preserved. |
| `<ORG_FILE_DIR>/bar/<BUNDLE>/baz/foo.png` | `<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/baz/foo.png` | If the file directory path contains `"/<BUNDLE>/"`, the directory structure following that `"/<BUNDLE>/"` is preserved. |
| Inside `<ORG_FILE_DIR>` | Copied-to location inside BUNDLE | Explanation |
|----------------------------------|--------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| `<ORG_FILE_DIR>/bar/baz/foo.png` | `<HUGO_BASE_DIR>/content/<SECTION>/<BUNDLE>/bar/baz/foo.png` | Even if the **outside** path does not have `/static/`, it is still inside the same dir as the Org file, so the directory structure is preserved. |

See [this other test](/images-in-content/page-bundle-images-in-same-dir/) for an example.
7 changes: 5 additions & 2 deletions test/site/content/posts/image-links.md
Expand Up @@ -58,7 +58,7 @@ NOTE
Here's the same link with `#+name` specified.. which should also be
clickable.

<a id="org43d74a3"></a>
<a id="org49ba17b"></a>
{{< figure src="/images/org-mode-unicorn-logo.png" link="/images/org-mode-unicorn-logo.png" >}}


Expand All @@ -73,7 +73,7 @@ Click below image to jump to the unicorn image.
{{< figure src="/images/org-mode-unicorn-logo.png" link="/images/org-mode-unicorn-logo.png" >}}


## Link to image outside of standard Hugo `static` directory {#link-to-image-outside-of-standard-hugo-static-directory}
## Link to image outside of standard Hugo `static` directory {#path-containing-static}

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

Expand All @@ -91,6 +91,9 @@ 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) |

The above path translations (when path contains `static`) hold true
even when inside page bundle pages.


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

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 458d654

Please sign in to comment.