BibleTime
cswordkey.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "cswordkey.h"
14 
15 #include <QRegularExpression>
16 #include <QRegularExpressionMatch>
17 #include <QString>
18 #include <string>
19 #include "../../util/btassert.h"
20 #include "../drivers/cswordmoduleinfo.h"
21 
22 // Sword includes:
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wextra-semi"
25 #pragma GCC diagnostic ignored "-Wsuggest-override"
26 #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
27 #ifdef __clang__
28 #pragma clang diagnostic push
29 #pragma clang diagnostic ignored "-Wsuggest-destructor-override"
30 #endif
31 #include <swkey.h>
32 #include <swmodule.h>
33 #include <utilstr.h>
34 #include <versekey.h>
35 #ifdef __clang__
36 #pragma clang diagnostic pop
37 #endif
38 #pragma GCC diagnostic pop
39 
40 
41 CSwordKey::~CSwordKey() noexcept = default;
42 
43 QString CSwordKey::normalizedKey() const { return key(); }
44 
45 QString CSwordKey::rawText() {
46  if (!m_module)
47  return QString();
48 
49  auto & m = m_module->swordModule();
50  m.getKey()->setText( rawKey() );
51 
52  if (key().isNull())
53  return QString();
54 
55  return QString::fromUtf8(m.getRawEntry());
56 }
57 
60 
61  auto & m = m_module->swordModule();
62  if (auto * const vk_mod = dynamic_cast<sword::VerseKey *>(m.getKey()))
63  vk_mod->setIntros(true);
64 
65  m.getKey()->setText(rawKey());
66 
68  m_module->snap();
69  /* In lexicons make sure that our key (e.g. 123) was successfully set to the module,
70  i.e. the module key contains this key (e.g. 0123 contains 123) */
71 
72  if (sword::stricmp(m.getKey()->getText(), rawKey())
73  && !strstr(m.getKey()->getText(), rawKey()))
74  {
75  qDebug("return an empty key for %s", m.getKey()->getText());
76  return QString();
77  }
78  }
79 
80  if (key().isNull())
81  return QString();
82 
83  bool DoRender = mode != ProcessEntryAttributesOnly;
84  auto text = QString::fromUtf8(m.renderText(nullptr, -1, DoRender).c_str());
85  if (!DoRender)
86  return QString();
87 
88  // This is yucky, but if we want strong lexicon refs we have to do it here.
90  const QString t(text);
91  static QRegularExpression const rx(
92  QStringLiteral(R"PCRE((GREEK|HEBREW) for 0*([1-9]\d*))PCRE"));
93  QRegularExpressionMatch match;
94  int pos = 0;
95  while ((pos = t.indexOf(rx, pos, &match)) != -1) {
96  auto language = match.captured(1);
97  auto langcode = language.at(0); // "G" or "H"
98  auto number = match.captured(2);
99  auto paddednumber = number.rightJustified(5, '0'); // Form 00123
100 
101  text.replace(
102  QRegularExpression(
103  QStringLiteral(
104  R"PCRE((>[^<>]+))PCRE" // Avoid replacing inside tags
105  R"PCRE(\\b(0*%1)\\b)PCRE") // And span around 0's
106  .arg(std::move(number))),
107  QStringLiteral("\\1<span lemma=\"%1%2\">"
108  "<a href=\"strongs://%3/%2\">\\2</a></span>")
109  .arg(std::move(langcode),
110  std::move(paddednumber),
111  std::move(language)));
112  pos += match.capturedLength();
113  }
114  }
115  return text;
116 }
117 
119  if (!m_module)
120  return QString();
121 
122  auto & m = m_module->swordModule();
123  m.getKey()->setText(std::string(rawKey()).c_str());
124 
125  return QString::fromUtf8(m.stripText());
126 }
#define BT_ASSERT(...)
Definition: btassert.h:17
QString strippedText()
Definition: cswordkey.cpp:118
virtual ~CSwordKey() noexcept
virtual const char * rawKey() const =0
const CSwordModuleInfo * m_module
Definition: cswordkey.h:112
QString renderedText(const CSwordKey::TextRenderType mode=CSwordKey::Normal)
Definition: cswordkey.cpp:58
TextRenderType
Definition: cswordkey.h:26
@ ProcessEntryAttributesOnly
Definition: cswordkey.h:28
QString rawText()
Definition: cswordkey.cpp:45
virtual QString key() const =0
virtual bool snap() const
ModuleType type() const
sword::SWModule & swordModule() const