Fix broken exporter - bookdarts - Extract KOReader generated highlights and publish them in a variety of formats↵ DIR Log DIR Files DIR Refs --- DIR commit 411e4b7296ef59b11d8cf8cbc4985bc680ef91cb DIR parent 2cb9ba25d0eb8d25575fa0b5c8912acf08f5db4d HTML Author: Scarlett McAllister <no+reply@roygbyte.com> Date: Tue, 19 Sep 2023 22:45:10 -0300 Fix broken exporter Diffstat: M bookdarts.sh | 122 +++++++++++++++++++++++++++++-- M export-from-device.sh | 16 ++++++++++------ 2 files changed, 124 insertions(+), 14 deletions(-) --- DIR diff --git a/bookdarts.sh b/bookdarts.sh @@ -1,11 +1,33 @@ #!/bin/sh +# Configuration +HIGHLIGHTS_FILE="highlights-converted.json" +PUBLISH_DIR="publish" +GPH_MENU_FILE="$PUBLISH_DIR/index.gph" +MARKDOWN_DOCUMENTS_DIR="$PUBLISH_DIR/documents" + +# Functions log() { printf "$1\n" } +standard_input_to_file() { + # Helper function, simply prints standard + # input into a given file. + # $1: Filename for menu + data=$(cat) + printf "$data" >> "$1" + return 1 +} + nth_element() { - cat "$HIGHLIGHTS_FILE" | jq .documents.[$1] + cat "$HIGHLIGHTS_FILE" \ + | tr -d '—' \ + | tr -t '“”' '\"' \ + | tr -t '—' '-' \ + | tr -d '[:cntrl:]' \ + | sed -E "s/[^a-zA-Z0-9?=&\-\[\]\{\}]//g" \ + | jq .documents.[$1] return 1 } @@ -15,11 +37,13 @@ get_document_attribute() { # $1: document as JSON object # $2: name of the attribute. # $3: index of attribute. - case $2 in - title) printf "$1" | jq .title ;; - author) cat "$1" | jq .author ;; - file) cat "$1" | jq .file ;; - entry) cat "$1" | jq .entries[$3] ;; + data=$(cat) + 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] ;; *) ;; esac } @@ -34,12 +58,94 @@ get_entry_attribute() { esac } -HIGHLIGHTS_FILE="highlights.json" +create_entries_file() { + # $1: Filename + data=$(cat) + touch "$1" + TITLE=$(printf "$data" | get_document_attribute title) + AUTHOR=$(printf "$data" | get_document_attribute author) + printf "${TITLE}\n${AUTHOR}\n" >> "$1" + 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) + 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" + return 1 +} + +create_gph_menu_item() { + # $1: type + # $2: desc + # $3: path + printf "[$1|$2|$3|||]\n" + return 1 +} + +# Main DOCUMENTS_COUNT=$(cat "$HIGHLIGHTS_FILE" | jq .documents.[].title | wc -l) log "Found $DOCUMENTS_COUNT documents." +# Refresh the publishing directory. +if [ -d "$PUBLISH_DIR" ]; then + log "Removing previously published files from $PUBLISH_DIR" + rm -r "$PUBLISH_DIR" +fi +mkdir -p "$MARKDOWN_DOCUMENTS_DIR" + +# Publish the new files, starting with gopher menu +standard_input_to_file "$GPH_MENU_FILE" <<EOF +\n + ,,,,.,,.,... + BOOKDARTS |''| |''''''' + | \ / + | \/ And sop + | What do we + | and so dog +:::::::::::::::::::::::::::::::::::::::::::::::::: +\n + +EOF INDEX=0 while [ $(( $INDEX < $DOCUMENTS_COUNT )) = 1 ]; do - get_document_attribute "$(nth_element $INDEX)" title + 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) + 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" \ + | get_document_attribute entries \ + | jq 'sort_by(.page)') + ENTRIES_COUNT=$(printf "$ENTRIES_SORTED" \ + | jq 'length') + # Create the file that'll contain all our highlights for + # this document. + printf "$ITEM" | create_entries_file $DOCUMENT_PUBLISHING_FILE + CURR_ENTRY=0 + # 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 \ + | publish_entry $DOCUMENT_PUBLISHING_FILE + CURR_ENTRY=$(( $CURR_ENTRY + 1 )) + done + # Construct gph menu item for this document's highlights file. + log "Constructing gph menu for document $INDEX..." + create_gph_menu_item "1" "$TITLE" \ + "$DOCUMENT_PUBLISHING_FILE" \ + >> $GPH_MENU_FILE + fi + # Prepare for next document. INDEX=$(( $INDEX + 1 )) done DIR diff --git a/export-from-device.sh b/export-from-device.sh @@ -4,20 +4,24 @@ BOOKDARTS_DIR=$PWD DEVICE_DIR=/media/kobo -HIGHLIGHTS_DIR=/.adds/koreader/clipboard +HIGHLIGHTS_DIR=.adds/koreader/clipboard HIGHLIGHTS_FILENAME_REGEX=".*-all-books.json" -HIGHLIGHTS_FILE=$(ls -a "$DEVICE_DIR/$HIGHLIGHTS_DIR" | sort | grep -E "$HIGHLIGHTS_FILENAME_REGEX" | head -n 1) +HIGHLIGHTS_FILE=$(ls -a "$DEVICE_DIR/$HIGHLIGHTS_DIR" \ + | sort \ + | grep -E "$HIGHLIGHTS_FILENAME_REGEX" \ + | head -n 1) if [ -z "$HIGHLIGHTS_FILE" ]; then printf "Could not find highlights file. Exiting..." exit 1 fi -printf "Found highlights file: %s" "$HIGHLIGHTS_DIR/$HIGHLIGHTS_FILE\n" +HIGHLIGHTS_FILE="$DEVICE_DIR/$HIGHLIGHTS_DIR/$HIGHLIGHTS_FILE" +printf "Found highlights file: %s\n" "$HIGHLIGHTS_FILE" -if [ ! -r "$HIGHLIGHTS_DIR/HIGHLIGHTS_FILE" ]; then - printf "Could not read from file. Exiting..." +if [ ! -r "$HIGHLIGHTS_FILE" ]; then + printf "Could not read from file. Exiting...\n" exit 1 fi -cp "$HIGHLIGHTS_DIR/$HIGHLIGHTS_FILE" "$BOOKDARTS_DIR" +cp "$HIGHLIGHTS_FILE" "$BOOKDARTS_DIR/highlights-from-device.json"