diff --git a/src/main.cc b/src/main.cc index 677059a..87cfdf5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -9,6 +9,7 @@ #include #include #include +#include // Struct to hold the view and id of each entry struct Entry { @@ -96,6 +97,72 @@ inline std::filesystem::path find_dic_file() { throw std::runtime_error("Couldn't find the word list."); } +inline void print_debug_info(const std::vector& substrings, const std::string& raw_output) { + std::cout << "Stored raw output:" << std::endl; + for (const auto& str : substrings) { + std::cout << str << std::endl; + } + std::cout << "Raw output:" << std::endl; + std::cout << raw_output << std::endl; +} + +inline void wrap_html_output( + const std::vector& substrings, + const std::map>& alternatives_map, + std::string& sentence +) { + std::string output_html; + output_html += "
"; + + std::string sentence_copy = sentence; + + output_html += "
"; // Open container div + + while (!sentence_copy.empty()) { + bool found = false; + for (const auto& substring : substrings) { + if (sentence_copy.rfind(substring, 0) == 0) { + output_html += "
" + substring + ""; + + // Adding alternatives for the current substring + auto alt_itr = alternatives_map.find(sentence_copy); + if (alt_itr != alternatives_map.end() && !alt_itr->second.empty()) { + output_html += "
    "; + for (const auto& alt : alt_itr->second) { + output_html += "
  • " + alt + "
  • "; + } + output_html += "
"; + } + + output_html += "
"; // Close segment div + sentence_copy = sentence_copy.substr(substring.size()); + found = true; + break; + } + } + if (!found) { + const auto [removed_char, new_sentence_copy] = remove_one_utf8_char(sentence_copy); + output_html += "
" + removed_char + "
"; + sentence_copy = new_sentence_copy; + } + } + + output_html += "
"; // Close container div + output_html += "
"; + std::cout << output_html << std::endl; +} + + int main(const int argc, char* argv[]) { bool debug_mode = false; bool goldendict_mode = false; @@ -131,6 +198,7 @@ int main(const int argc, char* argv[]) { std::vector substrings; std::vector results; std::map> derived_map; + std::map> alternatives_map; while (!search_string.empty()) { auto itr = trie.make_prefix_iterator(search_string); @@ -145,6 +213,7 @@ int main(const int argc, char* argv[]) { const std::string substring(entry.decoded_view); substrings.push_back(substring); derived_map[substring].push_back(search_string); + alternatives_map[search_string].insert(substring); if (!goldendict_mode) { std::cout << substring << std::endl; } @@ -174,26 +243,12 @@ int main(const int argc, char* argv[]) { // Print debug information if in debug mode if (debug_mode) { - std::cout << "Stored raw output:" << std::endl; - for (const auto& str : substrings) { - std::cout << str << std::endl; - } + print_debug_info(substrings, raw_output); } // Wrap the output in HTML format for GoldenDict if in GoldenDict mode if (goldendict_mode) { - std::cout << "
"; - for (const auto& [key, values] : derived_map) { - std::cout << "" << key << ""; - if (!values.empty()) { - std::cout << "
    "; - for (const auto& value : values) { - std::cout << "
  • " << value << "
  • "; - } - std::cout << "
"; - } - } - std::cout << "
"; + wrap_html_output(substrings, alternatives_map, sentence); } return 0;