Create a cooking website.

Creating a cooking website from publically available markdown recipes.

Creating the site

After seeing the quality of the minimalist recipes on Based Cooking and the power of many people collaborating on recipes (much like FOSS). I decided I could utalise these available markdown files, as the recipes were under Creative Commons Zero v1.0 Universal, and create my own “instance” as such of the site for personal use.


Therefore, I wrote a script to:

  • Pull all the recipes and their pictures from github.
  • Copy the recipes and corresponding pictures into the correct directories.
  • Parse the title and tags of each recipe.
  • Set each tags first letter to upper case.
  • Write the necessary header to the .md’s for my chosen hugo theme.
  • Remove the title and tags from recipes (as these are now handled by the hugo theme).
  • Correct markdown file’s link’s to the correct directories.

Once this was done, all I had to do was generate the static site with hugo -D, as the markdown files were now all correctly formatted with their correct names and tags etc.

I have another script above this to automate placing the new public directory (where the compiled site is stored) into my personal website.


URL for my instance: https://benarmstead.co.uk/cooking

Hugo theme used: m10c


Script

I do not recommend you run this script on your machine. It is written to personally work with my file directory structure.

This may cause loss of files if you run it incorrectly. So I advise you to read it and modify it first for your own use to apply to your system.

The script below is released under the GPLv3 license, you may find a copy below the script here.

posts=content/posts
repo_name=based.cooking
repo_link=https://github.com/LukeSmithxyz/based.cooking
rm -r $posts
cd $repo_name
git pull $repo_link
cp -r src ../$posts
cp -r data/pix ../static/
cd ../$posts

dir="."
write(){
    # Gets the title of the recipe
    title=$(head -n 1 $i)

    # Deletes the title of the recipe
    sed -i '1d' $i

    # Creates the necessary header for the hugo theme
    text="+++\ntitle = \"${title:2}\"\ntags = $tag_string\ndraft = false\n+++\n"

    # Adds the header to the file ($i)
    sed -i "1i $text" $i

    # Deletes the tags from the recipe
    head -n -1 $i > /tmp/tmp.txt && mv /tmp/tmp.txt $i

     # Corrects the picture directories link
    sed -i -e 's/pix/..\/..\/pix/g' $i
}

get_tags(){
    # Gets the tags of the recipe
    tag=$(tail -1 $i)

    # Reads the tags into an array
    readarray -d " " -t tags<<<"$tag"

    # Removes the first and last tag's from the tag array
    unset tags[0]
    unset tags[-1]


    tag_string="["

    # For every tag in tags array
    for individual_tag in "${tags[@]}"; do
        # Makes current tag's first letter upper case
        individual_tag="$(tr '[:lower:]' '[:upper:]' <<< ${individual_tag:0:1})${individual_tag:1}"

        # Append tag to new string format for tags
        tag_string=$tag_string"\""$individual_tag"\", "
    done
    tag_string=$tag_string"]"
}

IFS=!
ARRAY=(`find * -printf %f!`)


# For all files in the directory
for i in ${ARRAY[*]}; do
    get_tags
    write

done

License for above script

                       Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

Full license at gnu.org

Ben Armstead
Ben Armstead
Computer Science Undergraduate (Lancaster University)

I enjoy free software programming and learning about Linux.

Related