[LyX/master] DocBook: in ERTs, allow the use of \string.

Thibaut Cuvelier tcuvelier at lyx.org
Mon Feb 7 03:17:36 UTC 2022


commit 35588958ea9fe018eb3f89c1c4e50f9c6e748587
Author: Thibaut Cuvelier <tcuvelier at lyx.org>
Date:   Mon Feb 7 04:47:40 2022 +0100

    DocBook: in ERTs, allow the use of \string.
---
 src/insets/InsetERT.cpp |   64 +++++++++++++++++++++++++++++++++--------------
 1 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/src/insets/InsetERT.cpp b/src/insets/InsetERT.cpp
index 397f118..50b5daa 100644
--- a/src/insets/InsetERT.cpp
+++ b/src/insets/InsetERT.cpp
@@ -30,7 +30,7 @@
 #include "support/TempFile.h"
 
 #include <sstream>
-
+#include <regex>
 #include <iostream>
 
 using namespace std;
@@ -436,26 +436,52 @@ void InsetERT::docbook(XMLStream & xs, OutputParams const & runparams) const
 //		auto lay = getLayout();
 //	}
 
-	// Try to recognise some commands to have a nicer DocBook output. First step: some commands have a direct mapping
-	// to DocBook, mostly because the mapping is simply text or an XML entity.
-	docstring os_trimmed = trim(os.str());
+	// Try to recognise some commands to have a nicer DocBook output.
 	bool output_as_comment = true;
 
-	auto command_raw_translation = raw_latex_encoding_to_unicode_xml.find(os_trimmed);
-	if (command_raw_translation != raw_latex_encoding_to_unicode_xml.end()) {
-		xs << command_raw_translation->second;
-		output_as_comment = false;
-	} else {
-		// If the trimmed ERT ends with {}, try a mapping without it.
-		auto os_braces = os_trimmed.find(from_ascii("{}"));
-
-		if (os_braces != lyx::docstring::npos) {
-			auto key = os_trimmed.substr(0, os_braces);
-			auto command_braces_translation = raw_latex_encoding_to_unicode_xml.find(key);
-
-			if (command_braces_translation != raw_latex_encoding_to_unicode_xml.end()) {
-				xs << command_braces_translation->second;
-				output_as_comment = false;
+	// First step: some commands have a direct mapping to DocBook, mostly because the mapping is simply text or
+	// an XML entity.
+	{
+		docstring os_trimmed = trim(os.str());
+
+		auto command_raw_translation = raw_latex_encoding_to_unicode_xml.find(os_trimmed);
+		if (command_raw_translation != raw_latex_encoding_to_unicode_xml.end()) {
+			xs << command_raw_translation->second;
+			output_as_comment = false;
+		} else {
+			// If the trimmed ERT ends with {}, try a mapping without it.
+			auto os_braces = os_trimmed.find(from_ascii("{}"));
+
+			if (os_braces != lyx::docstring::npos) {
+				auto key = os_trimmed.substr(0, os_braces);
+				auto command_braces_translation = raw_latex_encoding_to_unicode_xml.find(key);
+
+				if (command_braces_translation != raw_latex_encoding_to_unicode_xml.end()) {
+					xs << command_braces_translation->second;
+					output_as_comment = false;
+				}
+			}
+		}
+	}
+
+	// Second step: the command \string can be ignored. If that's the only command in the ERT, then done.
+	// There may be several occurrences. (\string is 7 characters long.)
+	if (os.str().length() >= 7) {
+		docstring os_str = os.str();
+
+		while (os_str.length() >= 7) {
+			auto os_text = os_str.find(from_ascii("\\string"));
+
+			if (os_text != lyx::docstring::npos && !std::isalpha(static_cast<int>(os_str[os_text + 7]))) {
+				os_str = os_str.substr(0, os_text) + os_str.substr(os_text + 7, os_str.length());
+
+				if (os_str.find('\\') == std::string::npos) {
+					xs << os_str;
+					output_as_comment = false;
+					break;
+				}
+			} else {
+				break;
 			}
 		}
 	}


More information about the lyx-cvs mailing list