From 331016c692dda0a8ecd465bc5265e6757cab1cb4 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Thu, 14 Jun 2018 14:20:23 +0200 Subject: [PATCH] exportNotebook: remove bash version of exportNotebook Signed-off-by: Marcello Seri --- tools/exportNotebook | 125 ------------------------------------------- 1 file changed, 125 deletions(-) delete mode 100755 tools/exportNotebook diff --git a/tools/exportNotebook b/tools/exportNotebook deleted file mode 100755 index d6ac523..0000000 --- a/tools/exportNotebook +++ /dev/null @@ -1,125 +0,0 @@ -#!/usr/bin/env bash - -# Needs: -# - ssh and scp (openssh) -# - convert (imagemagick) -# - pdftk (pdftk) -# - rsvg-convert (optional, to avoid rasterizing of lines) -# - gs & pdfinfo (optional, to account for original pdf size) - -if [[ $# -eq 0 ]] ; then - echo "Usage: ./exportNotebook (Partial)NotebookName AdditionalrM2svgArguments" - echo "You can additionally append the -c argument for coloured annotations." - exit 0 -fi - -# Make sure we have rM2svg -command -v rM2svg >/dev/null 2>&1 -if [[ $? -ne 0 ]]; then - if [[ -x rM2svg ]]; then - rM2svg_cmd="$(dirname `readlink -f $0`)/rM2svg" - else - print "Cannot find rM2svg" - exit 1 - fi -else - rM2svg_cmd="rM2svg" -fi - -# Check if ssh configuration for "remarkable" exists -grep -Fxq "host remarkable" ~/.ssh/config -if [ $? -eq 0 ]; then - SSH_IP="root@remarkable" -else - SSH_IP="root@10.11.99.1" -fi - -# Start SSH in ControlMaster mode -control_path_dir=$(mktemp -d --suffix=ssh_remark) -control_options="-o ControlPath=$control_path_dir/%h_%p_%r -o ControlPersist=10s" -ssh -M ${control_options} ${SSH_IP} exit - -# Getting the notebook prefix (Newest notebook matching the name) -id=$(ssh ${control_options} ${SSH_IP} "ls -rt .local/share/remarkable/xochitl/*.metadata | xargs fgrep -l $1" | tail -n1 | cut -d. -f1,2) - -test -z "$id" && exit 1 - -tmpfolder=$(mktemp -d) - -# Getting notebook data -scp $control_options -q ${SSH_IP}:"${id}".{lines,pagedata,metadata} "${tmpfolder}"/ - -# Copy underyling document pdf if it exists -ssh $control_options -q ${SSH_IP} "[[ -f "${id}.pdf" ]]" && scp $control_options -q ${SSH_IP}:"${id}.pdf" "${tmpfolder}" - -# Fix for single page notebooks with no template (empty pagedata file by default) -if [ ! -s "${tmpfolder}"/*.pagedata ] -then - echo "Blank" > "${tmpfolder}"/*.pagedata -fi - -# Fix empty lines in pagedata files -sed -i -e "s/^[[:blank:]]*$/Blank/" "${tmpfolder}"/*.pagedata - -filename=$(grep -F '"visibleName"' "${tmpfolder}"/*.metadata | cut -d: -f2- | grep -o '"[^"]*"') -echo "Exporting notebook ${filename} ($(wc -l "${tmpfolder}"/*.pagedata | cut -d\ -f1) pages)" - -if [ -f "${tmpfolder}"/*.pdf ] -then - ln -s "${tmpfolder}/"*.pdf "${tmpfolder}/background_original.pdf" - echo "Found underlying document PDF, using as background." - - if command -v "gs" > /dev/null && command -v "pdfinfo" > /dev/null - then - # Read PDF dimensions for scale correction - size=$(pdfinfo ${tmpfolder}/background_original.pdf | grep "Page size" | awk '{print $3,$5}') - width=$(echo ${size} | cut -f1 -d " ") - height=$(echo ${size} | cut -f2 -d " ") - - # Calculate new width and necessary offset (rM dimensions: 1404x1872) - new_width=$(echo "scale=5; ${height} / 1872 * 1404" | bc) - offset=$(echo "scale=5; ${new_width} - ${width}" | bc) - - echo "Original PDF dimensions are (${width}x${height}), correcting by offset of ${offset} to fit rM foreground." - - # Add offset to background.pdf to match foreground dimensions - gs -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile=${tmpfolder}/background_with_offset.pdf \ - -dDEVICEWIDTHPOINTS=${new_width} -dDEVICEHEIGHTPOINTS=${height} -dFIXEDMEDIA \ - -c "{${offset} 0 translate}" \ - -f "${tmpfolder}/background_original.pdf" - - ln -s ${tmpfolder}/background_with_offset.pdf ${tmpfolder}/background.pdf - else - ln -s ${tmpfolder}/background_original.pdf ${tmpfolder}/background.pdf - fi -else - # Getting template files - sort -u "${tmpfolder}"/*.pagedata | while read -r tpl; do - scp $control_options -q ${SSH_IP}:"'/usr/share/remarkable/templates/${tpl}.png'" "${tmpfolder}"/ - done - - # Generate a PDF file out of the templates - sed -e "s|^|\"${tmpfolder}\"/\"|" -e 's|$|.png"|' "${tmpfolder}"/*.pagedata | tr '\n' ' ' | sed -e "s|$|-transparent white \"${tmpfolder}\"/background.pdf|" | xargs convert -fi - -# Extract annotations and create a PDF -$rM2svg_cmd --input "${tmpfolder}"/*.lines --output "${tmpfolder}/foreground" $2 - -if command -v "rsvg-convert" > /dev/null -then - rsvg-convert -a -f pdf "${tmpfolder}"/foreground*.svg -o "${tmpfolder}"/foreground.pdf -else - convert -density 100 "${tmpfolder}"/foreground*.svg -transparent white "${tmpfolder}"/foreground.pdf -fi - -# Strip .pdf suffix if it already exists (document vs. notebook) -filename=$(basename -s .pdf ${filename//\"/}) - -# Use multistamp instead of multibackground to preserve transparency -pdftk "${tmpfolder}"/background.pdf multistamp "${tmpfolder}"/foreground.pdf output "${filename}.pdf" - -filesize=$(ls -la "${filename}.pdf" | awk '{print $5}' | numfmt --to=iec-i --suffix=B --format="%.3f") -echo "Written ${filesize} to ${filename}.pdf" - -ssh $control_options -O exit ${SSH_IP} -rm -Rf "${tmpfolder}" "${control_path_dir}"