gpoore / minted

minted is a LaTeX package that provides syntax highlighting using the Pygments library. Highlighted source code can be customized using fancyvrb.
1.74k stars 126 forks source link

CustomLexer: Setting environment variables #371

Closed hackerb9 closed 1 year ago

hackerb9 commented 1 year ago

Hi! While the current Custom Lexer method is serviceable, it puts a lot of work on the end user to know the intricacies of the low-level pygments library. Often, people just want to add a few keywords, for example to show certain strings as Types.

I've created a Custom Lexer which lets a user set environment variables to control the lexing. For example, one could do:

export PYGMENTS_CUSTOM_BASE_LEXER=CppLexer
export PYGMENTS_CUSTOM_TYPE="[ 'vec3', 'Atom', 'System' ]"
pygmentize -l custom example.c

That's nice and simple and works fine with LaTeX + Minted, but it has one big drawback: there needs to be a shell script or Makefile which sets those environment variables before running pdflatex.

My main question is: How would one go about setting environment variables in a LaTeX file such that they will be in effect before Minted executes pygmentize?

Also: Does this sounds like something other people would find useful?

Thanks!

muzimuzhi commented 1 year ago

My main question is: How would one go about setting environment variables in a LaTeX file such that they will be in effect before Minted executes pygmentize?

https://github.com/gpoore/minted/blob/5c4cf60e8b04d735a4e6b3695fdd456bda951727/source/minted.dtx#L1626-L1629

But it's a misuse. And, ways to set environments variables are OS-specific.

Ah, redefining \MintedPygmentize may affect too much. minted also executes sth like \MintedPygmentize\space -S <style> -f <formatter> -P commandprefix=PYG > <output_file> to create .pygstyle files, and at this time setting env vars is useless (hope it also does no harm).

gpoore commented 1 year ago

The situation with custom lexers (and just about everything else) is going to change significantly over the next 6-8 months. I've recently received a grant from the TeX User's Group to create minted v3.0. This will replace pygmentize with a new, minted-specific Python executable that is compatible with restricted shell escape (so no more -shell-escape with accompanying security vulnerabilities). This new executable will also make it possible to extend minted using Python, not just LaTeX macro programming as currently. So in the next 6-8 months, it should be possible to have a LaTeX option like extrakeywords={vec3, Atom, System}, and then this will be passed to Python through the new LaTeX-Python interface, and finally the lexer can be customized on the Python size. I'm currently working on the final releases for minted 2.x and programming the new LaTeX-Python interface. Beta versions of the new executable will probably become available around the end of the year or early in 2024. I'm hoping to get the initial minted v3.0 release into the initial TeX Live 2024 release, but we'll have to see how things go.

hackerb9 commented 1 year ago

The situation with custom lexers (and just about everything else) is going to change significantly over the next 6-8 months. I've recently received a grant from the TeX User's Group to create minted v3.0. This will replace pygmentize with a new, minted-specific Python executable that is compatible with restricted shell escape (so no more -shell-escape with accompanying security vulnerabilities).

Never have I been so happy to be told "No". 😁

🎉