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