Avoid using printf, which messes with jq's parsing. - bookdarts - Extract KOReader generated highlights and publish them in a variety of formats↵ DIR Log DIR Files DIR Refs --- DIR commit 75968d58e15a9c4c2fb15036ac2d33547641964f DIR parent 411e4b7296ef59b11d8cf8cbc4985bc680ef91cb HTML Author: Scarlett McAllister <no+reply@roygbyte.com> Date: Wed, 20 Sep 2023 00:17:51 -0300 Avoid using printf, which messes with jq's parsing. Use temp files and cat instead. Diffstat: M bookdarts.sh | 54 ++++++++++++++++---------------- 1 file changed, 27 insertions(+), 27 deletions(-) --- DIR diff --git a/bookdarts.sh b/bookdarts.sh @@ -34,16 +34,14 @@ nth_element() { get_document_attribute() { # Given a document element, function evaluates # to the given attribute. - # $1: document as JSON object - # $2: name of the attribute. - # $3: index of attribute. - data=$(cat) + # $1: name of the attribute. + # $2: index of attribute (optional) case $1 in - title) printf "$data" | jq .title ;; - author) printf "$data" | jq .author ;; - file) printf "$data" | jq .file ;; - entries) printf "$data" | jq .entries ;; - entry) printf "$data" | jq .entries[$3] ;; + title) jq .title ;; + author) jq .author ;; + file) jq .file ;; + entries) jq .entries ;; + entry) jq .[$2] ;; *) ;; esac } @@ -60,22 +58,23 @@ get_entry_attribute() { create_entries_file() { # $1: Filename - data=$(cat) touch "$1" - TITLE=$(printf "$data" | get_document_attribute title) - AUTHOR=$(printf "$data" | get_document_attribute author) + cat > "document.temp" + TITLE=$(cat "document.temp" | get_document_attribute title) + AUTHOR=$(cat "document.temp" | get_document_attribute author) printf "${TITLE}\n${AUTHOR}\n" >> "$1" + rm "document.temp" return 1 } publish_entry() { # $1: Filename to add entry into - data=$(cat) - log "Publishing to: $1" - TEXT=$(printf "$data" | get_entry_attribute text | fold -w 20) - CHAPTER=$(printf "$data" | get_entry_attribute chapter) + cat > "entry.temp" + TEXT=$(cat "entry.temp" | get_entry_attribute text | fold -w 70) + CHAPTER=$(cat "entry.temp" | get_entry_attribute chapter) LINE=$(awk -v len=30 -v line=- "BEGIN{i=0;while(i<len){printf line;i++}}") printf "\n$LINE\n$TEXT\n$CHAPTER\n" >> "$1" + rm "entry.temp" return 1 } @@ -114,29 +113,29 @@ EOF INDEX=0 while [ $(( $INDEX < $DOCUMENTS_COUNT )) = 1 ]; do printf "Publishing document $INDEX \n" - ITEM=$(nth_element $INDEX) - echo "$ITEM" > $INDEX.temp - TITLE=$(printf "$ITEM" | get_document_attribute title) - AUTHOR=$(printf "$ITEM" | get_document_attribute author) + TITLE=$(nth_element "$INDEX" | get_document_attribute title) + AUTHOR=$(nth_element "$INDEX" | get_document_attribute author) DOCUMENT_FILENAME=$(printf "$TITLE - $AUTHOR" \ | sed -E 's/[^a-zA-Z0-9?=&\-\ ]//g' \ | sed -E 's/ /_/g' \ | tr '[:upper:]' '[:lower:]') DOCUMENT_PUBLISHING_FILE="$MARKDOWN_DOCUMENTS_DIR/$DOCUMENT_FILENAME.txt" - ENTRIES_SORTED=$(printf "$ITEM" \ + nth_element "$INDEX" \ | get_document_attribute entries \ - | jq 'sort_by(.page)') - ENTRIES_COUNT=$(printf "$ENTRIES_SORTED" \ - | jq 'length') + | jq 'sort_by(.page)' > "$INDEX-entries.temp" + ENTRIES_COUNT=$(cat "$INDEX-entries.temp" \ + | jq 'length') # Create the file that'll contain all our highlights for # this document. - printf "$ITEM" | create_entries_file $DOCUMENT_PUBLISHING_FILE + nth_element "$INDEX" | create_entries_file $DOCUMENT_PUBLISHING_FILE CURR_ENTRY=0 + log "Found $ENTRIES_COUNT entries." # Populate the file with highlights. if [ $ENTRIES_COUNT > 0 ]; then while [ $(( $CURR_ENTRY < $ENTRIES_COUNT )) = 1 ]; do - log "Publishing entry $CURR_ENTRY..." - printf "$ITEM" | get_document_attribute entry $CURR_ENTRY \ + #log "Publishing entry $CURR_ENTRY..." + # TODO: Progress bar! + cat "$INDEX-entries.temp" | get_document_attribute entry $CURR_ENTRY \ | publish_entry $DOCUMENT_PUBLISHING_FILE CURR_ENTRY=$(( $CURR_ENTRY + 1 )) done @@ -147,5 +146,6 @@ while [ $(( $INDEX < $DOCUMENTS_COUNT )) = 1 ]; do >> $GPH_MENU_FILE fi # Prepare for next document. + rm "$INDEX-entries.temp" INDEX=$(( $INDEX + 1 )) done