[LyX/master] Count words in hyperlink

Richard Kimberly Heck rikiheck at lyx.org
Sun Jan 10 06:54:04 UTC 2021


commit 3f3d769107eb174d44961ef41c6b6120235d9bc7
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Sun Jan 10 01:50:01 2021 -0500

    Count words in hyperlink
---
 src/insets/InsetCitation.cpp  |    9 ++-------
 src/insets/InsetHyperlink.cpp |    7 +++++++
 src/insets/InsetHyperlink.h   |    2 ++
 src/support/lstrings.cpp      |   15 +++++++++++++++
 src/support/lstrings.h        |    3 +++
 5 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp
index 6672ea1..1cc145a 100644
--- a/src/insets/InsetCitation.cpp
+++ b/src/insets/InsetCitation.cpp
@@ -37,7 +37,6 @@
 #include "support/FileNameList.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
-#include "support/textutils.h"
 
 #include <algorithm>
 #include <climits>
@@ -776,15 +775,11 @@ void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const
 		os << "}";
 }
 
+
 pair<int, int> InsetCitation::isWords() const
 {
 	docstring const label = generateLabel(false);
-	int words = 1;
-	for (auto const & c : label) {
-		if (lyx::isSpace(c))
-			words++;
-	}
-	return pair<int, int>(label.size(), words);
+	return pair<int, int>(label.size(), wordCount(label));
 }
 
 
diff --git a/src/insets/InsetHyperlink.cpp b/src/insets/InsetHyperlink.cpp
index 3801f4d..3f1131c 100644
--- a/src/insets/InsetHyperlink.cpp
+++ b/src/insets/InsetHyperlink.cpp
@@ -283,6 +283,13 @@ void InsetHyperlink::validate(LaTeXFeatures & features) const
 }
 
 
+pair<int, int> InsetHyperlink::isWords() const
+{
+	docstring const label = getParam("name");
+	return pair<int, int>(label.size(), wordCount(label));
+}
+
+
 string InsetHyperlink::contextMenuName() const
 {
 	return "context-hyperlink";
diff --git a/src/insets/InsetHyperlink.h b/src/insets/InsetHyperlink.h
index 999e375..974f2f4 100644
--- a/src/insets/InsetHyperlink.h
+++ b/src/insets/InsetHyperlink.h
@@ -52,6 +52,8 @@ public:
 	void docbook(XMLStream &, OutputParams const &) const override;
 	///
 	docstring xhtml(XMLStream &, OutputParams const &) const override;
+	///
+	std::pair<int, int> isWords() const override;
 	//@}
 
 	/// \name Static public methods obligated for InsetCommand derived classes
diff --git a/src/support/lstrings.cpp b/src/support/lstrings.cpp
index b85307f..351c977 100644
--- a/src/support/lstrings.cpp
+++ b/src/support/lstrings.cpp
@@ -975,6 +975,21 @@ int count_char(docstring const & str, docstring::value_type chr)
 }
 
 
+int wordCount(docstring const & d)
+{
+	docstring dt = trim(d);
+	if (dt.empty())
+		return 0;
+	int words = 1;
+	for (auto const & c : dt) {
+		if (isSpace(c))
+			words++;
+	}
+	return words;
+}
+
+
+
 int count_bin_chars(string const & str)
 {
 	QString const qstr = toqstr(str).simplified();
diff --git a/src/support/lstrings.h b/src/support/lstrings.h
index b4f99f8..3d42bb6 100644
--- a/src/support/lstrings.h
+++ b/src/support/lstrings.h
@@ -204,6 +204,9 @@ int count_char(std::string const & str, char chr);
 /// Count all occurrences of char \a chr inside \a str
 int count_char(docstring const & str, docstring::value_type chr);
 
+/// get an approximate word count
+int wordCount(docstring const &);
+
 /** Count all occurrences of binary chars inside \a str.
     It is assumed that \a str is utf-8 encoded and that a binary char
     belongs to the unicode class names Zl, Zp, Cc, Cf, Cs, Co, or Cn


More information about the lyx-cvs mailing list