Adds “formats” (that is, XBEL-output)
This commit is contained in:
parent
e8fd791bc2
commit
cfc4d05622
|
@ -8,47 +8,86 @@
|
||||||
# Lisc: GPLv3
|
# Lisc: GPLv3
|
||||||
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
||||||
|
|
||||||
SOURCE_DIRS="./sources/ $HOME/.local/libexec/bookmarks-dl/ /usr/local/libexec/bookmarks-dl/ /usr/libexec/bookmarks-dl/"
|
SOURCE_DIRS="./sources/ $HOME/.local/libexec/bookmarks-dl/sources/ /usr/local/libexec/bookmarks-dl/sources/ /usr/libexec/bookmarks-dl/sources/"
|
||||||
|
FORMAT_DIRS="./formats/ $HOME/.local/libexec/bookmarks-dl/formats/ /usr/local/libexec/bookmarks-dl/formats/ /usr/libexec/bookmarks-dl/formats/"
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
1>&2 echo "usage: $(basename "$0") SOURCE ..."
|
1>&2 echo "usage: $(basename "$0") SOURCE ..."
|
||||||
1>&2 echo " $(basename "$0") --list"
|
1>&2 echo " $(basename "$0") --sources"
|
||||||
|
1>&2 echo " $(basename "$0") --formats"
|
||||||
1>&2 echo " $(basename "$0") --help"
|
1>&2 echo " $(basename "$0") --help"
|
||||||
1>&2 echo ""
|
1>&2 echo ""
|
||||||
1>&2 echo " SOURCE is a source of bookmarks."
|
1>&2 echo " SOURCE is a source of bookmarks."
|
||||||
1>&2 echo " You can see a list of sources with '--list'."
|
1>&2 echo " You can see a list of sources with '--list'."
|
||||||
|
1>&2 echo " You can see a list of output formats with '--formats'."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Return the paths to all available bookmarks-dl “source” scripts.
|
# Return the paths to all available bookmarks-dl “source” scripts.
|
||||||
all_sources() {
|
all_scripts() {
|
||||||
find $SOURCE_DIRS -type f -name '*.sh' \
|
local target_dirs="$1"
|
||||||
|
find $target_dirs -type f -name '*.sh' \
|
||||||
2> /dev/null
|
2> /dev/null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Return the path to a specific bookmarks-dl source.
|
# Return the path to a specific bookmarks-dl source.
|
||||||
get_source() {
|
get_script() {
|
||||||
local source_name="$1"
|
local script_dirs="$1"
|
||||||
all_sources \
|
local script_name="$2"
|
||||||
| grep "/$source_name.sh" \
|
all_scripts \
|
||||||
|
| grep "/$script_name.sh" \
|
||||||
| head -1
|
| head -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# List all available bookmarks-dl sources user-friendly-like.
|
# List all available bookmarks-dl sources user-friendly-like.
|
||||||
list_sources() {
|
list_scripts() {
|
||||||
for source in $(all_sources); do
|
local script_dirs="$1"
|
||||||
|
for script in $(all_scripts "$script_dirs"); do
|
||||||
printf '%s\t%s\n' \
|
printf '%s\t%s\n' \
|
||||||
"$(basename "$source" | sed 's/\.sh//')" \
|
"$(basename "$script" | sed 's/\.sh//')" \
|
||||||
"$source"
|
"$script"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Given the arguments to this program, find that matching `-f`, the paramter
|
||||||
|
# for selecting a specific format.
|
||||||
|
get_format() {
|
||||||
|
while test -n "$1" -a ! "$1" = "-f"; do
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
if test "$1" = "-f"; then
|
||||||
|
echo "$2"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# The function called to format the internal JSON-format bookmarks into a
|
||||||
|
# more usable format. This should be overridden by a “format” script; see
|
||||||
|
# last couple of lines of this script.
|
||||||
|
# It receives the bookmarks over stdin, and returns them over stdout.
|
||||||
|
format_bookmarks() {
|
||||||
|
cat
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# The function called to parse arguments of a source and begin downloading
|
||||||
|
# bookmarks-dl.sh
|
||||||
|
# This should be overloaded by a “source” script.
|
||||||
|
source_start() {
|
||||||
|
exit 4
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ————————————————————————————————————————
|
||||||
|
# MISC. UTILS
|
||||||
|
# ————————————————————————————————————————
|
||||||
# Given some HTML, return it’s plain-text and deescaped form.
|
# Given some HTML, return it’s plain-text and deescaped form.
|
||||||
html_text_deescape() {
|
html_text_deescape() {
|
||||||
lynx -dump -stdin
|
lynx -dump -stdin \
|
||||||
|
| xargs echo # Trim trailing/preceding whitespace.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -88,22 +127,32 @@ curl_browseresque() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ————————————————————————————————————————
|
||||||
|
# INVOCATION
|
||||||
|
# ————————————————————————————————————————
|
||||||
SOURCE_NAME="$1"
|
SOURCE_NAME="$1"
|
||||||
case "$SOURCE_NAME" in
|
case "$SOURCE_NAME" in
|
||||||
--list|list)
|
--sources|sources)
|
||||||
list_sources
|
list_scripts "$SOURCE_DIRS"
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
|
--formats|formats)
|
||||||
|
list_scripts "$FORMAT_DIRS"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
|
||||||
--help|-h|help|'')
|
--help|-h|help|'')
|
||||||
usage
|
usage
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
source "$(get_source "$SOURCE_NAME")" \
|
SOURCE="$(get_script "$SOURCE_DIRS" "$SOURCE_NAME")"
|
||||||
2> /dev/null > /dev/null
|
if test -f "$SOURCE"; then
|
||||||
if test "$?" -ne 0; then
|
source "$SOURCE"
|
||||||
|
fi
|
||||||
|
if test "$?" -ne 0 -o ! -f "$SOURCE"; then
|
||||||
1>&2 echo "The source '$SOURCE_NAME' couldn’t be found."
|
1>&2 echo "The source '$SOURCE_NAME' couldn’t be found."
|
||||||
1>&2 echo "Try '$(basename "$0") --list' to see a list of possible sources."
|
1>&2 echo "Try '$(basename "$0") --sources' to see a list of possible sources."
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
@ -117,5 +166,13 @@ else
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Overloaded by the `source`-d bookmarks-dl “source.”
|
FORMAT="$(get_script "$FORMAT_DIRS" "$(get_format $@)")"
|
||||||
source_start $@
|
if test -f "$FORMAT"; then
|
||||||
|
source "$FORMAT" \
|
||||||
|
2> /dev/null > /dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# These both should be overloaded.
|
||||||
|
source_start $@ \
|
||||||
|
| format_bookmarks
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
#!/bin/sh
|
||||||
|
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
||||||
|
# Name: bookmarks-dl: xbel
|
||||||
|
# Desc: Output format for bookmarks-dl, formatting bookmarks in XBEL format.
|
||||||
|
# Auth: Jaidyn Ann <jadedctrl@posteo.at>
|
||||||
|
# Date: 2023-09-04
|
||||||
|
# Reqs: lynx, jq
|
||||||
|
# Lisc: GPLv3
|
||||||
|
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
||||||
|
|
||||||
|
format_bookmarks() {
|
||||||
|
local json_bookmarks_file="$(mktemp)"
|
||||||
|
cat \
|
||||||
|
> "$json_bookmarks_file"
|
||||||
|
|
||||||
|
echo '<?xml version="1.0" encoding="UTF-8"?>'
|
||||||
|
echo '<xbel version="1.0">'
|
||||||
|
|
||||||
|
items_count="$(jq -r '. | length' < "$json_bookmarks_file")"
|
||||||
|
item_index="0"
|
||||||
|
while test "$item_index" -lt "$items_count"; do
|
||||||
|
format_bookmark "$json_bookmarks_file" "$item_index"
|
||||||
|
item_index="$(echo "$item_index + 1" | bc)"
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "</xbel>"
|
||||||
|
rm "$json_bookmarks_file"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
format_bookmark() {
|
||||||
|
local json_file="$1"
|
||||||
|
local json_index="$2"
|
||||||
|
|
||||||
|
title="$(jq -r ".[$json_index].title" < "$json_file")" # | html_text_deescape | html_escape)"
|
||||||
|
desc="$(jq -r ".[$json_index].desc" < "$json_file")" # | html_text_deescape | html_escape)"
|
||||||
|
added="$(jq -r ".[$json_index].added" < "$json_file")"
|
||||||
|
href="$(jq -r ".[$json_index].href" < "$json_file")"
|
||||||
|
if test -z "$title" -a -n "$desc"; then
|
||||||
|
title="$(echo "$desc" | head --bytes=40)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat <<MDR
|
||||||
|
<bookmark href="$href" $(if valid_value "$added"; then echo "added=\"$added\""; fi)>
|
||||||
|
$(if valid_value "$title"; then echo "<title>$title</title>"; fi)
|
||||||
|
$(if valid_value "$desc"; then echo "<desc>$desc</desc>"; fi)
|
||||||
|
</bookmark>
|
||||||
|
MDR
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
valid_value() {
|
||||||
|
local value="$1"
|
||||||
|
test -n "$value" -a ! "$value" = "null"
|
||||||
|
}
|
|
@ -46,7 +46,7 @@ fetch_bookmarks() {
|
||||||
source_start() {
|
source_start() {
|
||||||
local auth=""
|
local auth=""
|
||||||
local domain=""
|
local domain=""
|
||||||
while getopts 'ha:u:d:' arg; do
|
while getopts 'hf:a:u:d:' arg; do
|
||||||
case $arg in
|
case $arg in
|
||||||
h)
|
h)
|
||||||
usage
|
usage
|
||||||
|
|
|
@ -75,7 +75,7 @@ fetch_bookmarks() {
|
||||||
source_start() {
|
source_start() {
|
||||||
local auth=""
|
local auth=""
|
||||||
local user_id=""
|
local user_id=""
|
||||||
while getopts 'ha:u:' arg; do
|
while getopts 'hf:a:u:' arg; do
|
||||||
case $arg in
|
case $arg in
|
||||||
h)
|
h)
|
||||||
usage
|
usage
|
||||||
|
|
Ŝarĝante…
Reference in New Issue