[LyX/master] Allow an 'other' type for hyperlinks. Format change.

Richard Kimberly Heck rikiheck at lyx.org
Sun Dec 25 16:53:48 UTC 2022


commit 144cf4bb9afaa594ef61b93fdb03d34e4752f2ad
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Sun Dec 25 12:42:07 2022 -0500

    Allow an 'other' type for hyperlinks. Format change.
    
    Also, perform the URL fixing magic for DocBook and XHTML.
    
    As it was, it was impossible to enter e.g. "tel:" type links. Now
    choosing the "Other" type just outputs the URL as given.
    
    Also, the addition of "http" or "file" was not being done for
    DocBook and XHTML. Now it is.
---
 development/FORMAT                 |    3 ++
 lib/lyx2lyx/lyx_2_4.py             |   39 ++++++++++++++++++++++++++++++++--
 src/frontends/qt/GuiHyperlink.cpp  |   16 ++++++++++++-
 src/frontends/qt/ui/HyperlinkUi.ui |   13 +++++++++--
 src/insets/InsetHyperlink.cpp      |   40 ++++++++++++++++++++++-------------
 src/version.h                      |    4 +-
 6 files changed, 90 insertions(+), 25 deletions(-)

diff --git a/development/FORMAT b/development/FORMAT
index 1003fa0..48ee924 100644
--- a/development/FORMAT
+++ b/development/FORMAT
@@ -7,6 +7,9 @@ changes happened in particular if possible. A good example would be
 
 -----------------------
 
+2022-12-25 Richard Kimberly Heck <rikiheck at lyx.org>
+	* Format incremented to 614: New "Other" type for hyperlinks
+
 2022-12-11 Jürgen Spitzmüller <spitz at lyx.org> 
 	* Format incremented to 613: Support \\fonts_default_family for non-TeX fonts.
 
diff --git a/lib/lyx2lyx/lyx_2_4.py b/lib/lyx2lyx/lyx_2_4.py
index 520b14c..00a46e0 100644
--- a/lib/lyx2lyx/lyx_2_4.py
+++ b/lib/lyx2lyx/lyx_2_4.py
@@ -4639,7 +4639,38 @@ def revert_familydefault(document):
         
     document.header[i] = "\\font_default_family default"
     add_to_preamble(document, ["\\renewcommand{\\familydefault}{\\" + dfamily + "}"])
-        
+
+
+def revert_hyper_other(document):
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset CommandInset href", i)
+        if i == -1:
+            break
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Cannot find end of inset at line " << str(i))
+            i += 1
+            continue
+        k = find_token(document.body, "type \"other\"", i, j)
+        if k == -1:
+            i = j
+            continue
+        # build command
+        n = find_token(document.body, "name", i, j)
+        t = find_token(document.body, "target", i, j)
+        if n == -1 or t == -1:
+            document.warning("Malformed hyperlink inset at line " + str(i))
+            i = j
+            continue
+        name = document.body[n][6:-1]
+        target = document.body[t][8:-1]
+        cmd = "\href{" + target + "}{" + name + "}"
+        ecmd = put_cmd_in_ert(cmd)
+        document.body[i:j+1] = ecmd
+        i += 1
+
+                   
 ##
 # Conversion hub
 #
@@ -4714,10 +4745,12 @@ convert = [
            [610, []],
            [611, []],
            [612, [convert_starred_refs]],
-           [613, []]
+           [613, []],
+           [614, []]
           ]
 
-revert =  [[612, [revert_familydefault]],
+revert =  [[613, [revert_hyper_other]],
+           [612, [revert_familydefault]],
            [611, [revert_starred_refs]],
            [610, []],
            [609, [revert_index_macros]],
diff --git a/src/frontends/qt/GuiHyperlink.cpp b/src/frontends/qt/GuiHyperlink.cpp
index c063ca2..257a5db 100644
--- a/src/frontends/qt/GuiHyperlink.cpp
+++ b/src/frontends/qt/GuiHyperlink.cpp
@@ -17,6 +17,8 @@
 
 #include "insets/InsetHyperlink.h"
 
+#include "support/debug.h"
+
 #if defined(LYX_MERGE_FILES) && !defined(Q_CC_MSVC)
 // GCC couldn't find operator==
 namespace lyx {
@@ -48,6 +50,8 @@ GuiHyperlink::GuiHyperlink(QWidget * parent) : InsetParamsWidget(parent)
 		this, SIGNAL(changed()));
 	connect(fileRB, SIGNAL(clicked()),
 		this, SIGNAL(changed()));
+	connect(noneRB, SIGNAL(clicked()),
+		this, SIGNAL(changed()));
 
 	setFocusProxy(targetED);
 }
@@ -68,6 +72,10 @@ void GuiHyperlink::paramsToDialog(Inset const * inset)
 		emailRB->setChecked(true);
 	else if (type == "file:")
 		fileRB->setChecked(true);
+	else if (type == "other")
+		noneRB->setChecked(true);
+	else
+		LYXERR0("Unknown hyperlink type: " << type);
 }
 
 
@@ -79,10 +87,12 @@ bool GuiHyperlink::initialiseParams(std::string const & sdata)
 	targetED->setText(toqstr(params["target"]));
 	nameED->setText(toqstr(params["name"]));
 	literalCB->setChecked(params["literal"] == "true");
-	if (params["type"] == from_utf8("mailto:"))
+	if (params["type"] == "mailto:")
 		emailRB->setChecked(true);
-	else if (params["type"] == from_utf8("file:"))
+	else if (params["type"] == "file:")
 		fileRB->setChecked(true);
+	else if (params["type"] == "other")
+		noneRB->setChecked(true);
 	else
 		webRB->setChecked(true);
 	return true;
@@ -101,6 +111,8 @@ docstring GuiHyperlink::dialogToParams() const
 		params["type"] = from_utf8("mailto:");
 	else if (fileRB->isChecked())
 		params["type"] = from_utf8("file:");
+	else if (noneRB->isChecked())
+		params["type"] = from_utf8("other");
 	params["literal"] = literalCB->isChecked()
 			? from_ascii("true") : from_ascii("false");
 	params.setCmdName("href");
diff --git a/src/frontends/qt/ui/HyperlinkUi.ui b/src/frontends/qt/ui/HyperlinkUi.ui
index f3a4cb5..9f19721 100644
--- a/src/frontends/qt/ui/HyperlinkUi.ui
+++ b/src/frontends/qt/ui/HyperlinkUi.ui
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>290</width>
-    <height>188</height>
+    <width>306</width>
+    <height>226</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -160,6 +160,13 @@
         </property>
        </widget>
       </item>
+      <item>
+       <widget class="QRadioButton" name="noneRB">
+        <property name="text">
+         <string>Other</string>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
@@ -170,7 +177,7 @@
      </property>
      <property name="sizeHint" stdset="0">
       <size>
-       <width>112</width>
+       <width>40</width>
        <height>20</height>
       </size>
      </property>
