Last active
November 11, 2020 11:43
-
-
Save twolfson/499509c0762087aa11a990facc6f4895 to your computer and use it in GitHub Desktop.
Hyperlink ODS text (LibreOffice Calc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Add hyperlinks to a LibreOffice Calc file | |
# We appreciate https://ask.libreoffice.org/en/question/81492/convert-clickable-hyperlink-to-viewable-url/ | |
# but don't want to handwrite the action every time nor trust a macro (though I guess a bash script isn't much better) | |
# Intended to be used on `.ods` with no hyperlinks at all, would need XML parser otherwise | |
# More recent version might exist in https://github.com/twolfson/dotfiles but no promises | |
# Exit on first error, unset variable, or pipe failure | |
set -euo pipefail | |
# Retrieve our parameters | |
set +u | |
ods_filepath="$1" | |
set -u | |
if test "$ods_filepath" = ""; then | |
echo "Usage: $0 <ods-filepath>" 1>&2 | |
exit 1 | |
fi | |
# Create a temporary folder to work in and set up cleanup hooks | |
tmp_folder="$(mktemp --directory)" | |
cleanup () { | |
rm -r "$tmp_folder" | |
} | |
trap cleanup EXIT | |
# Extract content from our `.ods` as a `.zip` | |
unzip "$ods_filepath" "content.xml" -d "$tmp_folder" 1> /dev/null | |
# <text:p>Non-link text <text:a xlink:href="https://google.com/" xlink:type="simple">https://google.com/</text:a></text:p> | |
# DEV: Regexp to match URLs without leading/trailing quotes or angel brackets (as likely in HTML attribute/content) | |
# DEV: Use list to know when to terminate URL | |
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent | |
# Except removing ",;()" since they could be in sentences but `.` is critical obviously for `.com` | |
sed --in-place -E 's/([^"])(https?:\/\/[-A-Za-z0-9_!~*;.\/?:@&=+$#]+)([^"])/\1<text:a xlink:href="\2" xlink:type="simple">\2<\/text:a>\3/g' "$tmp_folder/content.xml" | |
# Add our `.xml` back into our `.ods` (as a zip) | |
# DEV: `-u` is `update` | |
old_pwd="$PWD" | |
cd "$tmp_folder" | |
zip --verbose "$old_pwd/$ods_filepath" "content.xml" 1> /dev/null | |
cd - 1> /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment