minted icon indicating copy to clipboard operation
minted copied to clipboard

Ignore or drop language option

Open dpineiden opened this issue 5 years ago • 2 comments

Hello I'm trying to use a language named plaintuml, that not has minted lexer. So i think if i can set in a way to in that case the transformation to pdf must be in plain code block? There is a opcion to do that?

dpineiden avatar Aug 03 '20 06:08 dpineiden

A quick workaround:

\documentclass{article}
\usepackage{minted}

\makeatletter
% backup \\minted@pygmentize
\expandafter\let\expandafter\minted@pygmentize@old\expandafter=\csname\string\minted@pygmentize\endcsname

\renewcommand\minted@pygmentize[2][\minted@outputdir\[email protected]]{%
  \edef\@tempa{\noexpand\in@{#2}}%
  \expandafter\@tempa\expandafter{\minted@unsupported@language}%
  \ifin@
    \minted@pygmentize@old[#1]{text}%
  \else
    \minted@pygmentize@old[#1]{#2}%
  \fi
}

\newcommand*{\mintedUnsupportedLanguage}[1]{%
  \def\minted@unsupported@language{#1}%
}

\let\minted@unsupported@language\@empty
\makeatother

% user interface
\mintedUnsupportedLanguage{unsupported,unsupported2}

\begin{document}
\begin{minted}{text}
\newcommand\cmd[1]{#1}
\end{minted}

\begin{minted}{latex}
\newcommand\cmd[1]{#1}
\end{minted}

% this gives the same output of language "text"
\begin{minted}{unsupported}
\newcommand\cmd[1]{#1}
\end{minted}
\end{document}

With more efforts, this could be implemented as a minted option.

muzimuzhi avatar Aug 03 '20 12:08 muzimuzhi

Another attempt. This time the given language/lexer name is checked by pygments function get_lexer_by_name. The new flaw is, minted has to know which python executable pygments is installed for (here python3 is used).

\documentclass{article}
\usepackage{minted}
\usepackage{xpatch}

\makeatletter
\xpretocmd\minted@pygmentize
  {\minted@checklexer{#2}}
  {}{\fail}
\xpatchcmd\minted@pygmentize
  {-l #2}
  {-l \minted@lexer\space}
  {}{\fail}

\newcommand\minted@checklexer[1]{%
  \ShellEscape{python3 -c "%
    % # python code
    % from pygments.lexers import get_lexer_by_name
    % from pygments.util import ClassNotFound
    %
    %
    % try:
    %     get_lexer_by_name('<#1>')
    % except ClassNotFound:
    %     with open('<\[email protected]>', 'w') as f:
    %         f.write('')
    from pygments.lexers import get_lexer_by_name;
    from pygments.util import ClassNotFound;
    % trick: exec('<python compound statement>')
    exec('''%
      try:
        get_lexer_by_name('#1')\@backslashchar n%
      except ClassNotFound:\@backslashchar n%
        \space\space with open('\[email protected]', 'w') as f:
          f.write('')
    ''')
  "}%
  \IfFileExists{\[email protected]}
    {\def\minted@lexer{text}\DeleteFile{\[email protected]}}
    {\def\minted@lexer{#1}}%
}
\makeatother

\setminted{autogobble}

\begin{document}
known lexer \verb|tex|
\begin{minted}{tex}
  \newcommand\cmd[1]{#1 $\int_1^\infty$}
\end{minted}

unknown lexer, rollback to \verb|text|
\begin{minted}{unknown}
  \newcommand\cmd[1]{#1 $\int_1^\infty$}
\end{minted}
\end{document}

image

muzimuzhi avatar Aug 21 '21 23:08 muzimuzhi