💾 Archived View for supernovas.space › gemlog › 2021-06-26-new-search-function.gmi captured on 2024-12-17 at 09:57:26. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-09-08)

-=-=-=-=-=-=-

New Search Function

I mentioned before I wanted to try some more advanced gemini features, and here is my attempt at a search function for my gemlog. Thanks to some help from the people on the #gemini IRC channel correcting my rusty shell programming I got it working. The cgi script will do a grep search of all the gemlog post files for the provided search term and display the links to all the files that matched the search term. If you are just learning how gemini works this is a good place to start.

Here is the final script:

search.gmi

#!/bin/bash

# From gemini://gemini.susa.net/sitemap_script.gmi
function urldecode() {
    # Replace-ALL (//) '+' with <space>
    : "${*//+/ }";
    # Replace-ALL (//) '%' with escape-x and evaluate (-e) on echo
    echo -e "${_//%/\\x}";
}

# Prompt for search term if none provided
if ["$QUERY_STRING" == ""]
then
  echo 


10 Enter a search term:\r\n'
  exit
fi

# Get urldecoded query string and search with grep
query=$(urldecode "${QUERY_STRING}"|tr -cd '[A-Za-z0-9 _*]')
results=$(grep -i -l "$query" 20*.gmi | awk '{ print "=> " $0; }')
if [ "$results" == "" ]
then
  results="No results found"
fi

# Display results
echo 


20 text/gemini\r\n'
echo "## Search results for '$query'"
echo ''
echo "$results"
echo ''
echo '=> /gemlog Back to my Gemlog'

Things I learned:

Thanks to jan6, kevinsan and sysrq in #gemini for the assistance.

Try out the search page here!

Related posts:

Posts with tag 'search'

Posts with tag 'bash'

Posts with tag 'gemini'

tags: search, bash, gemini
timestamp: 2021-06-26 16:57:57