3

I have a URL for a HTTP-Request as the only content of an lstlisting environment, and I want to prettyprint it as follows:

  • color1 for the first line, and for ?, &, and |,

  • color2 for parameter names,

  • color3 for the parameter values (both characters and numbers).

I tried the to use the PHP language predefined by listings, but the way the base URL was typeset didn't satisfy me.

See my attempt below. As you can see, I colored the "keys" manually, which is probably not a good idea...

What should I do?

enter image description here

\documentclass[12pt,a4paper,oneside]{scrreprt}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{color1}{rgb}{0.0,  0.0,0.6}
\definecolor{color2}{rgb}{0.29, 0,  0.29}
\definecolor{color3}{rgb}{0.25, 0.5,0.5}


\lstset{
    basicstyle=\ttfamily,
    columns=fullflexible,
    showstringspaces=false 
}

\lstdefinelanguage{url}{
    moredelim=**[is][\color{color1}]{@}{@},
    showstringspaces=true,
    identifierstyle=\color{color2},
    keywordstyle=\color{color3},
    morekeywords={param1,param2,params,L},
}

\begin{document}    
    \lstset{language=url}
    \begin{lstlisting}
        http://domain.com/version/xyz.exe/abc
        ?L=sysname
        &param1=512
        &param2=1024
        &params=abc
        |paramA
        |a
        |paramB
        |b
    \end{lstlisting}
\end{document}
jub0bs
  • 58,916
Wiebke
  • 163

1 Answers1

3

More listings shenanigans...

The settings below should take care of automatically typesetting a URL with the colours you specified. Also, if everything works as expected, you shouldn't have to tediously add your parameter names to the list of keywords.

Note: refrain from indenting the contents of your lstlisting environment.

enter image description here

\documentclass[12pt,a4paper,oneside]{scrreprt}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{color1}{rgb}{0.0,  0.0,0.6}
\definecolor{color2}{rgb}{0.29, 0,  0.29}
\definecolor{color3}{rgb}{0.25, 0.5,0.5}


\lstset{
    basicstyle=\ttfamily,
    columns=fullflexible,
    showstringspaces=false 
}

\makeatletter
\lstdefinelanguage{url}{
    moredelim=[is][\ProcessAmpersand]{\&}{=},
    moredelim=[is][\ProcessQM]{?}{=},
    moredelim=[l][\color{color1}]{http://},
    alsoletter={0,1,2,3,4,5,6,7,8,9,.,/,:},
    showstringspaces=true,
    identifierstyle=\color{color3},
    literate = |{{\textcolor{color1}{|}}}1,
}

\def\ProcessAmpersand%
{%
    \lst@CalcLostSpaceAndOutput%
    \textcolor{color1}{\&}%
    \color{color2}%
    \aftergroup\ProcessClosingEq%
}

\def\ProcessQM%
{%
    \lst@CalcLostSpaceAndOutput%
    \textcolor{color1}{?}%
    \color{color2}%
    \aftergroup\ProcessClosingEq%
}

\def\ProcessClosingEq{\textcolor{color1}{=}}

\makeatother

\begin{document}    
\lstset{language=url}
\begin{lstlisting}
http://domain.com/version/xyz.exe/abc
?L=sysname
&param1=512
&param2=1024
&params=abc
|paramA
|a
|paramB
|b
\end{lstlisting}
\end{document}
jub0bs
  • 58,916