]> git.lyx.org Git - lyx.git/commitdiff
Consider known latex text macros (basically the logos) in convertaTeXCommands()
authorJuergen Spitzmueller <spitz@lyx.org>
Fri, 16 Aug 2024 09:23:57 +0000 (11:23 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Fri, 16 Aug 2024 09:23:57 +0000 (11:23 +0200)
src/Encoding.cpp
src/Encoding.h
src/insets/InsetIndex.cpp

index 33ae59045c48c6ba1a4c332de8ae48eb99077a62..cff17ee051807d3051c2666a4eb8012a0d4d53ba 100644 (file)
@@ -614,23 +614,30 @@ docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
 }
 
 
-docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_xhtml)
+/// text macros we can convert beyond unicodesymbols
+char const * const known_text_macros[] = {"LyX", "TeX", "LaTeXe", "LaTeX", ""};
+char const * const known_text_macros_out[] = {"LyX", "TeX", "LaTeX2e", "LaTeX", ""};
+
+
+docstring Encodings::convertLaTeXCommands(docstring const & str, bool const literal_math)
 {
        docstring val = str;
        docstring ret;
        docstring mret;
+       docstring cret;
 
        bool scanning_cmd = false;
        bool scanning_math = false;
        bool is_section = false;
        bool escaped = false; // used to catch \$, etc.
+       bool skip_space = false;
        while (!val.empty()) {
                char_type const ch = val[0];
 
                // if we're scanning math, we collect everything until we
                // find an unescaped $, and then try to convert this piecewise.
                if (scanning_math) {
-                       if (for_xhtml) {
+                       if (literal_math) {
                                // with xhtml, we output everything until we
                                // find an unescaped $, at which point we break out.
                                if (escaped)
@@ -688,6 +695,7 @@ docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_
                                continue;
                        }
                        if (isAlphaASCII(ch)) {
+                               cret += ch;
                                is_section = false;
                                val = val.substr(1);
                                escaped = false;
@@ -703,6 +711,17 @@ docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_
                        scanning_cmd = false;
                }
 
+               // check if it's a know text macro
+               // If so, output and skip the following space
+               if (!cret.empty()) {
+                       int const n = findToken(known_text_macros, to_ascii(cret));
+                       if (n != -1) {
+                               ret += known_text_macros_out[n];
+                               skip_space = true;
+                       }
+                       cret.clear();
+               }
+
                // was the last character a \? If so, then this is something like:
                // \\ or \$, so we'll just output it. That's probably not always right...
                if (escaped) {
@@ -728,6 +747,13 @@ docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_
                        continue;
                }
 
+               if (isSpace(ch) && skip_space) {
+                       val = val.substr(1);
+                       skip_space = false;
+                       continue;
+               }
+               skip_space = false;
+
                // Change text mode accents in the form
                // {\v a} to \v{a} (see #9340).
                // FIXME: This is a sort of mini-tex2lyx.
@@ -780,6 +806,14 @@ docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_
                escaped = true;
                val = val.substr(1);
        }
+       // check if it's a know text macro
+       // If so, output and skip the following space
+       if (!cret.empty()) {
+               int const n = findToken(known_text_macros, to_ascii(cret));
+               if (n != -1)
+                       ret += known_text_macros_out[n];
+       }
+
        return ret;
 }
 
index 1321a5ff617fbe786bd23e3ebdc3f538d2081e95..297650c847bf3bc2f9f24b7b2adc31ee6eb5cefd 100644 (file)
@@ -352,7 +352,7 @@ public:
        /// converts a string containing LaTeX commands into unicode
        /// for display.
        static docstring convertLaTeXCommands(docstring const & str,
-                                             bool const for_xhtml = false);
+                                             bool const literal_math = false);
        ///
        enum LatexCmd {
                ///
index 0f8b16d21020e50f307d5dac5bc4b341a651a0ac..be641015b9f1664ab942006c0b8ec5f0e958ed93 100644 (file)
@@ -266,7 +266,11 @@ void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) cons
                                // We do this on all levels.
                                // We don't do it if the level already contains a '@', though.
                                // We use a somewhat "plain" representation for this
-                               docstring const spart = Encodings::convertLaTeXCommands(thislevel);
+                               docstring spart = Encodings::convertLaTeXCommands(thislevel);
+                               // if convertLaTeXCommands() returns nothing, we fall back
+                               // to the command name without backslash
+                               if (trim(spart).empty())
+                                       spart = ltrim(thislevel, "\\");
                                processLatexSorting(os, runparams, thislevel, spart, escape_char);
                        }
                } else {