Skip to content

Commit

Permalink
Support parsing Strong's entries from Version 2.x module (fix #49)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-klein committed Dec 2, 2023
1 parent 0a0c32c commit c785597
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
108 changes: 103 additions & 5 deletions src/sword_backend/strongs_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,17 @@ bool StrongsReference::hasValidKey()
return StrongsEntry::isValidStrongsKey(this->key);
}

StrongsEntry::StrongsEntry(string key, string rawEntry)
StrongsEntry::StrongsEntry(string key, string rawEntry, string moduleVersion)
{
this->key = key;
this->parseFromRawEntry(rawEntry);

char moduleMinorVersion = moduleVersion[0];

if (moduleMinorVersion == '1') {
this->parseFromVersion1RawEntry(rawEntry);
} else {
this->parseFromVersion2RawEntry(rawEntry);
}
}

bool StrongsEntry::isValidStrongsKey(std::string key)
Expand Down Expand Up @@ -113,10 +120,13 @@ StrongsEntry* StrongsEntry::getStrongsEntry(SWModule* module, string key)
return 0;
}

string moduleVersion = module->getConfigEntry("Version");

// Cut off the first character (H or G), since the Sword engine uses the actual number strings as the key for Strong's
string strongsNumberString = key.substr(1);
module->setKey(strongsNumberString.c_str());
StrongsEntry* strongsEntry = new StrongsEntry(key, module->getRawEntry());

StrongsEntry* strongsEntry = new StrongsEntry(key, module->getRawEntry(), moduleVersion);

return strongsEntry;
}
Expand Down Expand Up @@ -165,7 +175,7 @@ void StrongsEntry::parseDefinitionAndReferences(vector<string>& lines)

for (unsigned int i = 0; i < lines.size(); i++) {
string currentLine = lines[i];
if (currentLine.substr(0,5) == " see ") {
if (currentLine.substr(0,5) == " see " || currentLine.substr(0,4) == "see ") {
StringHelper::trim(currentLine);
StrongsReference reference(currentLine);
// Only put the current line into the list of references if it's not already in there
Expand Down Expand Up @@ -193,7 +203,7 @@ void StrongsEntry::parseDefinitionAndReferences(vector<string>& lines)
this->references = references;
}

void StrongsEntry::parseFromRawEntry(string rawEntry)
void StrongsEntry::parseFromVersion1RawEntry(string rawEntry)
{
this->rawEntry = rawEntry;

Expand All @@ -219,3 +229,91 @@ void StrongsEntry::parseFromRawEntry(string rawEntry)
this->eraseEmptyLines(allLines);
this->parseDefinitionAndReferences(allLines);
}

void StrongsEntry::parseFromVersion2RawEntry(string rawEntry)
{
this->rawEntry = rawEntry;

cout << rawEntry << endl;

vector<string> allLines = StringHelper::split(this->rawEntry, "\n");
if (allLines.size() == 0) {
return;
}

string details = allLines[0];
string phoneticTranscription = details;

// Parse the transcription
string transcriptionTag = "<orth rend=\"bold\" type=\"trans\">";
string transcriptionEndTag = "</orth>";
this->transcription = this->parseFromVersion2Element(details, transcriptionTag, transcriptionEndTag);

// Parse the phonetic transcription
string phoneticTranscriptionTag = "<pron rend=\"italic\">{";
string phoneticTranscriptionEndTag = "}</pron>";
this->phoneticTranscription = this->parseFromVersion2Element(phoneticTranscription, phoneticTranscriptionTag, phoneticTranscriptionEndTag);

// Parse the definition
string definition = allLines[1];
std::size_t lineBreakPosition = definition.find("<lb");
string defEndTag = "</def>";
std::size_t defEndTagPosition = definition.find(defEndTag);

if (lineBreakPosition != string::npos) {
// Line break existing
definition.erase(lineBreakPosition, string::npos);
} else {
// No line break existing

if (defEndTagPosition != string::npos) {
definition.erase(defEndTagPosition, string::npos);
}
}

StringHelper::trim(definition);

// Parse the references
string references = allLines[1];
string lineBreak = "<lb/>";
lineBreakPosition = references.find(lineBreak);

if (lineBreakPosition != string::npos) {
references.erase(0, lineBreakPosition);

defEndTagPosition = references.find(defEndTag);

if (defEndTagPosition != string::npos) {
references.erase(defEndTagPosition, string::npos);
}

references.erase(0, lineBreak.size());
}

StringHelper::trim(references);
vector<string> referenceLines = StringHelper::split(references, "<lb/> ");
this->parseDefinitionAndReferences(referenceLines);

// Store definition from variable above.
this->definition = definition;
}

string StrongsEntry::parseFromVersion2Element(string rawEntry, string startTag, string endTag)
{
std::size_t startTagPosition = rawEntry.find(startTag);
if (startTagPosition != string::npos) {
rawEntry.erase(0, startTagPosition);
}

std::size_t endTagPosition = rawEntry.find(endTag);

if (endTagPosition != string::npos) {
rawEntry.erase(endTagPosition, string::npos);
}

if (startTag.size() <= rawEntry.size()) {
rawEntry.erase(0, startTag.size());
}

return rawEntry;
}
6 changes: 4 additions & 2 deletions src/sword_backend/strongs_entry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class StrongsReference
class StrongsEntry
{
public:
StrongsEntry(std::string key, std::string rawEntry);
StrongsEntry(std::string key, std::string rawEntry, std::string moduleVersion);
virtual ~StrongsEntry(){}

static bool isValidStrongsKey(std::string key);
Expand All @@ -58,7 +58,9 @@ class StrongsEntry
std::vector<StrongsReference> references;

private:
void parseFromRawEntry(std::string rawEntry);
void parseFromVersion1RawEntry(std::string rawEntry);
void parseFromVersion2RawEntry(std::string rawEntry);
std::string parseFromVersion2Element(std::string rawEntry, std::string startTag, std::string endTag);
void parseFirstLine(std::string firstLine);
void eraseEmptyLines(std::vector<std::string>& lines);
void parseDefinitionAndReferences(std::vector<std::string>& lines);
Expand Down

0 comments on commit c785597

Please sign in to comment.