Merge pull request 'merge with upstream & fix gd-handwritten' (#3) from xieamoe/gd-tools-plus:main into main

Reviewed-on: https://codeberg.org/hashirama/gd-tools/pulls/3
This commit is contained in:
千住柱間 2024-02-21 03:47:41 +00:00
commit a374b08400
6 changed files with 68 additions and 46 deletions

View file

@ -25,7 +25,7 @@ if( ${GUIX} )
include(tests/Catch.cmake)
target_link_libraries(tests PRIVATE cpr marisa rdricpp Catch2Main Catch2)
else()
target_link_libraries(tests PRIVATE cpr::cpr fmt::fmt nlohmann_json::nlohmann_json marisa rdricpp Catch2::Catch2WithMain)
target_link_libraries(tests PRIVATE cpr::cpr nlohmann_json::nlohmann_json marisa rdricpp Catch2::Catch2WithMain)
endif()

View file

@ -136,8 +136,8 @@
(method git-fetch)
(uri (git-reference
(url "https://codeberg.org/xieamoe/gd-tools-plus.git")
(commit "9b66d3caf8")))
(sha256 (base32 "135flpfkp2sjgah92546vpznxz712c5prvbdj93637y9pyrw9rci"))))
(commit "92a21e57e7")))
(sha256 (base32 "0zwb20s8kzdnzypgrx3cl0z4cicl20icb6pmmxbq8vh5dxpf71fd"))))
(build-system cmake-build-system)
(arguments
@ -178,5 +178,5 @@
(license
(list license:gpl3+
license:zlib))))
gd-tools
gd-tools

View file

@ -98,6 +98,9 @@ static constexpr std::string_view css_style = R"EOF(<style>
.gd-ankisearch-table a:hover {
filter: hue-rotate(-20deg) brightness(120%);
}
.gd-tag-link:not(:last-of-type)::after {
content: ", ";
}
</style>
)EOF";
@ -110,6 +113,7 @@ struct card_info
int64_t type;
std::string deck_name;
NameToValMap fields;
uint64_t nid;
};
auto split_anki_field_names(std::string_view const show_fields) -> std::vector<std::string>
@ -189,7 +193,7 @@ auto make_ankiconnect_request(std::string_view const request_str) -> cpr::Respon
);
}
auto make_info_request_str(std::vector<uint64_t> const& cids)
auto make_info_request_str(std::vector<uint64_t> const& cids) -> std::string
{
auto request = json::parse(R"EOF({
"action": "cardsInfo",
@ -232,6 +236,33 @@ auto fetch_media_dir_path() -> std::string
return obj["result"];
}
auto make_get_note_tags_request_str(uint64_t const nid) -> std::string
{
auto request = json::parse(R"EOF({
"action": "getNoteTags",
"version": 6,
"params": {
"note": 1483959289817
}
})EOF");
request["params"]["note"] = nid;
return request.dump();
}
auto get_note_tags(uint64_t const nid) -> std::string
{
auto const request_str = make_get_note_tags_request_str(nid);
cpr::Response const r = make_ankiconnect_request(request_str);
raise_if(r.status_code != cpr::status::HTTP_OK, "Couldn't connect to Anki.");
auto const obj = json::parse(r.text);
raise_if(not obj["error"].is_null(), "Error getting data from AnkiConnect.");
std::string html;
for (std::string const tag_name: obj["result"]) {
html += std::format(R"EOF(<a class="gd-tag-link" href="ankisearch:tag:{}">{}</a>)EOF", tag_name, tag_name);
}
return html;
}
void print_table_header(search_params const& params)
{
// Print the first row (header) that contains <th></th> tags, starting with Card ID.
@ -239,6 +270,7 @@ void print_table_header(search_params const& params)
ajt::print("<th>Card ID</th>");
ajt::print("<th>Deck name</th>");
for (auto const& field: params.show_fields) { ajt::print("<th>{}</th>", field); }
ajt::print("<th>Tags</th>");
ajt::print("</tr>\n");
}
@ -257,7 +289,8 @@ auto card_json_to_obj(nlohmann::json const& card_json) -> card_info
result.emplace(element.key(), element.value()["value"]);
}
return result;
}(), //
}(),
.nid = card_json["note"] //
};
}
@ -292,6 +325,7 @@ void print_cards_info(search_params const& params)
: "Not present")
);
}
ajt::print("<td>{}</td>\n", get_note_tags(card.nid));
ajt::print("</tr>\n");
}
ajt::print("</table>\n");

