Skip to content

Instantly share code, notes, and snippets.

@shazow
Last active January 28, 2023 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shazow/0a9f4e0afc9f5e7b85401ab0bf8b322e to your computer and use it in GitHub Desktop.
Save shazow/0a9f4e0afc9f5e7b85401ab0bf8b322e to your computer and use it in GitHub Desktop.
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p jq curl
set -e
prefix() {
declare token="${1}"
while read line; do
echo "${token}${line}"
done
}
urlnormalize() {
# Strip scheme and trailing slash
sed -e 's|^\w*://||g' | sed -e 's|/$||g'
}
urlencode() {
curl -s -w "%{url_effective}" --get --data-urlencode "@-" "example.com" | cut -d '?' -f2-
}
render() {
declare line=$1
declare url=$(echo $line | jq -j -r .href);
declare title=$(echo $line | jq -j -r .description);
declare description=$(echo $line | jq -j -r .extended);
declare timestamp=$(echo $line | jq -j -r .time);
declare tags=$(echo $line | jq -j -r .tags | sed 's|\s| #|g');
echo "# $title"
echo ""
echo "Link: $url"
echo "Timestamp: $timestamp"
if [[ "$tags" ]]; then
echo "Tags: #$tags"
else
"Tags: "
fi
echo ""
if [[ "$description" ]]; then
echo "## Description"
echo "$description" | sed 's|<.\?blockquote>||g' | prefix "> "
echo ""
fi
}
jq -c .[] $1 | while read line; do
declare path="$(echo $line | jq -j -r .href | urlnormalize | urlencode).md"
if [[ -f "$path" ]]; then
echo "Skipping: $path"
continue
fi
echo "Converting: $path"
render "$line" > $path
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment