Here is a quick script to convert from a flat Hugo content structure to a leaf bundle, including images.
You can set & change your input glob & output paths from the cmd input arguments without changing code yourself.
From flat:
`-- content
|-- about.md
`-- post
|-- content-1.md
|-- content-2.md
|-- content-3.md
`-- static
`|-- images
|-- image-1.jpg
|-- image-2.jpg
|-- image-3.jpg
To page bundles:
`-- content
|-- about.md
`-- post
`-- content-1
|-- index.md
`-- images
|-- image-1.jpg
`-- content-2
|-- index.md
`-- images
|-- image-1.jpg
|-- image-2.jpg
Tested with image tags taking all the following formats:
![image 1 here](/img/img1.jpg)
![image 4 here](http://localhost:1313/img/img4.jpg)
![](/img/img2-arb.jpg)
NOT particularly tested with image-within-link tags or the more unusual .md image styles.
This is what it looks like when you run:
$ pypyr ops/flat-to-bundle in='content/posts/post*.md' out=content/out in_img=static out_img=images
--> processing this path: content/posts/post*.md
--> processing file: content/posts/post-4.md
--> writing to: content/out/post-4/index.md
--> processing file: content/posts/post1.md
--> writing to: content/out/post1/index.md
--> processing file: content/posts/post2.md
--> writing to: content/out/post2/index.md
--> processing file: content/posts/post3.md
--> writing to: content/out/post3/index.md
--> processed images
static/img/img1.jpg
static/img/img2-arb.jpg
static/img/img4.jpg
done!
Input arguments:
-
in
: look for a single file or bunch of files depending on this input path or glob -
out
: create the bundles in this output directory -
in_img
: root directory for input images. Defaultstatic
. If you keep your images inassets
, change that here. -
out_img
: sub-directory for images in the output bundle
Note this does NOT delete or move any files, for the safety of your file-system - it creates new files/copies in out
instead. If you are happy with the run/conversion, you can delete the image files yourself with the handy list of --> processed images
it gives you at the end of processing on the console output. I also didn’t want to make assumptions about whether an image is in use on multiple pages, or other pages outside of the input glob scope, so another reason for not moving/deleting but copying instead.
Tested on MacOS and Linux - I tried to make sure not to make POSIX assumptions about paths, so it should work on Windows too (assuming you’re using powershell or WSL), but I haven’t actually tested on Windows so no promises!
Final Note: I say “quick” script meaning that although it works I didn’t particularly bother to optimise anything about this or test every possible permutation in the world - this style of content conversion is likely to be a once-off task for any given project so prob not deserving of the n-th degree of optimisation, but feel free to be in touch if you want to improve on anything!