]> git.lyx.org Git - lyx.git/commitdiff
Account for old versions of Pygments
authorEnrico Forestieri <forenr@lyx.org>
Mon, 26 Jun 2017 21:23:23 +0000 (23:23 +0200)
committerEnrico Forestieri <forenr@lyx.org>
Mon, 26 Jun 2017 21:23:23 +0000 (23:23 +0200)
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
src/insets/InsetListings.cpp
src/insets/InsetListingsParams.cpp
src/insets/InsetListingsParams.h

index 02da0f78cbfe16e027e644db7ec66df1fdbf95e4..7f33dadd6579ce5f369bfdc09784d4f967304599 100644 (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())
index cf5892edec2a5896d7d5fa1fb88dd266856ae981..f75e155d4454a3034ed6578c814d4635a645b50f 100644 (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())
index 4fde290d6e94cc6b916cf216c145d7a23747d360..0daf4081852867bedbc9aeba46f222c4dbc0c269 100644 (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))
index e565e99dc83194457e57579ed3fb5840da353981..a7120badebb25bae325f1b1282ae4b1e1c2f7c6c 100644 (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;