[Tex/LaTex] Correctly highlighting custom html tags in listings

codehighlightinghtmllistingsminted

Problem statement

I have several code listings containing HTML markup in my latex files. These listings contain multiple custom html tags (created in combination with corresponding AngularJS directives).

Such an HTML fragment could look for example like this:

<list-component data="angularDataObject"
    list-type="dataActor">
</list-component>
<div id="test"></div>

Originating from a CamelCase name of a directive listComponent in JavaScript, a dash is added in between resulting in list-component to maintain code readability.

I tried using both and for syntax highlighting:

listings

Using I achieved the following result:

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=HTML]
<list-component data="angularDataObject"
    list-type="dataActor">
</list-component>
<div id="test"></div>
\end{lstlisting}
\end{document}

listings result

As you can see, the tag name is not identified, neither the custom attribute list-type. I think the word type is recognized as standard html attribute and therefore highlighted.

minted

Using I achieved the following result:

\documentclass{article}
\usepackage{minted}
\usemintedstyle{borland}
\begin{document}
\begin{minted}{html}
<list-component data="angularDataObject"
    list-type="dataActor">
</list-component>
<div id="test"></div>
\end{minted}
\end{document}

minted result

The minted result is even worse, as the word list is highlighted before the dash – component by contrast isn't, even though list is not an HTML standard tag. Attributes look fine this time, but the closing tag is not identified at all and gets an ugly background behind the opening bracket <.

My goal

I search for a plugin that is able to automaticly detect and highlight custom tags, without needing to add them each by each – since I might be unsure which tags I'll use in the future. Further it would be great to identify custom attributes, but I could go without that, if attributes are not highlighted at all.
Finally it is essential that closing tags are at least highlighted the same way as the opening ones were highlighted before.

Thanks in advance for your generous help and advices!

By the way, while typing this question and adding language data for different code highlighting I discovered that Google Code prettifier is analyzing and displaying my code perfectly :-/

Other related TEX.SE questions

\listings code style for HTML5 (CSS, HTML, JavaScript)
In this solution any tags must be noted in the settings in order to be identified.

How to highlight own keywords in HTML lstlistings?
This solution allows individual highlighting of code parts, but changes need to be hard-coded into the listing. This ruins readability in the TEX markup.

How to use angle brackets for a keyword in \listings?
This solution reuires once again that all tags are defined a priori.

Best Answer

Ok, after some thinking about how to do this with listings while avoiding manually specifying keywords, I suppose a syntax based solution may work.

I.e. I defined a language for listings using moredelim and morestring to handle the most basic syntax of XML and get the following results: enter image description here

the corresponding code is:

\documentclass{report}

\usepackage{listings}
\usepackage{color}

\lstdefinelanguage{XML_SYNTAX}{%
    alsoletter=-,
    morestring=[b]",stringstyle=\color[rgb]{0,0,1},
    moredelim=*[s][{\color[rgb]{0.75,0,0}}]{<}{>},
    moredelim=[s][{\color[rgb]{0,0,0}}]{<!--}{-->},
    moredelim=[s][{\color[rgb]{0,0.75,0}}]{\ }{=},
    moredelim=[s][{\color[rgb]{0,0.75,0}}]{\    }{=} % here there is \tab
}

\lstset{
    % Basic design
    backgroundcolor=\color[rgb]{0.9,0.9,0.9},
    basicstyle={\small\ttfamily},
    breaklines=true,
    frame=l,
    tabsize=2,
    % Line numbers
    xleftmargin={0.75cm},
    numbers=left,
    stepnumber=1,
    firstnumber=1,
    numberfirstline=true,
    % HTML formatting
    language=XML_SYNTAX,
}

\begin{document}
\begin{lstlisting}
<!-- with tab -->
<list-component data="angularDataObject"
    list-type="dataActor">
</list-component>
<!-- with space -->
<list-component data="angularDataObject"
  list-type="dataActor">
</list-component>
<!-- some other tag -->
<div id="test"></div>
\end{lstlisting}
\end{document}

For explanation of what the the code does, refer to sections 3.2, 3.3 and 4.18 onwards of the listings documentation.

Note: for some reason the list-type attribute is still wrong, although I told listings to treat - as a letter with alsoletter={-} - maybe you can figure out why that is, otherwise I'll have some time for more testing in a few hours.

This was due to my usage of a tab for indentation, rather than a space. As that is fixed now, more debugging to do, since the closing > of the list-component tag has now become green as well..

Thanks to @Jubobs this is fixed as well - it was my bad to have an extra * in the attribute style line. In addition I fixed things to that indentation with tabs also works.