Published 2021-04-03.
Time to read: 1 minutes.
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
- Select some text in any document or anywhere that text can be selected.
- 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. - 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.
- Right-click in a folder
- Select New / Shortcut
-
For Type the location of the item, type:
Type the location of the item%windir%\System32\wsl.exe ~/.local/bin/escapeHtml/escapeHtml
- Click Change icon and select a retro icon for this shortcut.
- Click on Apply.
- Click on Next.
-
When prompted for Type a name for this shortcut,
save as
HTML Escape Clip to Clip
. - Click on Finish.
- Right-click on the new shortcut and click in Shortcut key
- I used CTRL+ALT+A.
- 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.