kc0bfv / autophugo

AutoPhugo [ˌɔtoʊˈfjuːgəʊ] is a gallery/photoblog theme for Hugo that's a little more automatic than Phugo.
Other
97 stars 56 forks source link

Question: How to refresh content/abc/_index.md? #13

Open Klaus-Tockloth opened 3 years ago

Klaus-Tockloth commented 3 years ago

I have something like this ('content/nature/_index.md'):

---
title: "Natur"
date: 2021-02-19T13:34:21+01:00
albumthumb: "Nature/frederik-lower-tatFzQW0a3s-unsplash.jpg"
resources:
- src: Nature/frederik-lower-tatFzQW0a3s-unsplash.jpg
  phototitle: Grass cat
  description: This cat stalks through the grass
- src: Nature/geran-de-klerk-wYy3rvvgjAU-unsplash.jpg
  phototitle: Grass cat
  description: This cat stalks through the grass
---

If I add further pictures to 'assets/nature/' ... how to refresh _index.md (without loosing the manually added phototitles and descriptions)?

kc0bfv commented 3 years ago

Oh man, I wish there was a way to do this. I looked around the Internet a bit for a hugo feature like that. I don't think there's something built-in right now, and it's not something that'd get added as part of the theme.

The problem is - the default.md archetype file has code in it that runs and builds the structure out for any of the photos it finds. (I think that was @githubmonkey's cool feature add - I didn't even realize hugo would run code in there.) But it only runs when you use "hugo new". I don't see anything like "hugo refresh" - but also the way everything is setup right now I can see how that would be really tough to make in a general case.

I think the fastest option would be to use "hugo new new_content.md" or something like that to create a new list of all the content again. Then I would dump all that stuff onto the bottom of my _index.md file and delete the parts that I don't want. That goes quickly... But it'd still be tedious once everything turned into a lot of content.

Another option I've used for similar problems is to use a database of filename / metadata, then have a separate script scan for the new files and update the database and output the content in the format Hugo needs. Actually - since YAML usually has programming language libraries easily available, it would be easy to use the file itself and the frontmatter as the database. But I'd still have to write a separate script to re-read the directory content and update the stuff in there.

Ok - now I'm thinking, there's probably a way I could do this in the general case and produce the output as needed by hugo. Thing is - I'd have to write this as a Python script (or similar). I could do that - but folks would have to have a Python interpreter on their system to use it. I can't think of a way to make this work using like, go templates, or something else Hugo would handle itself.

I might do this still. It'd be in Python. It'd be in like a separate "utilities" directory in the theme. Not sure if that's helpful to you and others though.

MrRaph commented 2 years ago

Hi ! :-)

I wrote a Bash script to handle that in my GitHub Action :

#!/bin/bash

echo "Generating Gallery"
cd ./sites/gallery

echo ">> Generating albums"
for album in $(find ./assets -type d | sed 's|./assets/||' | grep -ve 'images')
do
    if [[ ! -f ./content/$album/_index.md ]]
    then
        hugo new $album/_index.md
    fi

    sed -i.back '/^$/d' ./content/$album/_index.md

    grep 'resources:' ./content/$album/_index.md >> /dev/null
    if [[ $? -ne 0 ]]
    then
        sed -i.back '5d' ./content/$album/_index.md
        echo 'resources:' >> ./content/$album/_index.md
    else
        sed -i.back '$d' ./content/$album/_index.md
    fi

    for image in $(find ./assets/${album} -type f | sed 's|./assets/||' | grep -e "jpeg" -e "jpg" -e "png")
    do
        grep $image ./content/$album/_index.md >> /dev/null
        if [[ $? -ne 0 ]]
        then
            echo " - src: \"${image}\"" >> ./content/$album/_index.md
            echo "   phototitle: \"$(basename $image | awk -F'.' '{print $1}' )\"" >> ./content/$album/_index.md
            echo "   #description: \"${image}\"" >> ./content/$album/_index.md

            ### EXIF
            export LC_ALL=C
            echo "   description: \"$(exif ./assets/${image} | grep -e '^Model' | awk -F'|' '{print $2}') - $(exif ./assets/${image} | grep -e '^Lens Model' | awk -F'|' '{print $2}') : ISO $(exif ./assets/${image} | grep -e '^ISO Speed Ratings' | awk -F'|' '{print $2}'), $(exif ./assets/${image} | grep -e '^Exposure Time' | awk -F'|' '{print $2}'), $(exif ./assets/${image} | grep -e '^F-Number' | awk -F'|' '{print $2}')\"" >> ./content/$album/_index.md
            unset LC_ALL
        fi
    done

    echo '---' >> ./content/$album/_index.md

    sed -i.back 's/draft: true/draft: false/' ./content/$album/_index.md
    find ./content -name "_index.md.back" -exec rm -rf {} \;

done

This script will also gather EXIF data from each picture and add this information to the picture description.

You can see the result here : MrRaph_ Photo - Gallery

Hope it'll help you :-)

iHarshad commented 12 months ago

I am thinking an intermediary built using core Python and eliminating external dependancies. Most Linux distributions out there come with some version of Python 3.x pre-packaged so this should work out-of-the-box even in build systems and pipelines. The script runs and generates a JSON file in the Hugo data directory with details of all our images. This JSON file can act as our single file database (like sqlite) which is updated programatically by the script. When images are added/removed from the image folder, the script will accordingly update references in the JSON database file.

Sample JSON structure ```json { "name": "gallery", "type": "folder", "children": [ { "name": "album1", "type": "folder", "children": [ { "name": "landscape.jpg", "type": "file", "file_md5": "ccddidd", "title": "This is image title which is not maintained by script", "description": "This is image title which is not maintained by script" }, { "name": "portrait.jpg", "type": "file", "file_md5": "bbccdde", "title": "This is image title which is not maintained by script", "description": "This is image title which is not maintained by script" } ] }, { "name": "album2", "type": "folder", "children": [ { "name": "vacation.jpg", "type": "file", "file_md5": "aabbccc", "title": "This is image title which is not maintained by script", "description": "This is image title which is not maintained by script" } ] } ] } ```

We can then edit theme templates to consume the JSON file and build the necessary pages. We don't need to rely on Markdown files in the content directory. We also have the option to create multiple such single file databases, one for each album so that it is easier to tackle raw JSOn when updating image titles and descriptions.

Further reading:

Update: You might want to take a look at bep/galleriesdeluxe. The one small issue I have with it is its go dependancy. You cannot use it with standalone hugo binary.