Search code examples
syntax-highlightingmustachelexerpygmentssyntaxhighlighter

Any Pygments lexer for the Mustache templating language?


I'd like to list source code of Mustache templates in my paper, and I'm using the minted package for highlighting code in my LaTeX source. The minted package simply uses Pygments. But there isn't any Pygments lexer for Mustache at the moment. Does anyone know an easy solution for my use case? For example, is there any existing Mustache lexer that is implemented in Python and is ready to use for Pygments?

Any suggestion or thoughts will be highly appreciated!

Best, Nicole


Solution

  • There is already a Handlebars lexer which you can use, as Handlebars is more or less a superset of Mustache. I say “more or less” because, last time I checked, Handlebars did not have Inverted Selections. The lexer in Pygments does not seem to support them, either. If your code uses them, you should be able to easily implement your own Mustache lexer by extending HandlebarsLexer and HandlebarsHtmlLexer. See also https://pygments.org/docs/lexerdevelopment/.

    Here is an example:

    \documentclass[preview]{standalone}
    
    \usepackage[newfloat]{minted}
    
    \begin{document}
    \begin{minted}{html+handlebars}
    <div id="cart">
      {{#cart_items}}
      <div class="cart-item">
        <span>{{name}}</span>
        <span>{{quantity}}</span>
        <span>{{price}}</span>
      </div>
      {{/cart_items}}
    </div>
    \end{minted}
    \end{document}
    

    Which results in:

    Example