/* This file is part of epubgrep. * Copyright © 2021 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ #include "output.hpp" #include "version.hpp" #include #include #include // For compatibility with fmt 4. #include #include #include #include #include namespace epubgrep::output { using boost::locale::translate; using fmt::format; using std::cout; void print_matches(const std::vector &matches, const options::options &opts, bool single_file) { if (!single_file && !opts.no_fn_fs) { if (!opts.nocolor) { cout << termcolor::yellow; } cout << format(translate(" In {0:s}: \n").str(), fs::relative(matches[0].filepath_epub)); if (!opts.nocolor) { cout << termcolor::reset; } } for (const auto &match : matches) { std::vector metadata; if (!opts.no_fn_epub) { metadata.emplace_back(match.filepath_inside); } if (!match.headline.empty()) { // if (!opts.nocolor && termcolor::_internal::is_colorized(cout)) { std::stringstream ss; ss << termcolor::colorize << termcolor::underline << match.headline << termcolor::reset << termcolor::italic; metadata.emplace_back(ss.str()); } else { metadata.emplace_back(match.headline); } } if (!match.page.empty()) { metadata.emplace_back("page " + match.page); } if (!metadata.empty()) { if (!opts.nocolor) { cout << termcolor::italic; } for (const auto &part : metadata) { cout << part; if (part != *(metadata.rbegin())) { cout << ", "; } } cout << ": "; if (!opts.nocolor) { cout << termcolor::reset; } } cout << match.context.first; if (!opts.nocolor) { cout << termcolor::bright_magenta; } cout << match.text; if (!opts.nocolor) { cout << termcolor::reset; } cout << match.context.second << '\n'; } } void json_all(const std::vector> &matches_all) { nlohmann::json json; json["generator"] = {{"epubgrep", std::string(version)}}; for (const auto &matches : matches_all) { for (const auto &match : matches) { json["matches"].push_back( {{"filepath_epub", match.filepath_epub.string()}, {"filepath_inside", match.filepath_inside}, {"match", match.text}, {"context", {match.context.first, match.context.second}}, {"headline", match.headline}, {"page", match.page}}); } } cout << json.dump() << '\n'; } void html_all(const std::vector> &matches_all, const options::options &opts) { std::uint64_t count{1}; cout << "\n"; // Translators: Replace “en” with your language code here. cout << format(R"()", translate("en").str()); cout << "epubgrep output" "" "\n\n"; for (const auto &matches : matches_all) { const auto identifier{ [&opts, count, &matches] { if (opts.no_fn_fs) { return format(translate("File {0:d}").str(), count); } return fs::relative(matches[0].filepath_epub).string(); }()}; // Start article, table and print table header. cout << format(R"(
)", count) << "\n \n" << format(R"( )", count, identifier) << '\n' << " \n"; if (!opts.no_fn_epub) { cout << format(R"( )", count, translate("File path (in EPUB file)")) << '\n'; } cout << format(R"( )", count, translate("Last headline")) << '\n' << format(R"( )", count, translate("Page number")) << '\n' << format(R"( )", count, translate("Match")) << "\n \n"; for (const auto &match : matches) { const auto lang{[&match] { if (!match.language.empty()) { return format(R"( lang="{0:s}")", match.language); } return std::string{}; }()}; cout << " \n"; if (!opts.no_fn_epub) { cout << format( R"( )", count, match.filepath_inside) << '\n'; } cout << format( R"( )", count, lang, match.headline) << '\n'; cout << format(R"( )", count, match.page) << '\n'; cout << format(R"( )", count, lang, match.context.first, match.text, match.context.second) << '\n'; cout << " \n"; } cout << "
{1:s}
{1:s}{1:s}{1:s}{1:s}
{1:s}{2:s}{1:s}{2:s})" R"({3:s}{4:s}
\n
\n\n"; ++count; } cout << "\n"; } } // namespace epubgrep::output