GNU m4:转义反引号 (`)

GNU m4: escaping backticks (`)

一个简单的 GNU m4 问题,但我找不到正确答案。我想打印一个 markdown header starting/ending 一个代码部分:

```
echo Hello
```

如何创建包含 3 个反引号的 GNU M4 宏?像

define(`md_code',````')
md_code
echo Hello
md_code

在 M4 邮件列表上得到了 Eric Blake 的答复: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.