2020-12-16

gem-tags.sh

------------------------------------------------------------
#!/bin/bash
# 2020-12-05 ~ew
# License: CC0-1.0

unset LANG
set -euo pipefail

echo "# TAG Index"

find ./20* -type f -name \*.gmi |
    LANG='' sort -r |
    while read file
    do
        grep '^=> /tag-index.gmi tags:' ${file} | sed -e 's/^.*tags:[ ]*//' || true
    done |
    tr ' ' '\n' |
    grep -v '^


 |
    LANG='' sort -u |
    while read tag
    do
        (
            echo ""
            echo "## $tag"
            # files
            find ./20* -type f -name \*.gmi |
                LANG='' sort -r |
                while read file2
                do
                    X=$(grep "^=> /tag-index.gmi tags:" $file2 | sed -e 's/^.*tags:[ ]*//' | tr ' ' '\n' | grep -c "^${tag}$"  || true)
                    if [ "$X" -gt 0 ]
                    then
                        date=$(  sed -n -e 's/^#[# ]*//' -e '1p' "${file2}" )
                        title=$( sed -n -e 's/^#[# ]*//' -e '2p' "${file2}" )

                        printf "=> /%s   %s %s\n" "${file2}" "${date}" "${title}"
                    fi
                done
        )
    done

------------------------------------------------------------