[LyX/master] Properly resolve files and dois in InsetCitation (#12878)

Juergen Spitzmueller spitz at lyx.org
Wed Aug 16 09:32:13 UTC 2023


commit 124eda3a0d1e0d9b442bbbab7c6daec9e4224506
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Wed Aug 16 12:46:43 2023 +0200

    Properly resolve files and dois in InsetCitation (#12878)
---
 src/insets/InsetCitation.cpp |    6 ++++--
 src/support/filetools.cpp    |   18 ++++++++++++++++++
 src/support/filetools.h      |    3 +++
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp
index a3d58f8..c684b4c 100644
--- a/src/insets/InsetCitation.cpp
+++ b/src/insets/InsetCitation.cpp
@@ -35,6 +35,7 @@
 #include "support/debug.h"
 #include "support/docstream.h"
 #include "support/FileNameList.h"
+#include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
@@ -225,14 +226,15 @@ void InsetCitation::openCitation()
 		        << " citation search pattern: " << lyxrc.citation_search_pattern);
 		docstring locator;
 		if (!file.empty()) {
-			locator = file;
+			locator = provideScheme(file, from_ascii("file"));
 		} else if (!doi.empty()) {
-			locator = doi;
+			locator = provideScheme(doi, from_ascii("doi"));
 		} else if (!url.empty()) {
 			locator = url;
 		} else {
 			locator = "EXTERNAL " + titledata;
 		}
+		LYXERR(Debug::INSETS, "Resolved locator: " << locator);
 		FuncRequest cmd = FuncRequest(LFUN_CITATION_OPEN, locator);
 		lyx::dispatch(cmd);
 	}
diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp
index 6e9cf34..d2191cf 100644
--- a/src/support/filetools.cpp
+++ b/src/support/filetools.cpp
@@ -38,8 +38,10 @@
 #include "support/Systemcall.h"
 #include "support/qstring_helpers.h"
 #include "support/TempFile.h"
+#include "support/textutils.h"
 
 #include <QDir>
+#include <QUrl>
 
 #include "support/lassert.h"
 
@@ -942,6 +944,22 @@ string const getExtension(string const & name)
 }
 
 
+docstring const provideScheme(docstring const & name, docstring const & scheme)
+{
+	QUrl url(toqstr(name));
+	if (!url.scheme().isEmpty())
+		// Has a scheme. Return as is.
+		return name;
+	if (scheme == from_ascii("doi")) {
+		// check if it is the pure DOI (without URL)
+		if (isDigitASCII(name[1]))
+			return from_ascii("https://doi.org/") + name;
+	}
+	url.setScheme(toqstr(scheme));
+	return qstring_to_ucs4(url.toString());
+}
+
+
 string const unzippedFileName(string const & zipped_file)
 {
 	string const ext = getExtension(zipped_file);
diff --git a/src/support/filetools.h b/src/support/filetools.h
index 404dec2..205e7ff 100644
--- a/src/support/filetools.h
+++ b/src/support/filetools.h
@@ -240,6 +240,9 @@ addExtension(std::string const & name, std::string const & extension);
 /// Return the extension of the file (not including the .)
 std::string const getExtension(std::string const & name);
 
+/// Provide a scheme (such as "file") if not present. Assumes absolute path input.
+docstring const provideScheme(docstring const & name, docstring const & scheme);
+
 /** \return the name that LyX will give to the unzipped file \p zipped_file
   if the second argument of unzipFile() is empty.
  */


More information about the lyx-cvs mailing list