3

I'm using a combination of markdown and m4 to produce three different versions of a document depending on the flags given at the beginning. Let's call them Gold, Silver, and Bronze.

The problem that I've come across is that if I have a section that appears within an ifdef statement that has commas, m4 considers the rest of the section to be false.

ifdef(`GOLDANDSILVER',dnl
## Here's a subsection header

### Subsubsection about this, that, and the other thing

Aren't examples fun? Here's some punctuation, failure, and misfortune.
)dnl

Interestingly, it does not fail on the subsubsection, but fails in the body text.

My current, ugly, solution is to use a 'dummy comma' that can be piped to, and replaced by, sed.

ifdef(`GOLDANDSILVER',dnl
## Here's a subsection header

### Subsubsection about thisREPLACE_ME_COMMA thatREPLACE_ME_COMMA and the other thing

Aren't examples fun? Here's some punctuationREPLACE_ME_COMMA failureREPLACE_ME_COMMA and misfortune.
)dnl

I'm looking for a cleaner, preferably m4-only, solution that allows me to have "," in the body of my ifdef statements.


What I've learned by trial and error:

  • m4 seems to ignore any content or definitions on lines prefixed with #
  • [,] does not escape a comma
  • [[,]] does not escape a comma
aepksbuck
  • 367
  • 2
  • 10
  • Did you see my answer? Does it work? – uzsolt Mar 08 '17 at 19:34
  • It works on a small scale, but for whatever reason, there's something else in the document that causes it to no longer compile to HTML. For now, I'll just stick with REPLACE_ME_COMMA. – aepksbuck Mar 13 '17 at 14:24
  • If you provide a complex example I can check it. – uzsolt Mar 13 '17 at 17:39
  • https://comp.mail.sendmail.narkive.com/2JrKqHp4/how-to-escape-commas-in-m4#post2 Example: define(`INBOUND_CIDRS_VAL', ``"0.0.0.0/0, ::/0"'') – malcolm May 20 '21 at 08:35

1 Answers1

2

The best idea is to use string delimiters. The default string delimiters aren't the best solution (I think) - but you can change them with changequote.

You can change the comment delimiters (default is # and newline) - even can disable them.

I think the following will be good for you:

ifdef(`GOLDANDSILVER',changequote(XXX,YYY)dnl
XXX## Here's a subsection header

### Subsubsection about this, that, and the other thing

Aren't examples fun? Here's some punctuation, failure, and misfortune.YYY changequote()
)dnl

Please note the changequote(XXX,YYY): the quote begins with XXX and ends with YYY. In this case the line begin with # isn't comment because it's part of a string. And please note the changequote(): it restores the default string delimiters.

$  cat x.m4 | m4 -DGOLDANDSILVER # GOLDANDSILVER is defined
## Here's a subsection header

### Subsubsection about this, that, and the other thing

Aren't examples fun? Here's some punctuation, failure, and misfortune.
$  cat x.m4 | m4 # GOLDANDSILVER isn't defined
$
uzsolt
  • 5,832
  • 2
  • 20
  • 32