Skip to content

Commit

Permalink
Support Page Bundles (Hugo v0.32+)
Browse files Browse the repository at this point in the history
- New property HUGO_BUNDLE, used to specify the bundle URL.
- If the HUGO_BUNDLE is non-nil, it is auto-appended to the full page URL.
- Attachment rewrite now "does the right thing".. copied the
  attachments to the bundle dir instead of to the static/ dir.
- Added a page bundle example, with images, "resources" front-matter.
- Updated the test site "single.html" template with debug info for
  .Resources.

Fixes #111
  • Loading branch information
kaushalmodi committed Jan 18, 2018
1 parent 57a28d1 commit 5512bad
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 22 deletions.
62 changes: 40 additions & 22 deletions ox-hugo.el
Expand Up @@ -630,6 +630,7 @@ newer."
(:hugo-preserve-filling "HUGO_PRESERVE_FILLING" nil org-hugo-preserve-filling) ;Preserve breaks so that text filling in Markdown matches that of Org
(:hugo-delete-trailing-ws "HUGO_DELETE_TRAILING_WS" nil org-hugo-delete-trailing-ws)
(:hugo-section "HUGO_SECTION" nil org-hugo-default-section-directory)
(:hugo-bundle "HUGO_BUNDLE" nil nil)
(:hugo-base-dir "HUGO_BASE_DIR" nil nil)
(:hugo-code-fence "HUGO_CODE_FENCE" nil t) ;Prefer to generate triple-backquoted Markdown code blocks by default.
(:hugo-menu "HUGO_MENU" nil nil)
Expand Down Expand Up @@ -1531,48 +1532,64 @@ INFO is a plist used as a communication channel."
;; (message "[ox-hugo attachment DBG] The Hugo base dir is: %s" (plist-get info :hugo-base-dir))
(let* ((path-true (file-truename path))
(exportables org-hugo-external-file-extensions-allowed-for-copying)
(bundle-dir (and (plist-get info :hugo-bundle)
(org-hugo--get-pub-dir info)))
(static-dir (file-truename
(concat
(file-name-as-directory (plist-get info :hugo-base-dir))
"static/"))))
"static/")))
(dest-dir (or bundle-dir static-dir))
ret)
;; (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)
;; (message "[ox-hugo DBG attch rewrite] bundle-dir: %s" bundle-dir)
;; (message "[ox-hugo DBG attch rewrite] dest-dir: %s" dest-dir)
(if (and (file-exists-p path-true)
(member (file-name-extension path) exportables)
(file-directory-p static-dir))
(file-directory-p dest-dir))
(progn
;; Check if `path-true' is already inside `static-dir'
(if (string-match (regexp-quote static-dir) path-true)
;; Check if `path-true' is already inside `dest-dir'.
(if (string-match (regexp-quote dest-dir) path-true)
(progn
;; If so, return *only* the path considering the
;; static directory as root.
(concat "/" (substring path-true (match-end 0))))
;; destination directory as root.
(setq ret (concat "/" (substring path-true (match-end 0)))))
(let* ((file-name-sans-static (if (string-match "/static/" path-true)
(substring path-true (match-end 0))
(concat
(file-name-as-directory org-hugo-default-static-subdirectory-for-externals)
(if bundle-dir
""
(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
(dest-path (concat dest-dir file-name-sans-static))
(dest-path-dir (file-name-directory dest-path)))
;; The `dest-dir' would already exist. But if
;; `file-name-sans-static' is "images/image.png" or
;; "foo/bar.txt", it's likely that "`static-dir'/images"
;; or "`static-dir'/foo" might not exist. So create
;; those if needed below.
(unless (file-exists-p static-path-dir)
(mkdir static-path-dir :parents))
;; "foo/bar.txt", it's likely that "`dest-dir'/images"
;; or "`dest-dir'/foo" might not exist. So create those
;; if needed below.
(unless (file-exists-p dest-path-dir)
(mkdir dest-path-dir :parents))
;; (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)
;; (message "[ox-hugo DBG attch rewrite] dest-path: %s" dest-path)
;; (message "[ox-hugo DBG attch rewrite] dest-path-dir: %s" dest-path-dir)

;; 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)))
(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
;; If attachments are copied to the bundle
;; directory, don't prefix the path as "/"
;; as those paths won't exist at the site
;; base URL.
file-name-sans-static
(concat "/" file-name-sans-static))))))
(setq ret path))
;; (message "[ox-hugo DBG attch rewrite] returned path: %s" ret)
ret))

