Difference between revisions of "Text Filters"

From BBEditExtras
Jump to: navigation, search
(User created Text Filters)
(User created Text Filters: added new JS/JSON beautifier)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Text Filters accept the selection range (or entire document, if no selection) as input, and return text that replaces the selection range (or document). They may contain Unix scripts, AppleScript files, text factories, and Automator workflows. The selected text (or front document contents, if there is no selection) is passed as `argv[1]` to Unix executables, as a string to text factories, as a reference to a `RunFromBBEdit` entry point in AppleScripts, as text input to Automator workflows, and as a source to text factories.
+
Text filters operate on the selected text of the frontmost document (or on the whole document if there is no selection).
  
Starting with BBEdit 10.1, text input is passed to Unix executables via `stdin`, not `argv[0]`.
+
The selection will be replaced with the filtered text (or the whole document if there is no selection).
  
Text filters live in ~/Library/Application Support/BBEdit/Text Filters. To use a Text Filter, select Apply Text Filter from the Text menu.
+
A text filter may be a Unix script, AppleScript file, BBEdit Text Factory, or Automator workflow.
 +
 
 +
Text filter folder path:
 +
 
 +
    ~/Library/Application Support/BBEdit/Text Filters
 +
 
 +
To use a Text Filter, select Menu_Bar > Text > Apply Text Filter > your_filter_name
 +
 
 +
To easily edit a text filter hold down the Option-key when selecting one from the “Apply Text Filter” menu.
 +
 
 +
Holding down the Shift key when selecting a text filter will reveal it in the Finder.
 +
 
 +
'''Notes:'''
 +
 
 +
The selected text (or front document contents, if there is no selection) is passed as `argv[1]` to Unix executables, as a reference to a `RunFromBBEdit` entry point in AppleScripts, as text input to Automator workflows, and as a source to text factories.
 +
 
 +
Starting with BBEdit 10.1, text input is passed to Unix executables via `stdin`, not `argv[1]`.
 +
 
 +
'''Examples of Unix scripts:'''
 +
 
 +
Transform tab-delimited text into columns:
 +
 
 +
<pre>
 +
#!/usr/bin/env bash
 +
column -t -s ' ';
 +
</pre>
 +
 
 +
Change “http” to "https" in URLs:
 +
 
 +
<pre>
 +
#!/usr/bin/env bash
 +
sed -E 's!http://!https://!';
 +
</pre>
 +
 
 +
Find Every Email Address - Sort - Remove Duplicates:
 +
 
 +
<pre>
 +
#! /usr/bin/env perl -0777 -nsw
 +
my @array = m!\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b!ig;
 +
my %hash = map { $_ => 1 } @array;
 +
my @unique = sort(keys %hash);
 +
$, = "\n";
 +
print @unique;
 +
</pre>
 +
 
 +
A python script for [https://en.wikipedia.org/wiki/ROT13 ROT-13]:
 +
 
 +
<pre>
 +
#!/usr/bin/env python
 +
 
 +
import fileinput
 +
import string
 +
 
 +
unshifted = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 +
shifted =  'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
 +
rot13_table = string.maketrans(unshifted, shifted)
 +
 
 +
for my_line in fileinput.input():
 +
    print string.translate(my_line, rot13_table),
 +
</pre>
  
 
==User created Text Filters==
 
==User created Text Filters==
Line 9: Line 68:
 
* [http://www.dirtdon.com/?p=1149/ Prefixr CSS text filter]
 
* [http://www.dirtdon.com/?p=1149/ Prefixr CSS text filter]
 
* [http://objectivesea.tumblr.com/post/9700437213/jsbeautifierforbbedit JavaScript Beautifier]
 
* [http://objectivesea.tumblr.com/post/9700437213/jsbeautifierforbbedit JavaScript Beautifier]
 +
* [https://github.com/bobtiki/defaults/blob/master/BBEdit/JS%20Beautify.py JavaScript/JSON Beautifier (Python-based)]
 
* [http://crisp.tumblr.com/post/2574967567/json-pretty-print-formatting-in-bbedit JSON Pretty-Print formatter]
 
* [http://crisp.tumblr.com/post/2574967567/json-pretty-print-formatting-in-bbedit JSON Pretty-Print formatter]
 
* [http://bbedit-hints.tumblr.com/post/12327257363/more-useful-clipping-placeholders Remove BBEdit placeholder brackets from clippings]
 
* [http://bbedit-hints.tumblr.com/post/12327257363/more-useful-clipping-placeholders Remove BBEdit placeholder brackets from clippings]
 
* [http://www.mtaa.net/mtaaRR/news/twhid/geek/use_node_js_with_bbedit_text_filters_feature.html Use Node.js in text filters (examples to en/decodeURIComponent)]
 
* [http://www.mtaa.net/mtaaRR/news/twhid/geek/use_node_js_with_bbedit_text_filters_feature.html Use Node.js in text filters (examples to en/decodeURIComponent)]
 
* [https://github.com/sbecker/asset_packager/blob/master/lib/jsmin.rb JavaScript Minifier]
 
* [https://github.com/sbecker/asset_packager/blob/master/lib/jsmin.rb JavaScript Minifier]
 +
* [https://gist.github.com/levigroker/36525010ba0bce15450c89fe6a5f36b1 URL Encode]
 +
* [https://gist.github.com/levigroker/892fd435d701b4e8f56bfcec819d5ef2 URL Decode]
 +
* [https://gist.github.com/levigroker/193ba909e2354de870513eec46c08222 ASCII to Hex]

Latest revision as of 12:58, 7 February 2019

Text filters operate on the selected text of the frontmost document (or on the whole document if there is no selection).

The selection will be replaced with the filtered text (or the whole document if there is no selection).

A text filter may be a Unix script, AppleScript file, BBEdit Text Factory, or Automator workflow.

Text filter folder path:

    ~/Library/Application Support/BBEdit/Text Filters

To use a Text Filter, select Menu_Bar > Text > Apply Text Filter > your_filter_name

To easily edit a text filter hold down the Option-key when selecting one from the “Apply Text Filter” menu.

Holding down the Shift key when selecting a text filter will reveal it in the Finder.

Notes:

The selected text (or front document contents, if there is no selection) is passed as `argv[1]` to Unix executables, as a reference to a `RunFromBBEdit` entry point in AppleScripts, as text input to Automator workflows, and as a source to text factories.

Starting with BBEdit 10.1, text input is passed to Unix executables via `stdin`, not `argv[1]`.

Examples of Unix scripts:

Transform tab-delimited text into columns:

#!/usr/bin/env bash
column -t -s '	';

Change “http” to "https" in URLs:

#!/usr/bin/env bash
sed -E 's!http://!https://!';

Find Every Email Address - Sort - Remove Duplicates:

#! /usr/bin/env perl -0777 -nsw
my @array = m!\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b!ig;
my %hash = map { $_ => 1 } @array;
my @unique = sort(keys %hash);
$, = "\n";
print @unique;

A python script for ROT-13:

#!/usr/bin/env python

import fileinput
import string

unshifted = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
shifted =   'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
rot13_table = string.maketrans(unshifted, shifted)

for my_line in fileinput.input():
    print string.translate(my_line, rot13_table),

User created Text Filters