From: Juergen Spitzmueller Date: Wed, 16 Aug 2023 10:46:43 +0000 (+0200) Subject: Properly resolve files and dois in InsetCitation (#12878) X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=124eda3a0d1e0d9b442bbbab7c6daec9e4224506;p=features.git Properly resolve files and dois in InsetCitation (#12878) --- diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index a3d58f87e8..c684b4c7a9 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 6e9cf34fae..d2191cf1fd 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 +#include #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 404dec210f..205e7ff589 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. */