;;;; Paragraph
(defun org-hugo-paragraph (paragraph contents info)
Expand Down Expand Up @@ -2476,6 +2493,7 @@ are \"toml\" and \"yaml\"."
"HUGO_ALLOW_SPACES_IN_TAGS"
"HUGO_BLACKFRIDAY"
"HUGO_SECTION"
"HUGO_BUNDLE"
"HUGO_BASE_DIR"
"HUGO_CODE_FENCE"
"HUGO_MENU"
Expand Down
50 changes: 50 additions & 0 deletions test/site/content-org/all-posts.org
Expand Up @@ -1797,6 +1797,56 @@ Hash char at beginning of a continued line
Markdown]

This ! does not need to be escaped as there is no ambiguity.
* Page Bundles :page_bundles:
:PROPERTIES:
:EXPORT_HUGO_SECTION: bundles
:END:
** Page Bundle A
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: page-bundle-a
:END:
*** Page A
:PROPERTIES:
:EXPORT_FILE_NAME: index
:EXPORT_HUGO_RESOURCES: :src "images/copy-of-*.png" :title "First copy of Org mode logo"
:EXPORT_HUGO_RESOURCES+: :src "copy-2-*.png" :title "Second copy of Org mode logo"
:END:
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]]

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/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. |
|--------------------------+------------------------------------------------------+------------------------------------------------------------------------------------------------------|
*** Bundled page foo
:PROPERTIES:
:EXPORT_FILE_NAME: foo
:END:
"Foo" page in /Page Bundle A/.
*** Bundled page bar
:PROPERTIES:
:EXPORT_FILE_NAME: bar
:END:
"Bar" page in /Page Bundle A/.
* Footnotes Test :footnote:
** Footnotes 1
:PROPERTIES:
Expand Down
7 changes: 7 additions & 0 deletions test/site/content/bundles/page-bundle-a/bar.md
@@ -0,0 +1,7 @@
+++
title = "Bundled page bar"
tags = ["page-bundles"]
draft = false
+++

"Bar" page in _Page Bundle A_.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions test/site/content/bundles/page-bundle-a/foo.md
@@ -0,0 +1,7 @@
+++
title = "Bundled page foo"
tags = ["page-bundles"]
draft = false
+++

"Foo" page in _Page Bundle A_.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions test/site/content/bundles/page-bundle-a/index.md
@@ -0,0 +1,44 @@
+++
title = "Page A"
tags = ["page-bundles"]
draft = false
[[resources]]
src = "images/copy-of-*.png"
title = "First copy of Org mode logo"
[[resources]]
src = "copy-2-*.png"
title = "Second copy of Org mode logo"
+++

Index page of _Page Bundle A_.


## Link to images not in the current directory {#link-to-images-not-in-the-current-directory}


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

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

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/` {#source-path-does-not-contain-static}

{{<figure src="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. |
27 changes: 27 additions & 0 deletions test/site/themes/bare_min/layouts/_default/single.html
Expand Up @@ -61,10 +61,37 @@ <h1 class="post-title">{{ .Title }}</h1>
</p>
{{ .Content }}

{{ with .Resources }}
<hr>
<h3>Resources</h3>
<ul>
{{ range . }}
{{ if .RelPermalink }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ else }}
<li>{{ .ResourceType | title }} (no link, only embedded in page)</li>
{{ end }}
{{ end }}
</ul>
{{ end }}

{{ with .Resources.ByType "page" }}
<hr>
<h3 id="sub-pages">Bundled Sub-pages</h3>
{{ range . }}
<h4>{{ .Title }}</h4>
{{ .Content }}
{{ end }}
{{ end }}

<hr>
<a id="debug"></a>
<h3 id="page-debug">Page Params (Debug)</h3>
{{ partial "debugprint.html" .Params }}
{{ with .Resources }}
<h3 id="resources-debug">Resources (Debug)</h3>
{{ partial "debugprint.html" . }}
{{ end }}
<h3 id="file-debug">File Object (Debug)</h3>
{{ partial "debugprint.html" (slice "File" .) }}
</div>
Expand Down

0 comments on commit 5512bad

Please sign in to comment.