groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Groff] Re: Groff digest, Vol 1 #1155 - 3 msgs


From: Larry McVoy
Subject: [Groff] Re: Groff digest, Vol 1 #1155 - 3 msgs
Date: Wed, 14 May 2003 07:49:27 -0700
User-agent: Mutt/1.4i

You may or may not find this useful.  We do a lot in roff style data
and we also revision control the data.  This is a tool to make the diffs
we get from the revision control system look nicer.  My definition of
nicer is that each sentence starts on a new line so if someone changes
a word in a sentence, it shows diffs for that sentence only.  Think about
using fmt(1) on a long paragraph and then inserting a word towards the
beginning.  It will look like the whole paragraph changed.  If the data
had been run through this, only one sentence would look changed.

Here's what the message above looks like after being run through the tool
(which I call tfmt):

You may or may not find this useful.
We do a lot in roff style data and we also revision control the
data.
This is a tool to make the diffs we get from the revision control
system look nicer.
My definition of nicer is that each sentence starts on a new line
so if someone changes a word in a sentence, it shows diffs for
that sentence only.
Think about using fmt(1) on a long paragraph and then inserting a
word towards the beginning.
It will look like the whole paragraph changed.
If the data had been run through this, only one sentence would
look changed.

#!/usr/bin/perl -w

@words = ();
while (1) {
        unless (defined($_ = <>)) {
                &flush;
                exit 0;
        }
        next if &blocks;
        if (/^[\.<]/) {
                &flush;
                print;
                next;
        }
        foreach $w (split(/\s+/, $_)) {
                push(@words, $w);
        }
}

# Handle all the various troff blocks
# .CS/.CE       code
# .DS/.DE       display
# .FS/.FE       footnotes
# .GS/.GE       graphical block (Unix/Windows)
# .TS/.TE       table
# .WS/.WE       windows graphical
sub blocks
{
        return 0 unless /^\.([CDFGTW])S/;
        &flush;
        $c = $1;
        print;
        while (defined($_ = <>)) {
                print;
                return 1 if /^\.$c/ && /^..E\s*$/;
        }
        exit 0;
}

sub flush
{
        my($w, @line);

        return unless ($#words > -1);
        @line = ();
        foreach $w (@words) {
                push(@line, $w);
                if (($w =~ /[\.\!\?]$/) && &should_break($w)) {
                        &fmt(@line);
                        @line = ();
                }
        }
        if ($#line != -1) {
                &fmt(@line);
        }
        @words = ();
}

sub fmt
{
        my($w, $len);

        $len = 0;
        foreach $w (@_) {
                if (($len > 0) && ($len + length($w) > 65)) {
                        print "\n";
                        $len = 0;
                }
                if ($len) {
                        print " ";
                        $len++;
                }
                print $w;
                $len += length($w);
        }
        print "\n" if $len;
}

# Don't break on "K." in "Donald K. Someone".
# Don't break on Mr. | Ms.
sub should_break
{
        my($w) = $_[0];

        return 0 if $w =~ /^.\.$/;
        return 0 if $w =~ /^mr\.$/i;
        return 0 if $w =~ /^ms\.$/i;
        return 1;
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]