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.
This commit is contained in:
Enrico Forestieri 2017-06-26 23:23:23 +02:00
parent d87513a94d
commit 28be7d552f
4 changed files with 34 additions and 4 deletions

View File

@ -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())

View File

@ -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())

View File

@ -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))

View File

@ -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;