URI: 
       Title exporter now merges new titles into existing assoc file - bookdarts - Extract KOReader generated highlights and publish them in a variety of formats↵
   DIR Log
   DIR Files
   DIR Refs
   DIR README
       ---
   DIR commit 029678758bcbb1bf08a05f1a062c2ee4216ce7e7
   DIR parent d764d2841ca02118755f4d8c28fd40838f0c674f
  HTML Author: Scarlett McAllister <no+reply@roygbyte.com>
       Date:   Wed, 18 Oct 2023 23:18:28 -0300
       
       Title exporter now merges new titles into existing assoc file
       
       Diffstat:
         M export-from-device.sh               |      10 +++++++---
         A title-exporter.sh                   |      46 +++++++++++++++++++++++++++++++
         A title-replacer.sh                   |      32 +++++++++++++++++++++++++++++++
       
       3 files changed, 85 insertions(+), 3 deletions(-)
       ---
   DIR diff --git a/export-from-device.sh b/export-from-device.sh
       @@ -1,9 +1,13 @@
        #!/bin/sh
       -
       +# Name: export-from-device.sh
       +# Description: Export a highlights file from a KOReader device.
       +# Author: ROYGBYTE
       +# Date: Oct 2023
       +#
        # Dependencies: rsync
        
       -BOOKDARTS_DIR=$PWD
        DEVICE_DIR=/media/kobo
       +BOOKDARTS_DIR=$PWD
        HIGHLIGHTS_DIR=.adds/koreader/clipboard
        HIGHLIGHTS_FILENAME_REGEX=".*-all-books.json"
        HIGHLIGHTS_FILE=$(ls -a "$DEVICE_DIR/$HIGHLIGHTS_DIR/" \
       @@ -12,7 +16,7 @@ HIGHLIGHTS_FILE=$(ls -a "$DEVICE_DIR/$HIGHLIGHTS_DIR/" \
                              | head -n 1)
        
        if [ -z "$HIGHLIGHTS_FILE" ]; then
       -    printf "Could not find highlights file. Exiting..."
       +    printf "Could not find highlights file. Exiting...\n
            exit 1
        fi
        
   DIR diff --git a/title-exporter.sh b/title-exporter.sh
       @@ -0,0 +1,46 @@
       +#!/bin/sh
       +# Name: title-exporter.sh
       +# Desc: Exports titles in KOReader highlights file to a text file.
       +# Author: ROYGBYTE
       +# Date: Oct 2023
       +#
       +# Arguments:
       +#    $1: File containing highlights.
       +#
       +# Example:
       +#    ./title-exporter.sh highlights-from-device.json
       +
       +output_file="titles.txt"
       +tmp_titles=".titles.txt.temp"
       +
       +if [ ! -e "$1" ]; then
       +    printf "File doesn't exist. Exiting..."
       +fi
       +
       +titles=$(cat "$1" | jq -r .documents.[].title)
       +# If output file doesn't exist then create it
       +# and exit from remaining script.
       +if [ ! -r "$output_file" ]; then
       +    printf "Creating ${output_file} with titles...\n"
       +    printf "${titles}" > "${output_file}"
       +    exit
       +fi
       +
       +# Output the titles to a temp file so they can
       +# be read into the while loop later on.
       +touch "${tmp_titles}"
       +printf "${titles}" > "${tmp_titles}"
       +
       +# Output file does exist, so merge the titles
       +# from the given highlights file into the existing
       +# output file.
       +while IFS= read -r line; do
       +    exists=$(grep -F -c "${line}" "${output_file}")
       +    if [ $exists -eq 0 ]; then
       +        printf "Adding new title...\n"
       +        printf "${line}\n" >> "${output_file}"
       +    fi
       +done < "${tmp_titles}"
       +
       +# Clean up
       +rm "${tmp_titles}"
   DIR diff --git a/title-replacer.sh b/title-replacer.sh
       @@ -0,0 +1,32 @@
       +#!/bin/bash
       +# Name: title-replacer.sh
       +# Desc: Search and replace in files.
       +# Author: ROYGBYTE
       +# Date: Oct 2023
       +#
       +# Dependencies:
       +#    bash, for printf %q directive
       +#    jq, for parsing json file
       +#
       +# Argument:
       +#    $1: File to use
       +#    $2: File to search and replace
       +#
       +# Example:
       +#    ./title-replace.sh titles.txt highlights-from-device.json
       +
       +while read line; do
       +    original_title=$(printf "$line" | cut -f1)
       +    replaced_title=$(printf "$line" | cut -f2)
       +    shortened=$(printf "$original_title" | head -c 60)
       +    printf "Original: $original_title\n"
       +    printf "Replaced: $replaced_title\n"
       +    if [ "$replaced_title" = "$original_title" ]; then        
       +        printf "Skipping: $shortened...\n"
       +        continue
       +    fi
       +    printf "Replacing: $shortened ...\n"
       +    escaped_orig=$(printf "%q" "$original_title")
       +    escaped_repl=$(printf "%q" "$replaced_title")
       +    sed -i "s/$escaped_orig/$escaped_repl/g" "$2"
       +done <$1