Mike Slinn

Escaping HTML on Clipboard From a Windows Hot Key via WSL

Published 2021-04-03.
Time to read: 1 minutes.

This page is part of the posts collection, categorized under WSL.

I frequently show HTML source code when I write. That HTML must be escaped prior to displaying it on a web page.

Script to Apply HTML Escape to Clipboard

This bash script applies an HTML escape conversion to the contents of the system clipboard. If you have WSL on your machine, you could store it on the WSL file system, for example in ~/.local/bin/escapeHtml.

#!/bin/bash

# Convert from HTML to escaped HTML
# Reads from stdin if invoked in a pipeline, otherwise prompts for input
#
# SPDX-License-Identifier: Apache-2.0

function help {
  echo "$(basename $0) - Escapes HTML with entities.
Reads from STDIN or pipe, or converts the clipboard.
Result is copied to the clipboard.

To remove groff escape characters, use ansi2txt and col -b:
  sudo apt install kbtin
  aws ecr create-repository help | ansi2txt | tr -s ' ' | col -b | $(basename $0)
"
  exit 1
}

function checkDependencies {
  if [ -z "$(which recode)" ]; then yes | sudo apt install recode; fi
}

function filesAreLinked {
  "$1" -ef "$2"
}

function isWindows {
  if grep -q -i Microsoft /proc/version; then echo yes; fi
}


SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
if [ "$1" == -h ]; then help; fi
checkDependencies

if [ "$( isWindows )" ]; then
  TO_CLIPBOARD=clip.exe
else
  TO_CLIPBOARD="xclip -sel clip"
fi

if [ -t 0 ]; then  # Not reading from a terminal
  HTML="$( powershell.exe -command "Get-Clipboard" )"
else  # Reading from stdin or pipe
  # See https://stackoverflow.com/a/32365596/553865
  HTML=$(cat; echo x)
  HTML=${HTML%x}   # Strip the trailing x
fi
# RESULT="$( echo "$HTML" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | recode utf8..html )"
# TODO: sed -e 's/?????/━━━━━/' | # Removes a lot of lines, why?
RESULT="$( echo "$HTML" | sed -e 's/[[:space:]]*$//' | sed -e 's/(\[33m|\[39m|\[1m|\[0m)//' | recode utf8..html )"

# TODO: replace { with { and } with } ONLY IF they are not {% or %}
echo "$RESULT" | sed -e "s/
//g" -e "s/'/\'/g" | "$SCRIPT_DIR/replaceBlankLines" | "$TO_CLIPBOARD"
echo "$( echo "$RESULT" | wc -l ) lines have been placed on the clipboard."

Using the Script

  1. Select some text in any document or anywhere that text can be selected.
  2. Run escapeHtml on the same machine. If you have Windows with WSL you can run the script there, or run it in native Windows, does not matter.
  3. Paste escaped HTML into your target document.

Hot Key Trigger

Trigger the script with a hot key via your OS's facilities. This section just discusses how native Windows hot keys can be used to trigger this script running in WSL.

  1. Right-click in a folder
  2. Select New / Shortcut
  3. For Type the location of the item, type:

    Type the location of the item
    %windir%\System32\wsl.exe ~/.local/bin/escapeHtml/escapeHtml
  4. Click Change icon and select a retro icon for this shortcut.
  5. Click on Apply.
  6. Click on Next.
  7. When prompted for Type a name for this shortcut, save as HTML Escape Clip to Clip.
  8. Click on Finish.
  9. Right-click on the new shortcut and click in Shortcut key
  10. I used CTRL+ALT+A.
  11. Click on OK.
😁

You can now quickly copy HTML from any source to the clipboard, apply an HTML escape conversion to the clipboard contents, and then paste the escaped HTML into an editor.

* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.