Closed j-moeller closed 3 years ago
Hi @j-moeller, thanks for your pull request! Supporting lstlisting
environments is a great idea. Here are a couple thoughts:
lstlisting
environments.) I'd especially like to avoid duplicating code because it makes it much harder to maintain.ParsedLstListingArgs
class (in case in the future pylatexenc
is able to extract more information from an lstlisting
environment, maybe individual lines, etc.).lstlisting
environment accepts an optional argument. It would be great if pylatexenc
supported that, too!Concretely, what I suggest would be:
VerbatimArgsParser
class: (a) if verbatim_arg_type == 'verbatim-environment'
make its constructor accept an additional argument verbatim_environment_name
defaulting to "verbatim"
(but which could be set to "lstlisting"
) which sets the name of the environment that contains the verbatim content; (b) make the constructor accept another argument, say env_argspec
, providing a specification of the macro arguments that the begin-environment itself accepts (this way we can say that the lstlisting
environment accepts an optional argument); (c) make the constructor accept another argument, which should be the class object that will store the parsed argument(s) (by default ParsedVerbatimArgs
). The class should derive from ParsedVerbatimArgs
and its constructor should accept suitable keyword arguments (TBD) (maybe to include the verbatim environment name, additional arguments, etc.).lstlisting
environment can be declared as: EnvironmentSpec('lstlisting', args_parser=VerbatimArgsParser(verbatim_arg_type='verbatim-environment', verbatim_environment_name='lstlisting', env_argspec='[', parsed_args_class=ParsedLstListingArgs))
Thanks again for contributing and let me know if I'm missing anything!
The pull request fixes the parsing of the lstlisting environment which should be similar to the verbatim environment. Since I am not deeply familiar with the code base, most diff is basically copy-pasted from the verbatim environment (without the additional parameterization for the macro).
I added a test case which contains source code with a single opening curly bracket. If the lstlisting environment is not parsed verbatim, this corrupts the parse tree (every node after the lstlisting environment becomes a child of the environment).