/* 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 "search.hpp" #include "fs-compat.hpp" #include "helpers.hpp" #include "log.hpp" #include "zip.hpp" #include #include #include // For compatibility with fmt 4. #include #include #include #include #include namespace epubgrep::search { using fmt::format; using std::string; std::vector search(const fs::path &filepath, const std::string_view regex, const settings &opts) { LOG(log::sev::info) << format(R"(Starting search in {0:s} using regex "{1:s}")", filepath, regex); boost::regex::flag_type flags{}; switch (opts.regex) { case options::regex_kind::basic: { flags = opts.grep_like ? boost::regex::grep : boost::regex::basic; break; } case options::regex_kind::extended: { flags = opts.grep_like ? boost::regex::egrep : boost::regex::extended; break; } case options::regex_kind::perl: { flags = boost::regex::perl; break; } } if (opts.ignore_case) { flags |= boost::regex::icase; } const boost::regex re(regex.data(), flags); std::vector matches; std::vector epub_filepaths{[&opts, &filepath] { if (!opts.raw) { return zip::list_spine(filepath); } return zip::list(filepath); }()}; for (const auto &entry : epub_filepaths) { DEBUGLOG << "Processing " << entry; auto document{zip::read_file(filepath, entry)}; if (!opts.raw) { cleanup_text(document); document = helpers::unescape_html(document); } string::const_iterator begin{document.begin()}; string::const_iterator end{document.end()}; boost::match_results match_result; string last_headline; string last_page; while (boost::regex_search(begin, end, match_result, re, boost::match_default)) { match match; // FIXME: Rename variable or struct. match.filepath_epub = filepath; match.filepath_inside = entry; match.text = match_result[0]; match.context = context(match_result, opts.context); const auto current_headline{headline(match_result.prefix().str())}; if (!current_headline.empty()) { last_headline = current_headline; } match.headline = last_headline; const auto current_page{page(match_result.prefix().str())}; if (!current_page.empty()) { last_page = current_page; } match.page = last_page; matches.emplace_back(match); begin = match_result[0].second; } } return matches; } void cleanup_text(string &text) { static const boost::regex re_header_start{"<[hH][1-6]"}; static const boost::regex re_header_end{"]+pagebreak[^>]+" "(title|aria-label)" "=\"([[:alnum:]]+)\""}; size_t pos{}; while ((pos = text.find('<', pos)) != string::npos) { auto endpos{text.find('>', pos) + 1}; // Mark headlines. We need them later on. string replacement; if (boost::regex_match(text.substr(pos, 3), re_header_start)) { replacement = ""; } else if (boost::regex_match(text.substr(pos, 4), re_header_end)) { replacement = ""; } else if (text.substr(pos, 6) == " match; using it_size_t = string::const_iterator::difference_type; string::const_iterator begin{text.begin() + static_cast(pos)}; string::const_iterator end{text.begin() + static_cast(endpos)}; if (boost::regex_search(begin, end, match, re_pagebreak)) { replacement = format("", match[2].str()); } } else if (text.substr(pos, 7) == "