]> git.lyx.org Git - lyx.git/commitdiff
Allow an 'other' type for hyperlinks. Format change.
authorRichard Kimberly Heck <rikiheck@lyx.org>
Sun, 25 Dec 2022 17:42:07 +0000 (12:42 -0500)
committerRichard Kimberly Heck <rikiheck@lyx.org>
Sun, 25 Dec 2022 17:45:48 +0000 (12:45 -0500)
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
lib/lyx2lyx/lyx_2_4.py
src/frontends/qt/GuiHyperlink.cpp
src/frontends/qt/ui/HyperlinkUi.ui
src/insets/InsetHyperlink.cpp
src/version.h

index 1003fa07741fa483a4b0debd227e5afd735ed700..48ee9243f21193364a00ff9ef36638a8c65e26d3 100644 (file)
@@ -7,6 +7,9 @@ changes happened in particular if possible. A good example would be
 
 -----------------------
 
+2022-12-25 Richard Kimberly Heck <rikiheck@lyx.org>
+       * Format incremented to 614: New "Other" type for hyperlinks
+
 2022-12-11 Jürgen Spitzmüller <spitz@lyx.org> 
        * Format incremented to 613: Support \\fonts_default_family for non-TeX fonts.
 
index 520b14cce763784eb3fc5ad5d90674b6e80d2176..00a46e0ddbf238ef8fd71a02fd49587c99867b1f 100644 (file)
@@ -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]],
index c063ca236260789badf89e16a8178c55e010e019..257a5db249ad86c8b2d67028994cf1f3eda825be 100644 (file)
@@ -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");
index f3a4cb54bf6e18e58346136198f45bcd5b7bd2b1..9f19721cb05bbb6dba5983149c60f8de9decaf2b 100644 (file)
@@ -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">
         </property>
        </widget>
       </item>
+      <item>
+       <widget class="QRadioButton" name="noneRB">
+        <property name="text">
+         <string>Other</string>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
-       <width>112</width>
+       <width>40</width>
        <height>20</height>
       </size>
      </property>
index 3f1131c4ad3a86cb12a882a909026e5f89e61e52..4c1b43542c2503e11e04ea5c5484dc637da1b884 100644 (file)
@@ -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("&amp;")) + "\"");
+       docstring target = subst(getParam("target"), from_ascii("&"), from_ascii("&amp;")) ;
+       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);
 }
 
index c14f36db626382c2106a95601b433e7017c9200d..51d5678c7b274f896b4330f64c04fd48514cb701 100644 (file)
@@ -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