Skip to content

Commit

Permalink
Modernize the code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed Jun 3, 2024
1 parent 2f1271c commit d9efd95
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 67 deletions.
139 changes: 77 additions & 62 deletions compare/compare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
#include <jobs/index.h>


void compare_compare (std::string bible, std::string compare, int jobId)
// Compare the $bible with another Bible, passed through $compare.
void compare_compare (const std::string& bible, const std::string& compare, const int job_id)
{
Database_Logs::log (translate("Comparing Bibles") + " " + bible + " " + translate ("and") + " " + compare, Filter_Roles::consultant ());

Expand All @@ -50,26 +51,30 @@ void compare_compare (std::string bible, std::string compare, int jobId)
const std::string stylesheet = database::config::bible::get_export_stylesheet (bible);


database_jobs.set_progress (jobId, translate("The Bibles are being compared..."));
database_jobs.set_progress (job_id, translate("The Bibles are being compared..."));


// The results of the comparison. Will be displayed to the user.
std::vector <std::string> result;
result.push_back (translate("Bible") + " '" + bible + "' " + translate ("has been compared with") + " '" + compare + "'.");
{
std::stringstream ss {};
ss << translate("Bible") << " " << std::quoted(bible) << " " << translate ("has been compared with") << " " << std::quoted(compare) << ".";
result.push_back (ss.str());
}
result.push_back (translate("Additions are in bold.") + " " + translate ("Removed words are in strikethrough."));
result.push_back ("");
result.push_back (std::string());


// Get the combined books in both Bibles / Resources.
std::vector <int> bibleBooks = database_bibles.get_books (bible);
std::vector <int> compareBooks = database_bibles.get_books (compare);
std::vector <int> resourceBooks = database_usfmresources.getBooks (compare);
// Get the combined distinct books in both Bibles / Resources.
const std::vector <int> bible_books = database_bibles.get_books (bible);
const std::vector <int> compare_books = database_bibles.get_books (compare);
const std::vector <int> resource_books = database_usfmresources.getBooks (compare);
std::vector <int> books;
{
std::set <int> bookset;
bookset.insert (bibleBooks.begin(), bibleBooks.end());
bookset.insert (compareBooks.begin(), compareBooks.end());
bookset.insert (resourceBooks.begin(), resourceBooks.end());
bookset.insert (bible_books.begin(), bible_books.end());
bookset.insert (compare_books.begin(), compare_books.end());
bookset.insert (resource_books.begin(), resource_books.end());
books.assign (bookset.begin(), bookset.end ());
std::sort (books.begin(), books.end());
}
Expand All @@ -87,110 +92,120 @@ void compare_compare (std::string bible, std::string compare, int jobId)
std::vector <std::string> new_verses;


for (auto & book : books) {
for (const auto& book : books) {


std::string bookName = database::books::get_english_from_id (static_cast<book_id>(book));
database_jobs.set_progress (jobId, bookName);
const std::string book_name = database::books::get_english_from_id (static_cast<book_id>(book));
database_jobs.set_progress (job_id, book_name);


if (find (bibleBooks.begin(), bibleBooks.end(), book) == bibleBooks.end()) {
absent.push_back (translate("Bible") + " '" + bible + "' " + translate ("does not contain") + " " + bookName + ".");
if (std::find (bible_books.begin(), bible_books.end(), book) == bible_books.end()) {
std::stringstream ss {};
ss << translate("Bible") << " " << std::quoted(bible) << " " << translate ("does not contain") << " " << book_name << ".";
absent.push_back (ss.str());
continue;
}

if (find (compareBooks.begin(), compareBooks.end(), book) == compareBooks.end()) {
if (find (resourceBooks.begin(), resourceBooks.end(), book) == resourceBooks.end ()) {
absent.push_back (translate("Bible/Resource") + " '" + compare + "' " + translate ("does not contain") + " " + bookName + ".");
if (std::find (compare_books.begin(), compare_books.end(), book) == compare_books.end()) {
if (std::find (resource_books.begin(), resource_books.end(), book) == resource_books.end ()) {
std::stringstream ss{};
ss << translate("Bible/Resource") << " " << std::quoted(compare) << " " << translate ("does not contain") << " " << book_name << ".";
absent.push_back (ss.str());
continue;
}
}


// Get the combined chapters in both Bibles / Resources.
std::vector <int> bibleChapters = database_bibles.get_chapters (bible, book);
std::vector <int> compareChapters = database_bibles.get_chapters (compare, book);
std::vector <int> resourceChapters = database_usfmresources.getChapters (compare, book);
// Get the combined distinct chapters in both Bibles / Resources.
const std::vector <int> bible_chapters = database_bibles.get_chapters (bible, book);
const std::vector <int> compare_chapters = database_bibles.get_chapters (compare, book);
const std::vector <int> resource_chapters = database_usfmresources.getChapters (compare, book);
std::vector <int> chapters;
{
std::set <int> chapterset;
chapterset.insert (bibleChapters.begin(), bibleChapters.end());
chapterset.insert (compareChapters.begin(), compareChapters.end());
chapterset.insert (resourceChapters.begin(), resourceChapters.end());
chapterset.insert (bible_chapters.begin(), bible_chapters.end());
chapterset.insert (compare_chapters.begin(), compare_chapters.end());
chapterset.insert (resource_chapters.begin(), resource_chapters.end());
chapters.assign (chapterset.begin(), chapterset.end ());
std::sort (chapters.begin(), chapters.end());
}


for (const auto & chapter : chapters) {
for (const auto& chapter : chapters) {


// Look for, report, and skip missing chapters in the source Bible.
if (find (bibleChapters.begin(), bibleChapters.end(), chapter) == bibleChapters.end ()) {
absent.push_back (translate("Bible") + " '" + bible + "' " + translate ("does not contain") + " " + bookName + " " + std::to_string (chapter) + ".");
if (std::find (bible_chapters.begin(), bible_chapters.end(), chapter) == bible_chapters.end ()) {
std::stringstream ss {};
ss << translate("Bible") << " " << std::quoted(bible) << " " << translate ("does not contain") << " " << book_name << " " << chapter << ".";
absent.push_back (ss.str());
continue;
}


// Look for, report, and skip missing chapters in the comparison USFM data.
if (find (compareChapters.begin(), compareChapters.end(), chapter) == compareChapters.end()) {
if (find (resourceChapters.begin(), resourceChapters.end(), chapter) == resourceChapters.end()) {
absent.push_back (translate("Bible/Resource") + " '" + compare + "' " + translate ("does not contain") + " " + bookName + " " + std::to_string (chapter) + ".");
if (std::find (compare_chapters.begin(), compare_chapters.end(), chapter) == compare_chapters.end()) {
if (std::find (resource_chapters.begin(), resource_chapters.end(), chapter) == resource_chapters.end()) {
std::stringstream ss {};
ss << translate("Bible/Resource") << " " << std::quoted(compare) << " " << translate ("does not contain") << " " << book_name << " " << chapter << ".";
absent.push_back (ss.str());
continue;
}
}


// Get source and compare USFM, and skip them if they are equal.
std::string bible_chapter_usfm = database_bibles.get_chapter (bible, book, chapter);
const std::string bible_chapter_usfm = database_bibles.get_chapter (bible, book, chapter);
std::string compare_chapter_usfm = database_bibles.get_chapter (compare, book, chapter);
if (compare_chapter_usfm == "") {
if (compare_chapter_usfm.empty()) {
compare_chapter_usfm = database_usfmresources.getUsfm (compare, book, chapter);
}
if (bible_chapter_usfm == compare_chapter_usfm) continue;
if (bible_chapter_usfm == compare_chapter_usfm)
continue;


// Get the combined set of verses in the chapter of the Bible and of the USFM to compare with.
std::vector <int> bible_verse_numbers = filter::usfm::get_verse_numbers (bible_chapter_usfm);
std::vector <int> compare_verse_numbers = filter::usfm::get_verse_numbers (compare_chapter_usfm);
std::vector <int> verses;
// Get the sorted combined set of distinct verses in the chapter of the Bible and of the USFM to compare with.
std::vector <int> verses {};
{
std::set <int> verseset;
verseset.insert (bible_verse_numbers.begin(), bible_verse_numbers.end());
verseset.insert (compare_verse_numbers.begin(), compare_verse_numbers.end());
verses.assign (verseset.begin(), verseset.end ());
const std::vector <int> bible_verse_numbers = filter::usfm::get_verse_numbers (bible_chapter_usfm);
const std::vector <int> compare_verse_numbers = filter::usfm::get_verse_numbers (compare_chapter_usfm);
std::set <int> verse_set {};
verse_set.insert (bible_verse_numbers.begin(), bible_verse_numbers.end());
verse_set.insert (compare_verse_numbers.begin(), compare_verse_numbers.end());
verses.assign (verse_set.begin(), verse_set.end ());
std::sort (verses.begin(), verses.end());
}


for (const int & verse : verses) {
for (const int& verse : verses) {


// Get the USFM of verse of the Bible and comparison USFM, and skip it if both are the same.
std::string bible_verse_usfm = filter::usfm::get_verse_text (bible_chapter_usfm, verse);
std::string compare_verse_usfm = filter::usfm::get_verse_text (compare_chapter_usfm, verse);
if (bible_verse_usfm == compare_verse_usfm) continue;
const std::string bible_verse_usfm = filter::usfm::get_verse_text (bible_chapter_usfm, verse);
const std::string compare_verse_usfm = filter::usfm::get_verse_text (compare_chapter_usfm, verse);
if (bible_verse_usfm == compare_verse_usfm)
continue;

Filter_Text filter_text_bible = Filter_Text (bible);
Filter_Text filter_text_compare = Filter_Text (compare);
filter_text_bible.html_text_standard = new HtmlText ("");
filter_text_compare.html_text_standard = new HtmlText ("");
filter_text_bible.html_text_standard = new HtmlText (std::string());
filter_text_compare.html_text_standard = new HtmlText (std::string());
filter_text_bible.text_text = new Text_Text ();
filter_text_compare.text_text = new Text_Text ();
filter_text_bible.add_usfm_code (bible_verse_usfm);
filter_text_compare.add_usfm_code (compare_verse_usfm);
filter_text_bible.run (stylesheet);
filter_text_compare.run (stylesheet);
std::string bible_html = filter_text_bible.html_text_standard->get_inner_html ();
std::string compare_html = filter_text_compare.html_text_standard->get_inner_html ();
std::string bible_text = filter_text_bible.text_text->get ();
std::string compare_text = filter_text_compare.text_text->get ();
const std::string bible_html = filter_text_bible.html_text_standard->get_inner_html ();
const std::string compare_html = filter_text_compare.html_text_standard->get_inner_html ();
const std::string bible_text = filter_text_bible.text_text->get ();
const std::string compare_text = filter_text_compare.text_text->get ();
if (bible_text != compare_text) {
std::string modification = filter_diff_diff (compare_text, bible_text);
const std::string modification = filter_diff_diff (compare_text, bible_text);
result.push_back (filter_passage_display (book, chapter, std::to_string (verse)) + " " + modification);
new_verses.push_back (filter_passage_display (book, chapter, std::to_string (verse)) + " " + bible_text);
}
std::string modification = filter_diff_diff (compare_verse_usfm, bible_verse_usfm);
const std::string modification = filter_diff_diff (compare_verse_usfm, bible_verse_usfm);
raw.push_back (filter_passage_display (book, chapter, std::to_string (verse)) + " " + modification);
}
}
Expand All @@ -199,37 +214,37 @@ void compare_compare (std::string bible, std::string compare, int jobId)

// Add the absent books / chapters to the comparison.
if (!absent.empty ()) {
result.push_back ("");
result.push_back (std::string());
result.insert (result.end (), absent.begin(), absent.end());
}


// Add any differences in the raw USFM to the comparison.
if (!raw.empty ()) {
result.push_back ("");
result.push_back (std::string());
result.insert (result.end (), raw.begin(), raw.end());
}


// Add the text of the new verses, as they are in the $bible.
if (!new_verses.empty ()) {
result.push_back ("");
result.push_back (std::string());
result.push_back (translate("The texts as they are in the Bible") + " " + bible);
result.push_back ("");
result.push_back (std::string());
result.insert (result.end(), new_verses.begin(), new_verses.end());
}


// Format and store the result of the comparison.
for (auto & line : result) {
if (line == "") {
for (auto& line : result) {
if (line.empty()) {
line = "<br>";
} else {
line.insert (0, "<p>");
line.append ("</p>");
}
}
database_jobs.set_result (jobId, filter::strings::implode (result, "\n"));
database_jobs.set_result (job_id, filter::strings::implode (result, "\n"));


Database_Logs::log (translate("Comparison is ready"), Filter_Roles::consultant ());
Expand Down
2 changes: 1 addition & 1 deletion compare/compare.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

#include <config/libraries.h>

void compare_compare (std::string bible, std::string compare, int jobId);
void compare_compare (const std::string& bible, const std::string& compare, const int job_id);
8 changes: 4 additions & 4 deletions compare/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::string compare_index (Webserver_Request& webserver_request)
header.add_bread_crumb (bible_manage_url (), menu_logic_bible_manage_text ());
page = header.run ();

Assets_View view;
Assets_View view {};

const std::string bible = webserver_request.query ["bible"];
view.set_variable ("bible", bible);
Expand All @@ -86,7 +86,7 @@ std::string compare_index (Webserver_Request& webserver_request)
std::vector <std::string> names = webserver_request.database_bibles()->get_bibles ();

Database_UsfmResources database_usfmresources;
std::vector <std::string> usfm_resources = database_usfmresources.getResources ();
const std::vector <std::string> usfm_resources = database_usfmresources.getResources ();
names.insert (names.end (), usfm_resources.begin(), usfm_resources.end ());

sort (names.begin (), names.end ());
Expand All @@ -103,9 +103,9 @@ std::string compare_index (Webserver_Request& webserver_request)
document.print(ss, "", pugi::format_raw);
view.set_variable ("bibleblock", ss.str());

page += view.render ("compare", "index");
page.append (view.render ("compare", "index"));

page += assets_page::footer ();
page.append (assets_page::footer ());

return page;
}

0 comments on commit d9efd95

Please sign in to comment.