From f6b793ebb2c77305d949b42acc3f9c65bb7335d8 Mon Sep 17 00:00:00 2001 From: Gustav Eek Date: Fri, 21 Aug 2015 09:38:21 +0200 Subject: [PATCH] [confluence-tk] Visited pages support added In, by default, `~/.confluence-tk/sites.csv` page ID's and some details is stored. Functionality is added for to parse and list the content and also for to force rewrite. --- confluence-tk | 52 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/confluence-tk b/confluence-tk index 73c03fa..130e83c 100755 --- a/confluence-tk +++ b/confluence-tk @@ -11,9 +11,11 @@ HELP="${0##*/}: toolkit for confluence wiki page information retrieaval 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 @@ -22,6 +24,8 @@ preferred." example_page_id="46306514" example_page_id="46301431" +config_dir="$HOME/.${0##*/}" +sites_csv="$config_dir/sites.csv" function echoerr { echo "$@" 1>&2; } @@ -32,7 +36,8 @@ v= 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" @@ -40,10 +45,14 @@ while getopts hvmi:o:c: flag; do 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) @@ -55,15 +64,39 @@ while getopts hvmi:o:c: flag; do 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\ @@ -149,6 +182,15 @@ case "$action" in 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" -- 2.39.2