How To

Ditching ZenPhoto for Hugo?

My adventure of moving from ZenPhoto to Hugo did not go as expected.

This started out as a 230 word bullet point list of how to do things. It’s now two entire posts!

(Almost) Leaving ZenPhoto (Almost) Leaving ZenPhoto

I have to preface this by telling you that I didn’t in the end. Yeah. I know. I planned to. And here’s why:

I like ZenPhoto a lot. It’s a great Gallery tool, and after Gallery2 went to Gallery3 and I hated it, I was quite in love with them. But. They’ve started to inch towards being a general CMS and the whole ZenPhoto/ZenPhoto20 split left a weird taste in my mouth.

Plus the DB I have is already 65 megs, which has started to behave a little oddly. Now I have 38,534 images in 885 albums, and to a degree that makes sense. If you think about it in WordPress terms, I have 885 taxonomies (tags and categories), and 38,534 posts. Given that each post in this example would be a sentence at most (describing the image), the WordPress DB would be around 15megs based on some napkin math I did. Validating this, there’s a WordPress Joke DB with 40k Jokes and it’s only 14 megs.

Oh and did I mention that ZenPhoto has no export feature? I guess there’s a reason the first PHP script I wrote was a simple Gallery. All I really want is to display images in a structured format.

But. As I said before, I did not leave ZenPhoto! Why not? Because I was unable to do what I wanted to in pure Hugo, and because porting everything over to WordPress is incredibly hard and complex.

Top ↑

Why Not WordPress? Why Not WordPress?

WordPress has a lot of awesome advantages. It auto generates thumbnails for one, which Hugo does not, so porting the gallery over would save me one level of complication. But I have 38,534 images to import. No matter how you slice that, I’d have to import them and associate them with the right post. Probably manually.

With a static generator, I can very easily assign a variable for each album to say ‘get images from this folder and display them.’ And if there are folders, list each folder as a link to another page. By contrast, I would have to use a plugin like NextGen gallery to do this in WordPress, and while I do like the plugin, it’s got the same issue that ZenPhoto has for me. It’s doing too much. And, to be honest, part of this process is to limit my potential security issues. Adding in really big, really complex plugins is the opposite of that, no matter how rigorously reviewed they are.

Besides, if I hated it, I knew I could import MD files into WordPress. Someone already did it with Jekyll after all.

Top ↑

Get and Grep the Data Get and Grep the Data

No matter what I needed the data. Thankfully I knew how to do this with phpMyAdmin already. I dropped the _albums table in JSON format. Why JSON? Well as I’d already learned, I could generate a Markdown file from a JSON pretty easily. Of course, I got stumped on having it loop through the JSON file, but I have a plan for this.

On export, albums.json was 643 KB while images.json was 61.4 MB. And they were all one line. I’m not using images.json just yet.

I tossed it into BBedit and scrubbed the data, removing everything I didn’t need. This did not change the file size at first, since I also made the JSON human readable. There was a lot of gripping here, but I took each entry from 16-22 items down to six: tags, categories, folder path, title, description, date. And the file went down to 143 KB. One-sixth, more or less.

Finally I had to split each entry because, as I’d learned, trying to loop through a nested JSON array in grunt is complicated. So fine, I’ll do it manually. But how? Answer: By loving linux. You can split files!

$ split -a 6 -l 8 ../albums.json album-

There’s also csplit, which is contextual split. Either way, I now have 885 album-xxx.json files. Whew.

Top ↑

Rename and Move the Files Rename and Move the Files

I messed around with linux commands to move files based on their folder paths and rename them based on the last variable. I dreaded having to just move them and then going to mess with the renames. So after two days, I asked Twitter and was saved by Mike Little and Kailey Lampert. Between them I learned that there was a jq tool for messing with JSON (homebrew install it) and that a shell script is a godsend.

#! /bin/sh

# awk script:
# set field seperator to :
# strip comma from field 2
# strip quotes from field 2
# strip lead space from field 2
# strip trail space from field 2
# print field 2

for FILE in `ls *.json` ; do
    NAME=`cat $FILE | grep folder | awk 'BEGIN { FS= ":" } { 
        gsub( ",", "", $2 );   \
        gsub( "\"", "", $2 );  \
        gsub(/^[ \t]+/,"",$2); \
        gsub(/[ \t]+$/,"",$2); \
        print $2 
    mkdir -p $NAME;
    cp "$FILE" "$NAME/index.json"

I ended up changing it to reduce future broken URLs. But basically everything except the mkdir comes from Mike. He’s my hero.

Top ↑

To Be Continued To Be Continued

Once I had all the files, it was time to change them all to Markdown files and whip out my templates!

But you’ll need to wait for that because this post got really long.