#!/bin/bash # noter 1.3.0 - "nice configs" - @k@layer8.space - mit # a pretty nifty little logging utility! nlog() { local ORANGE='\033[0;33m' local NO_COLOR='\033[0m' local calling_function=${FUNCNAME[1]} echo -e "${ORANGE}[noter] ${calling_function} | ${1} ${NO_COLOR}" } nlog "sourcing config.sh" noter_dir=$(dirname "$0") source $noter_dir/config.sh # Override publish dir if passed as cli arg [[ ! -z "$1" ]] && nlog "overriding publish_dir to $1" && publish_dir="$1" if [ ! -d "notes" ]; then nlog "Error: 'notes' folder not found!" exit 1 fi checksetting() { # Use this function to check for a setting without repetition # Eg: # checksetting "yourmom" "$isyourmom" if [ "$2" = true ]; then nlog "$1 set to $2" echo "$1" >>"$output_file" fi } givefavicon() { local file_path="$1" if [ -f "$file_path" ]; then local base64_data="$(base64 -w 0 "$file_path")" echo "data:image/png;base64,$base64_data" fi } generate_note_html() { # NOTE TO SELF: Figure out parsing html blocks (ie: ) so that we can apply styles for whole # elements instead of doing what we're doing right now... local note_date="$(date -d "$(basename "$1" .txt)" +"%B %d, %Y")" for img in $(grep -oP '(?<=" echo "
" if [ "$2" = true ]; then echo "

$(date -d "$(basename "$1" .txt)" +"%Y")

" fi echo "

$note_date

" echo "$(cat "$1")
" echo "
" } generate_top_year_bar() { local years=$(find notes -name "*.txt" ! -empty | cut -d'/' -f2 | cut -d'-' -f1 | sort -u | tac) local top_bar="
" local first_year=true for year in $years; do if [ "$first_year" = false ]; then top_bar+=" | " else first_year=false fi top_bar+="$year" done top_bar+="

" echo "$top_bar" } # this took way too long but fuck it # im faster than Google at getting a # feed going goddamit! generate_rss_feed() { nlog "generating rss feed" local rss_file="$publish_dir/feed.xml" local rss_pubdate=$(date -u +"%a, %d %b %Y %H:%M:%S GMT") xml_escape() { local content="$1" sed 's/&/\&/g; s//\>/g; s/"/\"/g; s/'"'"'/\'/g' <<< "$content" } # im sorry for these crimes against humanity echo " $(xml_escape "$site_title") $site_link $(xml_escape "$site_description") $rss_pubdate $rss_pubdate https://cyber.harvard.edu/rss/rss.html noter" >"$rss_file" # this works, don't touch. for file in $(find notes -name '*.txt' -type f -print0 | sort -zr | xargs -0); do if [ -f "$file" ] && [ -s "$file" ]; then nlog "$file" local note_date=$(date -d "$(basename "$file" .txt)" +"%a, %d %b %Y %H:%M:%S GMT") local note_link="$rss_link#$(basename "$file" .txt)" local note_title=$(basename "$file" .txt) local note_description=$(head -n 1 "$file") # Currently we use the first line of the note as the description # This could be improved uppon by using a selector or something to grab # the title for example from a h1 or something as (at least I) tend to # use those when i type posts. echo " $(xml_escape "$note_title") $note_link $(xml_escape "$note_description") $note_date " >>"$rss_file" fi done echo " " >>"$rss_file" nlog "rss feed generated, please remember to move it too with the site: $rss_file" } # why is this stray here, im too afraid to move it # godspeed notecount... notecount=$(find notes -name "*.txt" ! -empty | wc -l) if [ ! -d "${publish_dir}" ]; then nlog "Creating ${publish_dir}" mkdir -p "${publish_dir}" fi # Create HTML file output_file="${publish_dir}/index.html" echo " $notecount notes | $site_title

$site_title


fedi | rss | criminally cute
" >"$output_file" generate_top_year_bar >>"$output_file" # loop for every note in notes nlog "generating page..." for file in $(ls -r notes/*.txt); do nlog "processing: $file" if [ -f "$file" ] && [ -s "$file" ]; then generate_note_html "$file" >>"$output_file" fi done # generate page's rss feed [[ $rssfeed == true ]] && generate_rss_feed # bottom navigation nlog "applying settings" checksetting "
generated with noter
" "$showgenerator" checksetting "" "$backtotop" checksetting "
last Updated: $(date +"%Y-%m-%d %H:%M:%S")
" "$lastupdated" echo "
" >>"$output_file" nlog "Done, please see: $output_file."