Page 1 of 1

How to Use Text Filters with Python, PHP and awk

Posted: Tue May 28, 2024 2:46 pm
by bbadmin
Text filters can be written in scripting languages such as Python, PHP and awk. Selected text can be filtered through them to perform edits which TextPad cannot do natively. Any script or program which reads from stdin and writes to stdout can be employed. Anything written to stderr will appear in the Tool Output window.

The following screenshots show how to add a text filter written in PHP and then run it on a selected column of numbers to calculate their total.
  1. Add a filter:
    1-AddFilter.png
    1-AddFilter.png (33.14 KiB) Viewed 11083 times
  2. Select a scripting language:
    2-SelectPHP.png
    2-SelectPHP.png (46.05 KiB) Viewed 11083 times
  3. Give it a meaningful name:
    3-RenameFilter.png
    3-RenameFilter.png (24.86 KiB) Viewed 11083 times
  4. Apply it:
    4-ApplyFilter.png
    4-ApplyFilter.png (25.47 KiB) Viewed 11083 times
  5. Specify the script in the Parameters box then click OK:
    5-SpecifyScript.png
    5-SpecifyScript.png (20.7 KiB) Viewed 11083 times
  6. This the sample script in PHP: (it's in TextPad's Samples folder.)
    6-PHPscript.png
    6-PHPscript.png (17.91 KiB) Viewed 11083 times
  7. Select the column in block mode and run the script:
    7-RunFilter.png
    7-RunFilter.png (50.56 KiB) Viewed 11083 times
  8. The result:
    8-FilterResult.png
    8-FilterResult.png (18.16 KiB) Viewed 11083 times
PHP can be installed from https://windows.php.net/download/.

Re: How to Use Text Filters

Posted: Wed May 29, 2024 3:20 pm
by bbadmin
Here is another example using HTML Tidy to validate and pretty print an HTML document.

HTMLtidy.png
HTMLtidy.png (21.12 KiB) Viewed 11077 times

Any errors are written to the Tool Output window.

HTML Tidy can be installed from http://binaries.html-tidy.org/

Re: How to Use Text Filters

Posted: Fri May 31, 2024 5:29 pm
by bbadmin
Here is a Python script to pretty print a JSON document:

Code: Select all

# PrettifyJson.py
# Pretty prints JSON.
# Select all before running it as a filter.

from sys import stdin
import json

obj = json.load(stdin)
prettyobj = json.dumps(obj, indent=4)
print(prettyobj)
This script is installed in TextPad's Samples folder.
Python can be installed from the Windows App Store.

Re: How to Use Text Filters

Posted: Sat Jun 08, 2024 3:40 pm
by bbadmin
Awk is a powerful scripting language which has been around since the early days of Unix. It can do a surprising amount in a single line, as this example to delete consecutive duplicate lines in a document shows. The script in the Parameters field is "!a[$0]++".

Screenshot 2024-06-08 163556.png
Screenshot 2024-06-08 163556.png (20.56 KiB) Viewed 11060 times

Awk can be installed with Google's Chocolatey using "choco install awk".

(Note that duplicate lines can also be deleted by searching for the regular expression "^(.+)$\n+\1" and replacing it with "$1". However, this will require multiple passes if lines are duplicated more than once.)