commit 28be7d552f62cc02fa86d7f79201d089bfb2d7b5 Author: Enrico Forestieri <for...@lyx.org> Date: Mon Jun 26 23:23:23 2017 +0200
Account for old versions of Pygments Pygments versions prior to 2.0 only accept lower case names for lexers. This commit makes sure to always use lower case names for the language that is written in the LaTeX file, while retaining the proper casing for the presentation in the GUI, which is dictated by compatibility with the listings package. Moreover, if one switches from listings to minted in a document, the language combo is properly updated even if the used language had attached a dialect (a concept not shared by minted), or even when importing a LaTeX document with tex2lyx. --- src/insets/InsetInclude.cpp | 2 +- src/insets/InsetListings.cpp | 4 ++-- src/insets/InsetListingsParams.cpp | 29 ++++++++++++++++++++++++++++- src/insets/InsetListingsParams.h | 3 +++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index 02da0f7..7f33dad 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -670,7 +670,7 @@ void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const if (!parameters.empty()) os << "[" << parameters << "]"; if (use_minted) - os << '{' << language << '}'; + os << '{' << ascii_lowercase(language) << '}'; os << '{' << incfile << '}'; if (use_minted && isfloat) { if (!caption.empty()) diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp index cf5892e..f75e155 100644 --- a/src/insets/InsetListings.cpp +++ b/src/insets/InsetListings.cpp @@ -268,7 +268,7 @@ void InsetListings::latex(otexstream & os, OutputParams const & runparams) const os << "\\mintinline"; if (!param_string.empty()) os << "[" << from_utf8(param_string) << "]"; - os << "{" << minted_language << "}"; + os << "{" << ascii_lowercase(minted_language) << "}"; } else { os << "\\lstinline"; if (!param_string.empty()) @@ -294,7 +294,7 @@ void InsetListings::latex(otexstream & os, OutputParams const & runparams) const os << breakln << "\\begin{minted}"; if (!param_string.empty()) os << "[" << param_string << "]"; - os << "{" << minted_language << "}\n" + os << "{" << ascii_lowercase(minted_language) << "}\n" << code << breakln << "\\end{minted}\n"; if (isfloat) { if (!caption.str.empty()) diff --git a/src/insets/InsetListingsParams.cpp b/src/insets/InsetListingsParams.cpp index 4fde290..0daf408 100644 --- a/src/insets/InsetListingsParams.cpp +++ b/src/insets/InsetListingsParams.cpp @@ -269,6 +269,31 @@ char const * allowed_languages = "[97]VRML\nXML\nXSLT"; +/// Return language allowed in the GUI without dialect and proper casing +string const languageonly(string const & lang) +{ + string const locase = ascii_lowercase(trim(lang, "{}")); + string const all_languages = ascii_lowercase(allowed_languages) + "\n"; + string language = (lang.at(0) == '[') ? locase + "\n" + : string("]") + locase + "\n"; + size_t i = all_languages.find(language); + if (i == string::npos && lang.at(0) != '[') { + language[0] = '\n'; + i = all_languages.find(language); + } + if (i == string::npos) + return lang; + if (all_languages.at(i) == '[') + i = all_languages.find(']', i); + if (i == string::npos) + return lang; + size_t j = all_languages.find('\n', i + 1); + if (j == string::npos) + return lang; + return string(allowed_languages).substr(i + 1, j - i - 1); +} + + /// ListingsParam Validator. /// This class is aimed to be a singleton which is instantiated in /// \c InsetListingsParams::addParam(). @@ -1020,11 +1045,13 @@ string InsetListingsParams::getValue(string const & key) const void InsetListingsParams::addParam(string const & key, - string const & value, bool replace) + string const & val, bool replace) { if (key.empty()) return; + string const value = (minted() && key == "language") ? languageonly(val) + : val; // duplicate parameters! string keyname = key; if (!replace && hasParam(key)) diff --git a/src/insets/InsetListingsParams.h b/src/insets/InsetListingsParams.h index e565e99..a7120ba 100644 --- a/src/insets/InsetListingsParams.h +++ b/src/insets/InsetListingsParams.h @@ -76,6 +76,9 @@ public: /// static int package() { return package_; } + /// + bool minted() { return package_ == 1; } + /// get value of option \c param std::string getParamValue(std::string const & param) const;