View file

@ -90,36 +90,3 @@ void stroke_order(std::span<std::string_view const> const args)
ajt::print("{}\n", ex.what());
}
}
//Later just use stroke_order to print it
void print_css_hw(stroke_order_params const& params)
{
static constexpr std::string_view css = R"EOF(
<style>
.gd_echo_{} {{
font-size: {};
font-family: "ArmedLemon";
}}
</style>
)EOF";
ajt::print(css, this_pid, params.font_size);
}
void print_with_hw(stroke_order_params const& params)
{
if (params.gd_word.length() <= params.max_len) {
ajt::print("<div class=\"gd_echo_{}\">{}</div>\n", this_pid, params.gd_word);
print_css_hw(params);
}
}
void handwritten(std::span<std::string_view const> const args)
{
try {
print_with_hw(fill_args<stroke_order_params>(args));
} catch (gd::help_requested const& ex) {
ajt::print(help_text);
} catch (gd::runtime_error const& ex) {
ajt::print("{}\n", ex.what());
}
}

View file

@ -85,7 +85,7 @@ auto take_action(std::span<std::string_view const> const args) -> void
case "gd-strokeorder"_h:
return stroke_order(rest);
case "gd-handwritten"_h:
return handwritten(rest);
return stroke_order(rest);
case "gd-massif"_h:
return massif(rest);
case "gd-images"_h:
@ -109,7 +109,7 @@ auto take_action(std::span<std::string_view const> const args) -> void
case "strokeorder"_h:
return stroke_order(rest);
case "handwritten"_h:
return handwritten(rest);
return stroke_order(rest);
case "massif"_h:
return massif(rest);
case "images"_h:
@ -144,12 +144,19 @@ auto main(/*int const argc, char const* const* const argv*/) -> int
args_utf8.push_back(test_str);
}
std::string_view font = "--font-family";
std::string_view font_value = "armedlemon";
if(std::string(args_utf8[1]) == "handwritten" || base_name(args_utf8[0]) == "gd-handwritten" && argc > 2){
args_utf8.push_back(font);
args_utf8.push_back(font_value);
}
for (const std::string& arg : args_utf8)
args.push_back(arg);
take_action(args);
return 0;
}
#endif //ifndef WIN32
@ -157,7 +164,21 @@ auto main(/*int const argc, char const* const* const argv*/) -> int
#ifndef WIN32 //General Main
auto main(int const argc, char const* const* const argv) -> int
{
take_action(std::vector<std::string_view>{ argv, std::next(argv, argc) });
return 0;
std::vector<std::string_view> args{};
for(int i{0}; i < argc; i++)
args.push_back(argv[i]);
//Automatically change font for handwritten
std::string_view font = "--font-family";
std::string_view font_value = "armedlemon";
if(std::string(argv[1]) == "handwritten" || base_name(argv[0]) == "gd-handwritten" && argc > 2){
args.push_back(font);
args.push_back(font_value);
}
take_action(args);
//Original code
//take_action(std::vector<std::string_view>{ argv, std::next(argv, argc) });
}
#endif //General Main

View file

@ -4,7 +4,7 @@ add_executable(tests tests_main.cpp kana_conv.h util.h)
target_compile_features(tests PRIVATE cxx_std_23)
# Should be linked to the rdricpp library, as well as the Catch2 testing library
#target_link_libraries(tests PRIVATE cpr::cpr fmt::fmt nlohmann_json::nlohmann_json marisa rdricpp Catch2::Catch2WithMain)
#target_link_libraries(tests PRIVATE cpr::cpr nlohmann_json::nlohmann_json marisa rdricpp Catch2::Catch2WithMain)
target_link_libraries(tests PRIVATE cpr::cpr marisa rdricpp Catch2Main)