From: Gustav Eek Date: Thu, 4 Apr 2024 07:01:59 +0000 (+0200) Subject: Switch from Unoconv to LibreOffice and complete with alternatives X-Git-Url: https://git.g-eek.se/?a=commitdiff_plain;ds=inline;p=users%2Fgustav%2Fscripts.git Switch from Unoconv to LibreOffice and complete with alternatives Switch from Unoconv backed to directly use LibreOffice. Unoconv starts to be buggy and LibreOffice is more tolerant to whether the server is running, therefore Unoconv is rather in the way. Use *pdftotext* for conversion of formats that will not convert to text using LibreOffice (spreadsheets and presentations). In case chache exists but input filename differ, then rename the cached filename to match the input one. This is usefull for Mutt, which always has a different temporary file. Rename format "pdf" -> "zatura", cfr "emacs" --- diff --git a/unoconv-display b/unoconv-display index 09559da..27ee822 100644 --- a/unoconv-display +++ b/unoconv-display @@ -16,7 +16,6 @@ DEBUG=1 cache="$HOME/.cache/unoconv-display" tmp="$XDG_RUNTIME_DIR/${0##*/}" mkdir -p "$cache" "$tmp" -if [ "$1" == "-e" ]; then fmt=emacs; shift; else fmt=pdf; fi debug () { test -n "$DEBUG" &&\ @@ -37,6 +36,7 @@ error () { debug "Start" +if [ "$1" == "-e" ]; then fmt=emacs; shift; else fmt=zatura; fi if [ -n "$1" ]; then file=$1 else file="$tmp/$$"; cat > "$file"; fi sha=$(sha1sum "$file") @@ -47,7 +47,14 @@ txt="$dir/$base.txt" debug "Continue for '$fmt', '$tmp', '$file', '$pdf', '$txt'." -if [ -d "$dir" ]; then # Start text viewer +if [ -d "$dir" ]; then # rename file to updated filename + test -f "$dir"/*.txt && mv -n "$dir"/*.txt "$txt" + test $? == 2 && rm -fr "$dir" # some legacy caused duplicates -- start over + test -f "$dir"/*.pdf && mv -n "$dir"/*.pdf "$pdf" + debug "Cache existed and now renamed." +fi + +if [ -f "$pdf" -a -f "$txt" ]; then # conversion available, start viewer(s) rm -f "$tmp/$$" debug "Found '$dir'. Start '$fmt' viewer." case $fmt in @@ -56,7 +63,7 @@ if [ -d "$dir" ]; then # Start text viewer echo "$txt" 1>&2 exit 0 ;; - pdf) + zatura) exec zathura --fork "$pdf" 2>> "/tmp/err" & echo "$pdf" 1>&2 exit 0 @@ -72,11 +79,18 @@ fi debug "Convert '$file'." -mkdir "$dir" -unoconv -f pdf -o "$pdf" "$file" || \ - unoconv -l && \ - unoconv -f pdf -o "$pdf" "$file" -unoconv -f txt -o "$txt" "$file" +mkdir -p "$dir" +info "$(libreoffice --convert-to pdf --outdir "$dir" "$file")" +info "$(libreoffice --convert-to txt --outdir "$dir" "$file")" + +if [ -f "$pdf" -a ! -f "$txt" ]; then # failed creating txt with LibreOffice + (cd "$dir"; pdftotext "$pdf") + info "Created '$txt' with pdftotext." +fi + +if [ ! -f "$pdf" -o ! -f "$txt" ]; then # something went wrong + error "Output files are missing" +fi # Recursively open