Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU m4: escaping backticks (`)

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
like image 745
Pierre Avatar asked Apr 08 '15 18:04

Pierre


1 Answers

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.

like image 184
Pierre Avatar answered Oct 02 '22 09:10

Pierre