Am 27.10.2011 um 27, 17:04 schrieb Bruce Van Allen:

> On 2011-10-27, Roland Küffner wrote:
>> My idea was to do it with some kind of dictionary file. In it each line 
>> would contain a single search replacement pair separated by tabs. Just like:
>> 
>> old term<tab>new term
>> some other random old text<tab>another replacement

A million thanks to Bruce and James,

Bruce's script works pretty good for normal text. That covers about 90 % of my 
use cases, so I'm already a happy camper. It might get a little tricky when the 
search terms contain characters that disturb the s/...-line in the PERL script. 
I tried one example where I had a URI to be replaced and the script chocked on 
it. But maybe I will be able to puzzle on this myself after getting nudged in 
the right basic direction. I will also ponder a little bit on the PHP version 
as my PHP is slightly better than my PERL and I might be able to tweak this 
more easily.

Meanwhile I tried something totally different and can provide third solution. 
But beware, this is a kind of an ugly hack: For my lack of scripting fu I like 
Text Factories and use them pretty much. Setting them up is a little tedious as 
it requires a lot of clicking an only little typing. So my idea was: Why not 
build a Text Factory that can build Text Factories (I know, it is madness!!) ...
But seriously: If you open a Text Factory in another text editor (TextEdit, if 
must be) you'll see that Factories are really plist and hence XML-files. A 
simple "Replace All" step from the factories will take about following form in 
plain text:

                <dict>
                        <key>ComponentArguments</key>
                        <dict>
                                <key>CaseSensitive</key>
                                <false/>
                                <key>MatchWords</key>
                                <false/>
                                <key>ReplaceString</key>
                                <string>\2</string>
                                <key>SearchString</key>
                                <string>\1</string>
                                <key>UseGrep</key>
                                <false/>
                        </dict>
                        <key>ComponentName</key>
                        <string>ReplaceAll</string>
                </dict>

I generated a new (meta-)Text Factory with a Replace all step. Using grep I 
simply search for
^(.+?)\t(.+)
to match my dictionary items (see OP).
The replace step contains the snippet above (ready with the \1 and \2 
placeholders). This action translates every line in my dictionary text file 
into a Text Factory action.
The last two steps in my meta-factory add the header and the footer of the 
plist-file. Simply add two further "Replace All" steps. The first searches for 
\A (matches the very beginning of a text file), the second for \Z (matches 
EOF). Copy the replacement text from a sample factory file (it's a little too 
long to post here - but you still have it open in TextEdit, haven't you?).
The last thing you should do is to add a Translate Text to HTML step at the 
very beginning of the meta-factory. This will escape potential < > and & 
characters in your dictionary file that would mess up a XML file.

The resulting meta-factory turns a tab delimited dictionary file into a working 
Text Factory by the press of one shortcut. Just save your treated dictionary 
file as a new file and give it the ending .textfactory - when you close and 
reopen it, it will automatically open as a factory ready to be unleashed upon 
your text files.

With this approach you can even set up a dictionary file where your 
search/replace pairs could contain grep patterns (of course you must set the 
UseGrep-entry in the above snippet to true) - Imagine the possibilites ...

Enjoy,
Roland


-- 
You received this message because you are subscribed to the 
"BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
<http://groups.google.com/group/bbedit?hl=en>
If you have a feature request or would like to report a problem, 
please email "[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

Reply via email to