From: Richard Kimberly Heck Date: Sun, 10 Jan 2021 06:50:01 +0000 (-0500) Subject: Count words in hyperlink X-Git-Tag: 2.4.0-alpha3~315 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=3f3d769107eb174d44961ef41c6b6120235d9bc7;p=lyx.git Count words in hyperlink --- diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index 6672ea1260..1cc145ab81 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 #include @@ -776,15 +775,11 @@ void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const os << "}"; } + pair InsetCitation::isWords() const { docstring const label = generateLabel(false); - int words = 1; - for (auto const & c : label) { - if (lyx::isSpace(c)) - words++; - } - return pair(label.size(), words); + return pair(label.size(), wordCount(label)); } diff --git a/src/insets/InsetHyperlink.cpp b/src/insets/InsetHyperlink.cpp index 3801f4de90..3f1131c4ad 100644 --- a/src/insets/InsetHyperlink.cpp +++ b/src/insets/InsetHyperlink.cpp @@ -283,6 +283,13 @@ void InsetHyperlink::validate(LaTeXFeatures & features) const } +pair InsetHyperlink::isWords() const +{ + docstring const label = getParam("name"); + return pair(label.size(), wordCount(label)); +} + + string InsetHyperlink::contextMenuName() const { return "context-hyperlink"; diff --git a/src/insets/InsetHyperlink.h b/src/insets/InsetHyperlink.h index 999e3753c0..974f2f431a 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 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 b85307f5b2..351c9775cc 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 b4f99f8156..3d42bb6031 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