#!/bin/sh
#
# Simple bookmark script for Chawan. By default, M-b shows bookmarks,
# and M-a opens the "add bookmark" screen.
#
# Also works in w3m; just add to keymap:
#
# bind M-a GOTO /cgi-bin/chabookmark
#
# The format is a subset of Markdown, so that it's easy to render.
# You can also edit it manually and it will still work, as long as you
# don't do anything weird.
#
# Sample:
#
#
# Bookmarks
#
#
#
# # Bookmarks
#
# ## Section 1
#
# * [Entry 1](https://a.example)
# * [Entry 2](https://b.example)
#
# ## Section 2
#
# * [Entry 3](https://c.example)
#
# (end of sample)
#
# TODO:
# * sub-entries (or really, sub-sections)
# * maybe tags? (but I don't know what they are good for...)
# * think of a better structured bookmark format?
# * rewrite in Nim?
bookmark_file=${CHA_BOOKMARK:-"${CHA_DIR:-"$HOME/.chawan"}/bookmark.md"}
# utils
die() {
echo "Cha-Control: ConnectionError $*"
exit 1
}
urldec() {
printf '%s\n' "$1" | sed 's/+/ /g' | "${CHA_LIBEXEC_DIR:-/usr/local/libexec/chawan}"/urldec
}
urlenc() {
printf '%s\n' "$1" | "$CHA_LIBEXEC_DIR"/urlenc
}
post_only() {
test "$REQUEST_METHOD" = POST || die InvalidMethod
}
safe_printf() {
printf "$@" || die "InternalError error writing file"
}
safe_println() {
safe_printf '%s\n' "$1"
}
safe_rm() {
rm -f "$1" || die "InternalError error removing temporary file"
}
list_sections() {
# we don't care if there is a file or not
sed -n '/^## /s/## //p' "$bookmark_file" 2>/dev/null
}
# endpoints
ensure_bookmark() {
if test -f "$bookmark_file"; then return 0; fi
import_text=
if test -f "${W3M_DIR:-"$HOME"/.w3m}"/bookmark.html
then import_text="
"
fi
exec cat <No bookmark file
No bookmark file
No bookmark file found. Create a new one at $bookmark_file?
$import_text
EOF
}
new_ed() {
ensure_bookmark
new_ed_section=$(list_sections | sed 's/.*/