nisus.com
https://www.nisus.com/forum/

Improve "Cleaning up punctuation"?
https://www.nisus.com/forum/viewtopic.php?f=17&t=2618
Page 1 of 1

Author:  vitaka [ 2007-10-25 12:07:59 ]
Post subject:  Improve "Cleaning up punctuation"?

Hi, I really like the "Cleaning up punctuation.nwm" macro. Could it be possible to add a couple lines to also clean up spacing errors with parentheses? Like having a space before but not after an opening parentheses, and a space after but before a closing parentheses?
In other words, turn something like “I ( really )like the( completely),new version of Nisus Writer Pro!” into:
“I (really) like the (completely), new version of Nisus Writer Pro!”

Author:  martin [ 2007-10-25 12:43:20 ]
Post subject: 

Adding these lines to the macro should do the trick:
Code:
# clean spaces between parenthesis/braces/brackets and adjacent content
Replace All ‘([({\[]) +’, ‘\1’, ‘E’
Replace All ‘(?<=\S)([({\[])’, ‘ \1’, ‘E’
Replace All ‘([)}\]])(?=\S)’, ‘\1 ’, ‘E’
Replace All ‘ +([)}\]])’, ‘\1’, ‘E’

Cheers!

Author:  Dirk_Barends [ 2007-10-26 04:55:40 ]
Post subject:  Cleaning up punctuation

I also use this "Cleaning up punctuation" macro a lot, and have made several adjustments and additions too, but the limiting thing is that I cannot make this macro work properly on selected text only, using 'aEs' as options? Therefore I still use the old perl macro "Common Regexp Processing", but that macro can ruin styling...

Author:  martin [ 2007-10-26 12:46:46 ]
Post subject: 

The problem is that after each Replace command the replaced text is selected. Currently there is no way to preserve the selection when a replacement is made, but we have it filed as a feature we need to add.

The best you can manage right now is to manually save and restore the selection using the Selection Location/Length commands, but this would not account for changes in text length as the replacements proceed. Depending on the replacement, you might be able to calculate the new selection length yourself, eg:

Code:
$selLoc = Selection Location
$selLen = Selection Length
$count = Replace All "red", "double", "as"
While $count > 0
    $selLen += 3
    $count -= 1
End
Set Selection $selLoc, $selLen

Not convenient, but for now I think it's the only possibility.

Author:  Dirk_Barends [ 2007-10-26 23:52:30 ]
Post subject:  workaround

A more convenient 'workaround' for me -- so far -- has been to combine perl in a macro like this:
Code:
Find and Replace '[\t ]+$', '', 'aE'  #Remove trailing white space
Find and Replace '  +', ' ', 'aE' #Replace multiple spaces with one

Begin Perl
#Nisus Macro Block
#source front selection
#destination front selection
#End Nisus Macro Block
use utf8;
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");

while (my $line = <STDIN>) {
   $line =~ s/^[\t ]+//g; #Remove leading white space
   $line =~ s/\t\t+/\t/g; #Replace multiple tabs with one
   $line =~ s/\s+([,.;:\!\?\)\]}])/\1/g; #Remove white space preceding punctuation
   $line =~ s/([\(\[{])\s+/\1/g; #Remove white space after punctuation
   $line =~ s/(?<=[,:;\!\?])(?=\p{Latin})/ /g; #Add space after punctuation followed by a letter
   $line =~ s/(?<=\S)([\(\[{])/ \1/g; #Add space before punctuation preceded by a letter
   print $line;
}
End

This way I have more control to ensure nothing 'unexpected' happens with my text using this macro.

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/