Need help on line numbering macro

Have a problem? A question? This is the place for answers from other Express users.
Post Reply
martinkerz
Posts: 28
Joined: 2005-08-25 07:44:39

Need help on line numbering macro

Post by martinkerz »

Hey there,

I want to use NWX for working with linguistic transcripts. Therefore I need to add line numbers to some bigger text.

I modified the line numbering script that way, that it adds zeros in front of the line number, so that the line numbers have all the same length.

The new macro looks like this:

Code: Select all

#Nisus Macro Block
#source clipboard
#destination clipboard
#before execution
#Copy
#after execution
#Paste
#End Nisus Macro Block
my $count = 1;
while (my $line = <>) {
if ($count < 10)
  {
    print "000$count\t$line";
	$count++;
  }
elsif ($count < 100)
 {
    print "00$count\t$line";
	$count++;
  }
elsif ($count < 1000)
 {
     print "0$count\t$line";
	$count++;
  }
else
  {	print "$count\t$line";
	$count++;
}
}
I have nevertheless some more problems to solve and would be glad if anyone could help me out.

I need to get all lines numbered. I have to get the sot wrapped text hard wrapped somehow. Does anyone know if and how that's possible.

Moreover the macro does kill the styling of the text alltogether, changing it to the appearance of the first word in the text. So here's my second question: How can you keep the appearence (e.g. font color and weight) of the text intact?

I would be glad if you could help me out.[/b][/code]
dshan
Posts: 334
Joined: 2003-11-21 19:25:28
Location: Sydney, Australia

Line Numering Macro

Post by dshan »

As far as the styling of lines in Perl Macros the NWX User Guide has this to say:

"However, because Perl Macros are not yet sensitive to the attributes of text, when the text is copied from the document all attributes are removed. In practical terms this means if the text being changed has multiple fonts, styles or sizes before the change, after the change all such attributes will be the same. Although Nisus Writer Express provides a way to maintain formatting when changing text in a Perl Macro (through the option of including the RTF formatting in the copied text) only the advanced Perl connoisseurs will be able to take advantage of the feature. In future Nisus Writer Express will make the Perl macros aware of text formats."

Unless something has changed here with NWX 2.5, it seems you will have to use the "#Send Text as RTF" directive and learn the details of RTF tags and how to set/modify them in Perl.
dshan
Posts: 334
Joined: 2003-11-21 19:25:28
Location: Sydney, Australia

Simplified Line Numbering Macro

Post by dshan »

Here's a much simpler version of your line numbering macro. printf is your friend!

Code: Select all

#Nisus Macro Block
#source clipboard
#destination clipboard
#before execution
#Copy
#after execution
#Paste
#End Nisus Macro Block
my $count = 1;
while (my $line = <>) {
	printf("%.4u\t%s", $count++,$line);
} 
martinkerz
Posts: 28
Joined: 2005-08-25 07:44:39

Post by martinkerz »

First of all, thanks for the new Macro. Great! :-)

I think I'll have to get to know some RTF Tags. I'll report back what I'm going to learn.

Does anyone have a clue how I can get NWX to hard wrap text? That would be great. Right now I have the idea that I have to let the macro count the number of characters in a line. If this number is smaller than, say, 80 characters, nothing happens. If it surpasses 80, I'll try to let it insert a line break. Could that be some kind of reasonable approach?

Thanks

Martin
dshan
Posts: 334
Joined: 2003-11-21 19:25:28
Location: Sydney, Australia

Post by dshan »

martinkerz wrote:
Does anyone have a clue how I can get NWX to hard wrap text? That would be great. Right now I have the idea that I have to let the macro count the number of characters in a line. If this number is smaller than, say, 80 characters, nothing happens. If it surpasses 80, I'll try to let it insert a line break. Could that be some kind of reasonable approach?
Martin,

Yes, that seems the only way. Rather than inserting a regular line break ("\n" in Perl) you probably want to insert a soft return so it doesn't treat every line as a new paragraph. Some digging revealed that soft returns are RTF "\line" tags (vs "\par" for hard returns), but it's a lot easier to avoid using the "#Send Text as RTF" directive and just insert the hex codes for a soft return at the appropriate places using your macro. Further digging revealed that the hex codes for a soft return line break are 0xe2, 0x80, 0xa8 ("\xe2\x80\xa8" in Perl). Not surprisingly this turns out to be the Unicode line separator character (0x2028) encoded in UTF-8.

I tested this out and it seems to work fine, Perl happily creates soft returns in a document using those hex chars and NWX (both 2.1 and 2.5b6) displays the resulting text on separate lines as expected.

David.
Post Reply