2023-07-12 22:42:21 +03:00
|
|
|
#!/bin/bash
|
2024-02-17 16:01:25 +02:00
|
|
|
# noter 1.3.0 - "nice configs" - @k@layer8.space - mit
|
2023-07-14 21:56:28 +03:00
|
|
|
|
2024-02-17 16:01:25 +02:00
|
|
|
source ./config.sh
|
2023-12-13 10:54:50 +02:00
|
|
|
|
|
|
|
# a pretty nifty little logging utility!
|
2023-07-12 22:42:21 +03:00
|
|
|
nlog() {
|
2023-08-13 03:28:52 +03:00
|
|
|
local ORANGE='\033[0;33m'
|
|
|
|
local NO_COLOR='\033[0m'
|
2023-12-13 10:54:50 +02:00
|
|
|
local calling_function=${FUNCNAME[1]}
|
|
|
|
|
|
|
|
echo -e "${ORANGE}[noter] ${calling_function} | ${1} ${NO_COLOR}"
|
2023-07-14 21:56:28 +03:00
|
|
|
}
|
|
|
|
|
2023-12-13 10:54:50 +02:00
|
|
|
|
2023-08-13 03:28:52 +03:00
|
|
|
if [ ! -d "notes" ]; then
|
|
|
|
nlog "Error: 'notes' folder not found!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-07-14 21:56:28 +03:00
|
|
|
|
2023-08-13 03:35:03 +03:00
|
|
|
checksetting() {
|
2023-08-13 03:43:31 +03:00
|
|
|
# Use this function to check for a setting without repetition
|
|
|
|
# Eg:
|
|
|
|
# checksetting "yourmom" "$isyourmom"
|
|
|
|
if [ "$2" = true ]; then
|
2023-12-13 10:54:50 +02:00
|
|
|
nlog "$1 set to $2"
|
2023-08-13 03:43:31 +03:00
|
|
|
echo "$1" >>"$output_file"
|
|
|
|
fi
|
2023-08-13 03:36:38 +03:00
|
|
|
}
|
2023-08-13 03:35:03 +03:00
|
|
|
|
|
|
|
givefavicon() {
|
2023-08-13 03:43:31 +03:00
|
|
|
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
|
2023-08-13 03:35:03 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 22:42:21 +03:00
|
|
|
generate_note_html() {
|
2023-09-15 18:23:20 +03:00
|
|
|
# NOTE TO SELF: Figure out parsing html blocks (ie: <code>) so that we can apply styles for whole
|
|
|
|
# elements instead of doing what we're doing right now...
|
2023-08-13 03:28:52 +03:00
|
|
|
local note_date="$(date -d "$(basename "$1" .txt)" +"%B %d, %Y")"
|
|
|
|
for img in $(grep -oP '(?<=<img src=").*?(?=")' "$1"); do
|
2023-09-15 18:23:20 +03:00
|
|
|
# Check if the loading attribute already exists
|
|
|
|
if ! grep -q "src=\"$img\" loading=\"lazy\"" "$1"; then
|
|
|
|
# Add the loading attribute if it doesn't exist
|
|
|
|
sed -i "s|<img src=\"$img\"|<img src=\"$img\" loading=\"lazy\"|g" "$1"
|
|
|
|
fi
|
2023-08-13 03:28:52 +03:00
|
|
|
done
|
2023-11-26 12:28:23 +02:00
|
|
|
|
2023-08-13 03:28:52 +03:00
|
|
|
echo "<a name='$(basename "$1" .txt)'></a>"
|
|
|
|
echo "<div class='note'>"
|
|
|
|
if [ "$2" = true ]; then
|
|
|
|
echo "<h3><a href='#$(date -d "$(basename "$1" .txt)" +"%Y")'>$(date -d "$(basename "$1" .txt)" +"%Y")</a></h3>"
|
|
|
|
fi
|
2023-08-13 03:39:53 +03:00
|
|
|
echo "<h4><a href='#$(basename "$1" .txt)'>$note_date</a></h4>"
|
2023-12-13 10:54:50 +02:00
|
|
|
echo "$(cat "$1")</br>"
|
2023-08-13 03:28:52 +03:00
|
|
|
echo "</div>"
|
2023-07-12 22:42:21 +03:00
|
|
|
}
|
|
|
|
|
2023-08-13 03:28:52 +03:00
|
|
|
generate_top_year_bar() {
|
|
|
|
local years=$(find notes -name "*.txt" ! -empty | cut -d'/' -f2 | cut -d'-' -f1 | sort -u | tac)
|
|
|
|
local top_bar="<center><div class='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+="<a href='#$(find notes -name "$year-*.txt" ! -empty | sort -n | head -n1 | cut -d'/' -f2 | cut -d'.' -f1)'>$year</a>"
|
|
|
|
done
|
|
|
|
top_bar+="</div></center><br>"
|
|
|
|
echo "$top_bar"
|
|
|
|
}
|
2023-07-12 22:42:21 +03:00
|
|
|
|
2023-12-12 16:51:02 +02:00
|
|
|
|
|
|
|
# this took way too long but fuck it
|
|
|
|
# im faster than Google at getting a
|
|
|
|
# feed going goddamit!
|
|
|
|
generate_rss_feed() {
|
2023-12-13 10:54:50 +02:00
|
|
|
nlog "generating rss feed"
|
2023-12-12 16:51:02 +02:00
|
|
|
local rss_file="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; s/'"'"'/\'/g' <<< "$content"
|
|
|
|
}
|
|
|
|
|
|
|
|
# im sorry for these crimes against humanity
|
|
|
|
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
|
|
|
|
<rss version=\"2.0\">
|
|
|
|
<channel>
|
2024-02-17 16:01:25 +02:00
|
|
|
<title>$(xml_escape "$site_title")</title>
|
|
|
|
<link>$site_link</link>
|
|
|
|
<description>$(xml_escape "$site_description")</description>
|
2023-12-12 16:51:02 +02:00
|
|
|
<pubDate>$rss_pubdate</pubDate>
|
|
|
|
<lastBuildDate>$rss_pubdate</lastBuildDate>
|
|
|
|
<docs>https://cyber.harvard.edu/rss/rss.html</docs>
|
|
|
|
<generator>noter</generator>" >"$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
|
2023-12-13 10:54:50 +02:00
|
|
|
nlog "$file"
|
2023-12-12 16:51:02 +02:00
|
|
|
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)
|
2023-12-13 10:54:50 +02:00
|
|
|
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.
|
2023-12-12 16:51:02 +02:00
|
|
|
|
|
|
|
echo " <item>
|
|
|
|
<title>$(xml_escape "$note_title")</title>
|
|
|
|
<link>$note_link</link>
|
|
|
|
<description>$(xml_escape "$note_description")</description>
|
|
|
|
<pubDate>$note_date</pubDate>
|
|
|
|
</item>" >>"$rss_file"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "</channel>
|
|
|
|
</rss>" >>"$rss_file"
|
|
|
|
|
|
|
|
nlog "rss feed generated, please remember to move it too with the site: $rss_file"
|
|
|
|
}
|
|
|
|
|
2023-12-13 10:54:50 +02:00
|
|
|
# why is this stray here, im too afraid to move it
|
|
|
|
# godspeed notecount...
|
2023-07-12 22:42:21 +03:00
|
|
|
notecount=$(find notes -name "*.txt" ! -empty | wc -l)
|
|
|
|
|
|
|
|
# Create HTML file
|
2023-12-12 16:51:02 +02:00
|
|
|
output_file="index.html"
|
2023-07-12 22:42:21 +03:00
|
|
|
echo "<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2023-08-13 03:28:52 +03:00
|
|
|
<meta charset='utf-8'>
|
2023-07-12 22:42:21 +03:00
|
|
|
<title>$notecount notes | noter</title>
|
2024-02-17 16:01:25 +02:00
|
|
|
<meta property='og:title' content='$site_title' />
|
|
|
|
<meta property='og:description' content='$site_description' />
|
2023-12-12 16:51:02 +02:00
|
|
|
<meta property='og:type' content='blog' />
|
2023-07-12 22:42:21 +03:00
|
|
|
<meta property='og:generator' content='noter' />
|
2023-08-13 03:28:52 +03:00
|
|
|
<!-- those who seek, shall see - but thy shall be prepared... -->
|
2023-08-13 03:35:03 +03:00
|
|
|
<link rel='icon' type='image/png' href='$(givefavicon "$favicon")'>
|
2023-12-12 16:51:02 +02:00
|
|
|
<link rel='alternate' type='application/atom+xml' title="rss" href='/feed.xml' />
|
2023-07-12 22:42:21 +03:00
|
|
|
<meta name='last-generated' content='$(date +"%Y-%m-%d %H:%M:%S")' />
|
|
|
|
<style>
|
2024-02-17 16:01:25 +02:00
|
|
|
$site_css
|
2023-07-12 22:42:21 +03:00
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
2023-12-13 10:54:50 +02:00
|
|
|
<div class='container'>
|
2024-02-17 16:01:25 +02:00
|
|
|
<h1>$site_title</h1><br>
|
2023-12-13 10:54:50 +02:00
|
|
|
<center> <a rel='me' href='https://layer8.space/@k'>fedi</a> | <a href="feed.xml">rss</a> | <a href="https://the-sauna.icu/">sauna</a> </center>" >"$output_file"
|
2023-08-13 03:28:52 +03:00
|
|
|
generate_top_year_bar >>"$output_file"
|
2023-12-13 10:54:50 +02:00
|
|
|
|
2023-07-12 22:42:21 +03:00
|
|
|
# loop for every note in notes
|
|
|
|
nlog "generating page..."
|
|
|
|
for file in $(ls -r notes/*.txt); do
|
2023-08-13 03:28:52 +03:00
|
|
|
nlog "processing: $file"
|
|
|
|
if [ -f "$file" ] && [ -s "$file" ]; then
|
|
|
|
generate_note_html "$file" >>"$output_file"
|
|
|
|
fi
|
2023-07-12 22:42:21 +03:00
|
|
|
done
|
|
|
|
|
2023-12-12 16:51:02 +02:00
|
|
|
# generate page's rss feed
|
2024-02-17 17:00:30 +02:00
|
|
|
[[ $rssfeed == true ]] && generate_rss_feed
|
2023-12-12 16:51:02 +02:00
|
|
|
|
2023-08-13 03:28:52 +03:00
|
|
|
# bottom navigation
|
2023-12-13 10:54:50 +02:00
|
|
|
nlog "applying settings"
|
2023-08-13 03:35:03 +03:00
|
|
|
checksetting "<div class='generated-with'>generated with <a href='https://git.sr.ht/~koutsie/noter'>noter</a></div>" "$showgenerator"
|
|
|
|
checksetting "<div class='back-to-top'><a href='#'>Back to Top</a></div>" "$backtotop"
|
|
|
|
checksetting "<div class='last-updated'>last Updated: $(date +"%Y-%m-%d %H:%M:%S")</div>" "$lastupdated"
|
2023-07-12 22:42:21 +03:00
|
|
|
|
2023-11-26 12:28:23 +02:00
|
|
|
|
2023-07-12 22:42:21 +03:00
|
|
|
echo "</div>
|
|
|
|
</body>
|
|
|
|
</html>" >>"$output_file"
|
|
|
|
|
2023-12-12 17:03:46 +02:00
|
|
|
nlog "Done, please see: $output_file."
|