Usage: ${0##*/} [-h] [-i PAGE_ID] [-m]
-i PAGE_ID Page ID of page to operate on
+ -l List used page ID's
-m Print page as markdown
-o Out file (optional)
-c HTML out (optional)
+ -f Foce used page ID's rewrite
-h Help -- show this page and exit
-v Vebose output
example_page_id="46306514"
example_page_id="46301431"
+config_dir="$HOME/.${0##*/}"
+sites_csv="$config_dir/sites.csv"
function echoerr { echo "$@" 1>&2; }
curl_out_file="/tmp/${0##*/}-$$-curlout"
html_out_file="/tmp/${0##*/}-$$-htmlout"
html_save=
-while getopts hvmi:o:c: flag; do
+force_rewrite=
+while getopts hvlfmi:o:c: flag; do
case "$flag" in
h)
echoerr "$HELP"
i)
page_id="$OPTARG" ;;
o)
- out_file="$OPTARG" ;;
+ output_file="$OPTARG" ;;
+ f)
+ force_rewrite="t" ;;
c)
html_save="t"
html_out_file="$OPTARG" ;;
+ l)
+ action="l" ;;
m)
action="m" ;;
v)
done
shift $(( OPTIND - 1 ))
-if [[ -z "$action" || -z "$page_id" ]]; then
- echoerr "${0##*/}: ERROR: no action, page ID, and/or ouput page provided. Exits."
+if [[ -z "$action" ]]; then
+ echoerr "${0##*/}: ERROR: No action provided. Provide action Exits."
exit 1
fi
+# Checks configuration CSV with stored sites
+if [ ! -f "$sites_csv" ]; then
+ mkdir -p "$config_dir"
+ echo > "$sites_csv"
+fi
+
+case "$action" in
+ l)
+ message="Reads '$sites_csv'."
+ if [ "$v" ]; then echo "${0##*/}: $message" 1>&2; fi
+ echo "Some page ID's:"
+ sed 's/,/\t/; s/,/\t/' < "$sites_csv"
+ exit 0 ;;
+ m)
+ if [[ -z "$page_id" ]]; then
+ message="No page ID provided for action 'm'. Provide page ID. Exits."
+ echo "${0##*/}: ERROR: $message" 1>&2
+ exit 1
+ fi ;;
+ *)
+ message="Non action. Can not happen."
+ echo "${0##*/}: ERROR: $message" 1>&2
+ exit 1 ;;
+esac
+
message="Continues with page id '$page_id' and action '$action'."
if [ "$v" ]; then echoerr "${0##*/}: $message"; fi
-
# Try to get page
curl https://wiki.etraveli.net/plugins/viewstorage/viewpagestorage.action?pageId="$page_id"\
--verbose\
exit 4 ;;
esac
+# Visit sites file
+if [ "$v" ]; then echo "${0##*/}: Checks '$sites_csv'." 1>&2; fi
+if [ "$force_rewrite" ] || ! grep -q "${page_id}" "$sites_csv"; then
+ title_row=$(head -n 2 < "$html_out_file" | tail -n 1 | pandoc -f html -t markdown | head -n 1)
+ sed "/$page_id/ d" < "$sites_csv" > "${sites_csv}_"; mv -f "${sites_csv}_" "$sites_csv"
+ echo "$page_id,$output_file,$title_row" >> "$sites_csv"
+ if [ "$v" ]; then echo "${0##*/}: File '$sites_csv' modified." 1>&2; fi
+fi
+
# Clean up
if [ "$v" ]; then echo "${0##*/}: Cleans up and exits." 1>&2; fi
rm -f "$curl_out_file"