]> git.lyx.org Git - features.git/commitdiff
Properly resolve files and dois in InsetCitation (#12878)
authorJuergen Spitzmueller <spitz@lyx.org>
Wed, 16 Aug 2023 10:46:43 +0000 (12:46 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Wed, 16 Aug 2023 10:47:42 +0000 (12:47 +0200)
src/insets/InsetCitation.cpp
src/support/filetools.cpp
src/support/filetools.h

index a3d58f87e8a2d68e27827dc2e8b8b15ac1383913..c684b4c7a97748a25ea74705bc7546c8072e5e56 100644 (file)
@@ -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);
        }
index 6e9cf34fae4e908681a4a4797a7ec871e4fa52ff..d2191cf1fd0854aeabcfc2fd2b631b80a57611af 100644 (file)
 #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);
index 404dec210f591a41a5b039d1d586132d04040c08..205e7ff58977577fddebaa3e7ef81b25dd00eb97 100644 (file)
@@ -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.
  */