[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Smart quotes via finite state machine
From: |
onf |
Subject: |
Smart quotes via finite state machine |
Date: |
Mon, 23 Sep 2024 01:02:35 +0200 |
Hi all,
I've been thinking about ways to implement smart quotes with groff
lately. There are, of course, simple ways to do this (e.g. by defining
an inline macro " that switches back and forth between the opening and
closing quotes), but this doesn't work when auto-importing large
amounts of text from elsewhere (in which case it needs to be done
by hand).
I wondered if perhaps having some minimal context could allow one to
decide whether the " character is actually a quotation mark or just
an inch mark, and similarly with '. The idea is that opening quotation
marks are preceded by whitespace (unless at beginning of line), and
similarly closing quotation marks are followed either by whitespace
or punctuation (unless at end of line).
A pre-processor could presumably be written based on this, but the
requirement to be able to skip inline escapes would require that
the pre-processor embeds a complete troff parser, which seems like
way too much work. So what I'm thinking is that a bunch of groff's
.char requests could be generated by an external program in such a
way as to construct a finite state machine which does this within
groff[1].
For example, the program could, with the help of some Unicode data,
generate a list of whitespace characters, and make all of them
set some register to indicate the last character was a whitespace,
while all the other characters would reset that register.
Yeah, I know, it's kinda overkill, but it could work, I guess?
It sounds better than writing/repurposing a troff parser, and
the possibility of not having to bother with quotes seems too
nice to me.
I am attaching a diagram of this state machine concept. Please
let me know what you think.
~ onf
[1] Inspired by this suggestion from when this topic was last discussed
here: https://mail.gnu.org/archive/html/groff/2001-02/msg00015.html
quot.png
Description: PNG image
- Smart quotes via finite state machine,
onf <=