Skip to content

Instantly share code, notes, and snippets.

@chrxn
Forked from jdbrice/md-cal.sh
Last active March 6, 2023 07:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrxn/6f133e283f3a7587231728fd240bc289 to your computer and use it in GitHub Desktop.
Save chrxn/6f133e283f3a7587231728fd240bc289 to your computer and use it in GitHub Desktop.
Generate markdown table calendar. Use wiki links to individual days
# modified to use gdate for macos -- obtain 'gdate' by running `brew install coreutils`
startdate=2020-01-01
enddate=2020-12-31
# controls the link format for each day:
# [[ $linkformat | $displayformat ]]
linkformat="%Y-%b-%d-%a"
displayformat="%d"
# marker for empty days in calendar view
emptydays="❌"
monthHeadingLvl="# " # the heading used for the months
monthHeadingFormat="%B, %Y"
# table headers
hWeek="Week"
hMon="Mon"
hTue="Tue"
hWed="Wed"
hThu="Thu"
hFri="Fri"
hSat="Sat"
hSun="Sun"
# initialize the output file
echo "" > calendar.md
curr="$startdate"
while true; do
echo "$curr"
[ "$curr" \< "$enddate" ] || break
# Print the table header on the first day of the month
if [ `gdate +%d --date "$curr"` == "01" ]; then
# pad the end of the last month
if [ `gdate +%a --date "$curr -1 day"` != "Sun" ] && [ `gdate +%b --date "$curr"` != "Jan" ] ; then
# day in the week
ndays=`gdate +%u --date "$curr"`
# loop from last day to the end of the week
for i in $(seq $ndays 7); do
echo -n "|$emptydays" >> calendar.md
done
# close the last column
echo -n "|" >> calendar.md
fi
# start a new month
echo " " >> calendar.md
echo " " >> calendar.md
echo "$monthHeadingLvl" `gdate +"$monthHeadingFormat" --date "$curr"` >> calendar.md
echo "| $hWeek | $hMon | $hTue | $hWed | $hThu| $hFri | $hSat | $hSun |" >> calendar.md
echo "|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|" >> calendar.md
# If the months first week doesnt start on Monday, pad it
# and print the week number
if [ `gdate +%a --date "$curr"` != "Mon" ]; then
# print the week number
echo -n "|" `gdate +%W --date "$curr"` >> calendar.md
# day in the week
ndays=`gdate +%u --date "$curr"`
# minus one for padding
ndays=`echo "$ndays-1" | bc`
# loop from Monday to the first day of the month
for i in $(seq 1 $ndays); do
echo -n "|$emptydays" >> calendar.md
done
fi
fi
# start the week row with the week number
if [ `gdate +%a --date "$curr"` == "Mon" ]; then
echo -n "|" `gdate +%W --date "$curr"` >> calendar.md
fi
# Main link output
echo -n "| [[" `gdate +$linkformat --date "$curr"` "\| " `gdate +$displayformat --date "$curr"` "]]" >> calendar.md
# End the row on Sunday
if [ `gdate +%a --date "$curr"` == "Sun" ]; then
echo "|" >> calendar.md
fi
# increment the date by one day
curr=$( gdate +%Y-%m-%d --date "$curr +1 day" )
done
# print the last day and finish the month off
echo -n "| [[" `gdate +$linkformat --date "$curr"` "\| " `gdate +$displayformat --date "$curr"` "]]" >> calendar.md
# pad the end of the last month
if [ `gdate +%a --date "$curr"` != "Sun" ] ; then
# day in the week
ndays=`gdate +%u --date "$curr"`
# loop from last day to the end of the week
for i in $(seq $ndays 7); do
echo -n "|$emptydays" >> calendar.md
done
# close the last column
echo -n "|" >> calendar.md
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment