finish goldendict integration

This commit is contained in:
千住柱間 2024-05-17 21:34:09 -04:00
parent ac8af23fba
commit b6b47f6ba3
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -9,6 +9,7 @@
#include <unistd.h>
#include <filesystem>
#include <map>
#include <set>
// 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<std::string>& 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<std::string>& substrings,
const std::map<std::string, std::set<std::string>>& alternatives_map,
std::string& sentence
) {
std::string output_html;
output_html += "<!DOCTYPE html><html><head><style>";
output_html += ".hakurei { font-size: 2rem; margin-bottom: 0.05em; margin-top: -0.2em; color: #1268c3; font-weight: normal; }";
output_html += ".hakurei a { display: inline-block; font-weight: normal; color: royalblue; text-decoration: none; border-bottom: dashed max(1px, calc(1em / 16)) currentColor; }";
output_html += ".hakurei a.hakurei-headword { background-color: #ddeeff; border-radius: 0.2rem; font-weight: 500; }";
output_html += ".hakurei > ul { --size: 1rem; font-size: var(--size); padding-inline-start: var(--size); margin-block: 2px; }";
output_html += ".hakurei .alternatives { --size: 1rem; display: grid; font-size: var(--size); gap: calc( var(--size) / 4); max-width: 100%; margin: 0 auto; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); align-content: start; justify-content: space-around; text-align: left; padding: 5px 0px; }";
output_html += ".hakurei .alternatives > ul { list-style-type: none; margin: 0; padding: calc( var(--size) / 4); background-color: hsl(0 0% 50% / 0.05); box-shadow: 0 0 4px hsl(0 0% 0% / 0.1); border-radius: 0.2rem; }";
output_html += ".hakurei .alternatives > ul > li { margin-right: 1rem; }";
output_html += ".container { display: flex; flex-wrap: wrap; gap: 10px; }"; // Adjusted container class
output_html += ".segment { display: inline-block; }"; // Adjusted segment class
output_html += "</style></head><body><div class=\"hakurei\">";
std::string sentence_copy = sentence;
output_html += "<div class=\"container\">"; // Open container div
while (!sentence_copy.empty()) {
bool found = false;
for (const auto& substring : substrings) {
if (sentence_copy.rfind(substring, 0) == 0) {
output_html += "<div class=\"segment\"><a class=\"hakurei-headword\" href=\"bword:" + substring + "\">" + substring + "</a>";
// 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 += "<div class=\"alternatives\"><ul>";
for (const auto& alt : alt_itr->second) {
output_html += "<li><a href=\"bword:" + alt + "\">" + alt + "</a></li>";
}
output_html += "</ul></div>";
}
output_html += "</div>"; // 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 += "<div class=\"segment\">" + removed_char + "</div>";
sentence_copy = new_sentence_copy;
}
}
output_html += "</div>"; // Close container div
output_html += "</div></body></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<std::string> substrings;
std::vector<Entry> results;
std::map<std::string, std::vector<std::string>> derived_map;
std::map<std::string, std::set<std::string>> 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 << "<!DOCTYPE html><html><head><style>.hakurei { font-size: 2rem; margin-bottom: 0.05em; margin-top: -0.2em; color: #1268c3; font-weight: normal; } .hakurei a { display: inline-block; font-weight: normal; color: royalblue; text-decoration: none; border-bottom: dashed max(1px, calc(1em / 16)) currentColor; } .hakurei a.hakurei-headword { background-color: #ddeeff; border-radius: 0.2rem; font-weight: 500; } .hakurei > ul { --size: 1rem; font-size: var(--size); padding-inline-start: var(--size); margin-block: 2px; } .hakurei .alternatives { --size: 1rem; display: grid; font-size: var(--size); gap: calc( var(--size) / 4); max-width: 100%; margin: 0 auto; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); align-content: start; justify-content: space-around; text-align: left; padding: 5px 0px; } .hakurei .alternatives > ul { list-style-type: none; margin: 0; padding: calc( var(--size) / 4); background-color: hsl(150deg 30% 60% / 10%); } </style></head><body><div class=\"hakurei\">";
for (const auto& [key, values] : derived_map) {
std::cout << "<a href=\"bword:" << key << "\">" << key << "</a>";
if (!values.empty()) {
std::cout << "<ul>";
for (const auto& value : values) {
std::cout << "<li><a href=\"bword:" << value << "\">" << value << "</a></li>";
}
std::cout << "</ul>";
}
}
std::cout << "</div></body></html>";
wrap_html_output(substrings, alternatives_map, sentence);
}
return 0;