diff --git a/man/epubgrep.1.adoc b/man/epubgrep.1.adoc index 1c5c29e..7d31fbf 100644 --- a/man/epubgrep.1.adoc +++ b/man/epubgrep.1.adoc @@ -2,7 +2,7 @@ :doctype: manpage :Author: tastytea :Email: tastytea@tastytea.de -:Date: 2021-06-07 +:Date: 2021-06-08 :Revision: 0.0.0 :man source: epubgrep :man manual: General Commands Manual @@ -88,6 +88,10 @@ program. There will be an object named `generator` with the property `epubgrep`. The value is the version of the program, as string. The matches are in an array named `matches`. I will try not to break the API. 😊 +*--html*:: +Output HTML instead of plain text. HTML will only be output at the end of the +program. + == USAGE [source,shellsession] diff --git a/src/main.cpp b/src/main.cpp index b3f1680..5d3014c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -223,7 +223,7 @@ int main(int argc, char *argv[]) std::async(std::launch::async, search_file, filepath)); DEBUGLOG << "Launched new thread"; - if (!matches_all.empty() && !opts.json) + if (!matches_all.empty() && !opts.json && !opts.html) { output::print_matches(matches_all[0], opts, input_files.size() == 1); @@ -238,6 +238,10 @@ int main(int argc, char *argv[]) { output::json_all(matches_all); } + else if (opts.html) + { + output::html_all(matches_all, opts); + } else { for (const auto &matches : matches_all) diff --git a/src/options.cpp b/src/options.cpp index 077df45..f15e551 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -95,6 +95,8 @@ options parse_options(int argc, char *argv[]) translate("Enable debug output.") .str().data()) ("json", translate("Output JSON instead of plain text.") .str().data()) + ("html", + translate("Output HTML instead of plain text.") .str().data()) ; po::options_description options_hidden("Hidden options"); @@ -235,6 +237,7 @@ options parse_again(const po::variables_map &vm) opts.ignore_archive_errors = vm.count("ignore-archive-errors") > 0; opts.debug = vm.count("debug") > 0; opts.json = vm.count("json") > 0; + opts.html = vm.count("html") > 0; if (vm.count("regexp") > 0) { diff --git a/src/options.hpp b/src/options.hpp index a555401..8929cbf 100644 --- a/src/options.hpp +++ b/src/options.hpp @@ -58,6 +58,7 @@ struct options bool ignore_archive_errors{false}; bool debug{false}; bool json{false}; + bool html{false}; //! For the debug output. friend std::ostream &operator<<(std::ostream &out, const options &opts); diff --git a/src/output.cpp b/src/output.cpp index 105578b..19bd72f 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -131,4 +132,79 @@ void json_all(const std::vector> &matches_all) cout << json.dump() << '\n'; } +void html_all(const std::vector> &matches_all, + const options::options &opts) +{ + std::uint64_t count{1}; + + cout << "\n" + << "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) + { + cout << " \n"; + if (!opts.no_fn_epub) + { + cout << format( + R"( )", count, + match.filepath_inside) + << '\n'; + } + cout << format(R"( )", + count, match.headline) + << '\n'; + cout << format(R"( )", + count, match.page) + << '\n'; + cout << format(R"( )", + count, match.context.first, match.text, + match.context.second) + << '\n'; + cout << " \n"; + } + cout << "
{1:s}
{1:s}{1:s}{1:s}{1:s}
{1:s}{1:s}{1:s}{1:s})" + R"({2:s}{3:s}
\n
\n\n"; + ++count; + } + + cout << "\n"; +} + } // namespace epubgrep::output diff --git a/src/output.hpp b/src/output.hpp index e578103..4161d68 100644 --- a/src/output.hpp +++ b/src/output.hpp @@ -32,6 +32,10 @@ void print_matches(const std::vector &matches, //! Print all matches as JSON. void json_all(const std::vector> &matches_all); +//! Print all matches as HTML. +void html_all(const std::vector> &matches_all, + const options::options &opts); + } // namespace epubgrep::output #endif // EPUBGREP_OUTPUT_HPP