GNU m4: escaping backticks (`)
Asked Answered
A

1

11

A simple GNU 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
Alvinaalvine answered 8/4, 2015 at 18:34 Comment(0)
A
11

got an answer from Eric Blake on the M4 mailing list: http://lists.gnu.org/archive/html/m4-discuss/2015-04/msg00004.html

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 (backticks) in an unbalanced manner; and again in the macro expansion, since the expansion will output backticks in an unbalanced manner; for each changed quote, the original quotes must be restored. This assumes that the default quoting stays at (backticks) ' 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 (backticks) ' 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 (backticks) 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.

Alvinaalvine answered 9/4, 2015 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.