[comp.lang.perl] replacing " with `` and ''

djm@eng.umd.edu (David J. MacKenzie) (05/04/91)

Here's a little script I wrote when I needed to typeset some documents
that had been written with a word processor.  Anyone have a better way
to do this in perl?

#!/usr/local/bin/perl
# Change " to `` and '' for typesetting.
# Leave unchanged lines that start with `.', `'', or `\"',
# because they are probably troff code.
# David MacKenzie, djm@eng.umd.edu

$leftquote = 1;
while (<>) {
    if (!(/^[.\']/ || /^\\\"/)) {
	while (/\"/) {
	    if ($leftquote) {
		s/\"/\`\`/;
	    } else {
		s/\"/\'\'/;
	    }
	    $leftquote = !$leftquote;
	}
    }
    print;
}
--
David J. MacKenzie <djm@eng.umd.edu> <djm@ai.mit.edu>

bjaspan@athena.mit.edu (Barr3y Jaspan) (05/05/91)

In article <DJM.91May3235942@egypt.eng.umd.edu>, djm@eng.umd.edu (David J. MacKenzie) writes:
|> Here's a little script I wrote when I needed to typeset some documents
|> that had been written with a word processor.  Anyone have a better way
|> to do this in perl?

Well, I won't say this is necessarily ``better,'' but it is how I would have
done it.  Note that instead of keeping track of the $leftquote state, I just
assume some rules about how quotes are used in text (in fact, nearly the same
rules that emacs TeX mode uses).  A " that is followed by a "special symbol"
(which i've defined to be [ \n\t.?,], probably there are others) is assumed to
be a close-quote, and all others are open-quotes.


#!/afs/athena/contrib/perl/perl

$follow_end = "[ \n\t.!?,]";
while (<>) {
	if (! (/^[.\']/ || /^\"/)) {
		s/\"($follow_end)/\'\'$1/g;
		s/\"/\`\`/g;
	}
	print;
}


-- 
Barr3y Jaspan, bjaspan@mit.edu

raymond@math.berkeley.edu (Raymond Chen) (05/05/91)

In article <1991May4.184851.3472@athena.mit.edu>, bjaspan@athena (Barr3y Jaspan) writes:
>[Instead of counting quote marks, check the context to determine if it's an
>opening or closing quotation mark.]

This method is more robust in the sense that it doesn't cause problems
if your text contains nested quotations

  I wrote, "She said, `He said, "Ouch!"'"

or long quotations

  "This is a long quoted section of text that goes on for pages and
  pages.

  "Which means that you never see a closing quotation mark until the
  quotation is ended, but you'll see lots of opening quotation marks
  at the beginning of each paragraph."

or places where the symbol is not used as a quotation mark

  I'm not very tall; only 5'3".

or even just typos

  I wrote, "This is a long quotation, and I forgot to put a closing
  quotation mark at the end.  Later, I also wrote, "Another thing
  that I wrote."

What does this have to do with perl?  Beats me.

  was not a very useful reply.