[comp.sys.next] nroff output to rtf

mdixon@parc.xerox.com (Mike Dixon) (04/03/91)

when you look at man pages with the Librarian, it (sometimes) manages
to show them with reasonable formatting.  sometimes it doesn't, and
sometimes you want to look at them with something other than Librarian,
so here's a little hack to convert nroff output to rtf.

    a) it's in perl.  it would probably be trivial to rewrite in C,
       should you feel the need.

    b) it's the first perl program i ever wrote, so i'm sure it could
       be better, but it works pretty well.

    c) this version makes an rtf file with 12pt ohlfs, converting
       nroff underlining to rtf underlining.  it's trivial to use a
       different font (e.g. the Librarian uses helvetica) or a
       different attribute for underlining (e.g. Librarian uses
       italics).  if you want to do this and can't figure out the
       code, send me a message.

to use it, put it somewhere like /usr/local/bin/tty2rtf, and e.g.

    tty2rtf </usr/man/cat1/csh.1 | openfile

if anyone wants to do this right, it should be possible to hack
groff so that it generates rtf directly...

------------------------------------------------------------
#!/usr/local/bin/perl
print "{\\rtf0\\ansi{\\fonttbl\\f0\\fmodern Ohlfs;}\n",
  "\\paperw12160\n",
  "{\\colortbl\\red0\\green0\\blue0;}\n",
  "\\pard\\f0\\b0\\i0\\ul0\\fs24\\fc0\n";

$skip = 1;
while (<STDIN>) {
	chop;
	if ($_ ne "") {
		while (/(_\008.)+/) {
			do fix($`,"\n\\ul ");
			$bld = $&;
			$_ = $';
			$bld =~ s/_\008//g;
			do fix($bld,"\n\\ul0 ");
			}
		do fix($_,"\\\n");
		$skip = 0;
		}
	elsif (! $skip) {
		print "\\\n";
		$skip = 1;
		}
	}
print "}\n";

sub fix {
	local($tmp,$tail) = @_;
	$tmp =~ s/\\/\\\\/g;
	$tmp =~ s/\{/\\\{/g;
	$tmp =~ s/\}/\\\}/g;
	print $tmp,$tail;
	}
--

                                             .mike.