Search code examples
markdownm4

Escaping Commas in m4 ifdef Statements


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

Solution

  • 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
    $