diff --git a/src/insets/InsetHyperlink.cpp b/src/insets/InsetHyperlink.cpp
index 3f1131c..4c1b435 100644
--- a/src/insets/InsetHyperlink.cpp
+++ b/src/insets/InsetHyperlink.cpp
@@ -109,10 +109,10 @@ bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
 {
 	switch (cmd.action()) {
 	case LFUN_INSET_EDIT: {
+		docstring const & utype = getParam("type");
 		QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
-		bool url_valid = getParam("type").empty() && url.isValid();
-
-		flag.setEnabled(url_valid || getParam("type") == "file:");
+		bool url_valid = utype.empty() && url.isValid();
+		flag.setEnabled(url_valid || utype == "file:");
 		return true;
 		}
 
@@ -128,7 +128,6 @@ void InsetHyperlink::viewTarget() const
 		QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
 		if (!QDesktopServices::openUrl(url))
 			LYXERR0("Unable to open URL!");
-
 	} else if (getParam("type") == "file:") {
 		FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
 		string const format = theFormats().getFormatFromFile(url);
@@ -137,11 +136,20 @@ void InsetHyperlink::viewTarget() const
 }
 
 
+docstring makeURL(docstring const & url, docstring const & type) {
+	if (type == "other" ||
+			(!type.empty() && url.find(type) == 0))
+		return url;
+	return type + url;
+}
+
+
 void InsetHyperlink::latex(otexstream & os,
 			   OutputParams const & runparams) const
 {
-	docstring url = getParam("target");
-	docstring name = getParam("name");
+	docstring url   = getParam("target");
+	docstring name  = getParam("name");
+	docstring const & utype = getParam("type");
 	static char_type const chars_url[2] = {'%', '#'};
 
 	// For the case there is no name given, the target is set as name.
@@ -177,10 +185,9 @@ void InsetHyperlink::latex(otexstream & os,
 
 		// add "http://" when the type is web (type = empty)
 		// and no "://" or "run:" is given
-		docstring type = getParam("type");
 		if (url.find(from_ascii("://")) == string::npos
 			&& url.find(from_ascii("run:")) == string::npos
-			&& type.empty())
+			&& utype.empty())
 			url = from_ascii("http://") + url;
 
 	} // end if (!url.empty())
@@ -190,7 +197,7 @@ void InsetHyperlink::latex(otexstream & os,
 					ParamInfo::HANDLING_LATEXIFY);
 		// replace the tilde by the \sim character as suggested in the
 		// LaTeX FAQ for URLs
-		if (getParam("literal") != from_ascii("true")) {
+		if (getParam("literal") != "true") {
 			docstring const sim = from_ascii("$\\sim$");
 			for (size_t i = 0, pos;
 				(pos = name.find('~', i)) != string::npos;
@@ -203,7 +210,7 @@ void InsetHyperlink::latex(otexstream & os,
 		os << "\\protect";
 
 	// output the ready \href command
-	os << "\\href{" << getParam("type") << url << "}{" << name << '}';
+	os << "\\href{" << makeURL(url, utype) << "}{" << name << '}';
 }
 
 
@@ -226,7 +233,8 @@ int InsetHyperlink::plaintext(odocstringstream & os,
 
 void InsetHyperlink::docbook(XMLStream & xs, OutputParams const &) const
 {
-	xs << xml::StartTag("link", "xlink:href=\"" + subst(getParam("target"), from_ascii("&"), from_ascii("&")) + "\"");
+	docstring target = subst(getParam("target"), from_ascii("&"), from_ascii("&")) ;
+	xs << xml::StartTag("link", "xlink:href=\"" + makeURL(target, getParam("type")) + "\"");
 	xs << xml::escapeString(getParam("name"));
 	xs << xml::EndTag("link");
 }
@@ -236,8 +244,8 @@ docstring InsetHyperlink::xhtml(XMLStream & xs, OutputParams const &) const
 {
 	docstring const & target =
 		xml::escapeString(getParam("target"), XMLStream::ESCAPE_AND);
-	docstring const & name   = getParam("name");
-	xs << xml::StartTag("a", to_utf8("href=\"" + target + "\""));
+	docstring const & name = getParam("name");
+	xs << xml::StartTag("a", to_utf8("href=\"" + makeURL(target, getParam("type")) + "\""));
 	xs << (name.empty() ? target : name);
 	xs << xml::EndTag("a");
 	return docstring();
@@ -265,13 +273,15 @@ void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
 
 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
 {
-	docstring url = getParam("target");
-	docstring type = getParam("type");
+	docstring const & url = getParam("target");
+	docstring const & type = getParam("type");
 	docstring guitype = _("www");
 	if (type == "mailto:")
 		guitype = _("email");
 	else if (type == "file:")
 		guitype = _("file");
+	else if (type == "other")
+		guitype = _("other");
 	return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
 }
 
diff --git a/src/version.h b/src/version.h
index c14f36d..51d5678 100644
--- a/src/version.h
+++ b/src/version.h
@@ -32,8 +32,8 @@ extern char const * const lyx_version_info;
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-#define LYX_FORMAT_LYX 613 // spitz: \defaultfamily for non-TeX fonts
-#define LYX_FORMAT_TEX2LYX 613
+#define LYX_FORMAT_LYX 614 // rkh: Add 'other' option to hyperlink
+#define LYX_FORMAT_TEX2LYX 614
 
 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX
 #ifndef _MSC_VER


More information about the lyx-cvs mailing list