m4-discuss
[Top][All Lists]
Advanced

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

Re: escaping backticks (`)


From: Eric Blake
Subject: Re: escaping backticks (`)
Date: Thu, 09 Apr 2015 10:21:56 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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