Re: escaping backticks (`)

Eric Blake <[email protected]> Thu, 09 Apr 2015 10:21:56 -0600
Newsgroups gmane.comp.gnu.m4.general
Organization Red Hat, Inc.
Message-ID <[email protected]>
On 04/09/2015 05:08 AM, Pierre Lindenbaum wrote:
> cross-posted on SO: http://stackoverflow.com/questions/29522699
> 
> 
> A simple/naive m4 question, but I cannot find the correct answer.
> I'd like to print a markdown header starting/ending a code section:
> 
> ```
> echo Hello
> ```
> 
> How do I create a GNU M4 macro containing the 3 backticks ? something like
> 
> define(`md_code',````')
> md_code
> echo Hello
> md_code

changequote is your friend.  This will do it:

define(`md_code', changequote([,
 ])[changequote([,])```changequote(`,')]changequote(`,'))

I have to change quotes twice: once around the macro definition, since
the definition itself intends to use ` in an unbalanced manner; and
again in the macro expansion, since the expansion will output ` in an
unbalanced manner; for each changed quote, the original quotes must be
restored.  This assumes that the default quoting stays at ` ' throughout
the m4 run.

Although in your case, I'd recommend using changequote up front to
something else, and globally write your input under those quoting rules
instead of the default ` ' quoting rules.  Remember that autoconf
intentionally went with [ ] as the quoting characters, because they were
much likely to be balanced in output, as opposed to ` and ' not
occurring in balanced pairs in shell scripts.  In fact, choosing 2- or
3-byte quoting strings is even less ambiguous, although it then requires
more typing.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc (application/pgp-signature, 604 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
Comment: Public key at http://people.redhat.com/eblake/eblake.gpg
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBCAAGBQJVJqckAAoJEKeha0olJ0NqypgIAK+W3ZfrE75LNV7A8eZkoZBU
BNQClk9+SdqXiIMsfdCXv7H7LwbsMLWjX6Ii9S831qdFyu6HXDhmhJYJsu+jZx80
ux1s8fmKUGs4KcI2/F+BNfVhmVLcHQUkNscfR32px4dSF4kJp5MYgfbGqmshIDtw
OaCCJfpSygRcNz8JiqClEC8CmrxbYdoLMhNQ0W91XLPkniLpmIm6Ubip2Yah8bXM
mluT/yKVPXJl7UKW+ZAS6dpQmPsxMJI5iBzfk2EyOvy+pUwsPWwfOv5w935ZVugJ
QTSHvD3OjDD3Z7BtaF4gn/6ZLgPZLejNgCJHJy/aFflSRNtczKWIYPPaLO3Vblo=
=w0Jt
-----END PGP SIGNATURE-----