[LyX/master] Consider known latex text macros (basically the logos) in convertaTeXCommands()

Juergen Spitzmueller spitz at lyx.org
Fri Aug 16 09:24:59 UTC 2024


commit 712600dd6cea52e97fd4dc78e3d9969f9f83a560
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Fri Aug 16 11:23:57 2024 +0200

    Consider known latex text macros (basically the logos) in convertaTeXCommands()
---
 src/Encoding.cpp          | 38 ++++++++++++++++++++++++++++++++++++--
 src/Encoding.h            |  2 +-
 src/insets/InsetIndex.cpp |  6 +++++-
 3 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/src/Encoding.cpp b/src/Encoding.cpp
index 33ae59045c..cff17ee051 100644
--- a/src/Encoding.cpp
+++ b/src/Encoding.cpp
@@ -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;
 }
 
diff --git a/src/Encoding.h b/src/Encoding.h
index 1321a5ff61..297650c847 100644
--- a/src/Encoding.h
+++ b/src/Encoding.h
@@ -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 {
 		///
diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp
index 0f8b16d210..be641015b9 100644
--- a/src/insets/InsetIndex.cpp
+++ b/src/insets/InsetIndex.cpp
@@ -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 {


More information about the lyx-cvs mailing list