2021-05-24 07:52:36 +02:00
|
|
|
/* This file is part of epubgrep.
|
|
|
|
* Copyright © 2021 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "search.hpp"
|
|
|
|
|
|
|
|
#include "fs-compat.hpp"
|
2021-05-24 15:35:49 +02:00
|
|
|
#include "zip.hpp"
|
2021-05-24 07:52:36 +02:00
|
|
|
|
2021-05-24 15:35:49 +02:00
|
|
|
#include <boost/regex.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2021-05-30 13:31:59 +02:00
|
|
|
#include <array>
|
2021-05-24 15:35:49 +02:00
|
|
|
#include <string>
|
2021-05-24 07:52:36 +02:00
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace epubgrep::search
|
|
|
|
{
|
|
|
|
|
2021-05-26 18:02:27 +02:00
|
|
|
using std::string;
|
|
|
|
|
2021-05-28 19:07:27 +02:00
|
|
|
std::vector<match> search(const fs::path &filepath,
|
|
|
|
const std::string_view regex, const settings &opts)
|
2021-05-24 07:52:36 +02:00
|
|
|
{
|
2021-05-24 15:35:49 +02:00
|
|
|
boost::regex::flag_type flags{};
|
|
|
|
|
|
|
|
switch (opts.regex)
|
|
|
|
{
|
2021-05-27 17:20:00 +02:00
|
|
|
case options::regex_kind::basic:
|
2021-05-24 15:35:49 +02:00
|
|
|
{
|
|
|
|
flags = opts.grep_like ? boost::regex::grep : boost::regex::basic;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-27 17:20:00 +02:00
|
|
|
case options::regex_kind::extended:
|
2021-05-24 15:35:49 +02:00
|
|
|
{
|
|
|
|
flags = opts.grep_like ? boost::regex::egrep : boost::regex::extended;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-27 17:20:00 +02:00
|
|
|
case options::regex_kind::perl:
|
2021-05-24 15:35:49 +02:00
|
|
|
{
|
|
|
|
flags = boost::regex::perl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.ignore_case)
|
|
|
|
{
|
|
|
|
flags |= boost::regex::icase;
|
|
|
|
}
|
|
|
|
|
2021-05-27 09:46:59 +02:00
|
|
|
const boost::regex re(regex.data(), flags);
|
2021-05-24 15:35:49 +02:00
|
|
|
std::vector<match> matches;
|
2021-05-29 15:50:03 +02:00
|
|
|
std::vector<string> epub_filepaths{[&opts, &filepath]
|
|
|
|
{
|
|
|
|
if (!opts.raw)
|
|
|
|
{
|
|
|
|
return zip::list_spine(filepath);
|
|
|
|
}
|
|
|
|
return zip::list(filepath);
|
|
|
|
}()};
|
|
|
|
|
|
|
|
for (const auto &entry : epub_filepaths)
|
2021-05-24 15:35:49 +02:00
|
|
|
{
|
|
|
|
auto document{zip::read_file(filepath, entry)};
|
2021-05-24 16:01:41 +02:00
|
|
|
if (!opts.raw)
|
|
|
|
{
|
|
|
|
cleanup_text(document);
|
|
|
|
}
|
|
|
|
|
2021-05-26 18:02:27 +02:00
|
|
|
string::const_iterator begin{document.begin()};
|
|
|
|
string::const_iterator end{document.end()};
|
|
|
|
boost::match_results<string::const_iterator> match_result;
|
|
|
|
string last_headline;
|
|
|
|
string last_page;
|
2021-05-24 17:18:10 +02:00
|
|
|
|
2021-05-24 15:35:49 +02:00
|
|
|
while (boost::regex_search(begin, end, match_result, re,
|
|
|
|
boost::match_default))
|
|
|
|
{
|
|
|
|
match match; // FIXME: Rename variable or struct.
|
2021-05-27 14:46:23 +02:00
|
|
|
match.epub_filepath = filepath;
|
2021-05-24 15:35:49 +02:00
|
|
|
match.filepath = entry;
|
|
|
|
match.text = match_result[0];
|
|
|
|
match.context = context(match_result, opts.context);
|
2021-05-24 17:18:10 +02:00
|
|
|
const auto current_headline{headline(match_result.prefix().str())};
|
|
|
|
if (!current_headline.empty())
|
|
|
|
{
|
|
|
|
last_headline = current_headline;
|
|
|
|
}
|
|
|
|
match.headline = last_headline;
|
2021-05-24 18:56:43 +02:00
|
|
|
const auto current_page{page(match_result.prefix().str())};
|
|
|
|
if (!current_page.empty())
|
|
|
|
{
|
|
|
|
last_page = current_page;
|
|
|
|
}
|
|
|
|
match.page = last_page;
|
2021-05-24 15:35:49 +02:00
|
|
|
|
|
|
|
matches.emplace_back(match);
|
|
|
|
begin = match_result[0].second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
2021-05-26 18:02:27 +02:00
|
|
|
void cleanup_text(string &text)
|
2021-05-24 16:23:07 +02:00
|
|
|
{
|
2021-05-28 19:07:27 +02:00
|
|
|
static const boost::regex re_header_start{"<[hH][1-6]"};
|
2021-05-29 23:00:16 +02:00
|
|
|
static const boost::regex re_header_end{"</[hH][1-6]"};
|
2021-05-28 19:07:27 +02:00
|
|
|
static const boost::regex re_pagebreak{".+pagebreak.+(title|aria-label)"
|
|
|
|
"=\"([[:alnum:]]+)\".*"};
|
|
|
|
|
2021-05-24 19:58:59 +02:00
|
|
|
size_t pos{};
|
2021-05-26 18:02:27 +02:00
|
|
|
while ((pos = text.find('<', pos)) != string::npos)
|
2021-05-24 16:23:07 +02:00
|
|
|
{
|
2021-05-24 18:08:40 +02:00
|
|
|
// Mark headlines. We need them later on.
|
2021-05-26 18:02:27 +02:00
|
|
|
string replacement;
|
2021-05-28 19:07:27 +02:00
|
|
|
if (boost::regex_match(text.substr(pos, 3), re_header_start))
|
2021-05-24 16:37:30 +02:00
|
|
|
{
|
2021-05-24 18:08:40 +02:00
|
|
|
replacement = "<H>";
|
2021-05-24 16:37:30 +02:00
|
|
|
}
|
2021-05-29 23:00:16 +02:00
|
|
|
else if (boost::regex_match(text.substr(pos, 4), re_header_end))
|
2021-05-24 18:08:40 +02:00
|
|
|
{
|
|
|
|
replacement = "</H>";
|
|
|
|
}
|
2021-05-29 18:49:35 +02:00
|
|
|
else if (text.substr(pos, 6) == "<span ")
|
2021-05-24 18:56:43 +02:00
|
|
|
{
|
|
|
|
auto endpos{text.find('>')};
|
|
|
|
boost::match_results<const char *> match;
|
|
|
|
if (boost::regex_search(text.substr(pos, endpos).data(), match,
|
|
|
|
re_pagebreak))
|
|
|
|
{
|
|
|
|
replacement = "<PAGE " + match[2] + ">";
|
|
|
|
}
|
|
|
|
}
|
2021-05-29 18:49:35 +02:00
|
|
|
else if (text.substr(pos, 7) == "<style "
|
|
|
|
|| text.substr(pos, 8) == "<script ")
|
|
|
|
{
|
|
|
|
pos = text.find('>', pos) + 1;
|
|
|
|
}
|
|
|
|
|
2021-05-24 18:08:40 +02:00
|
|
|
text.replace(pos, text.find('>', pos) + 1 - pos, replacement);
|
|
|
|
pos += replacement.length();
|
2021-05-24 16:23:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 19:58:59 +02:00
|
|
|
pos = 0;
|
2021-05-26 18:02:27 +02:00
|
|
|
while ((pos = text.find('\r', pos)) != string::npos)
|
2021-05-24 16:23:07 +02:00
|
|
|
{
|
2021-05-24 18:08:40 +02:00
|
|
|
text.erase(pos, 1);
|
2021-05-24 16:23:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 19:58:59 +02:00
|
|
|
pos = 0;
|
2021-05-26 18:02:27 +02:00
|
|
|
while ((pos = text.find('\n', pos)) != string::npos)
|
2021-05-24 16:23:07 +02:00
|
|
|
{
|
|
|
|
text.replace(pos, 1, " ");
|
|
|
|
}
|
|
|
|
|
2021-05-24 19:58:59 +02:00
|
|
|
pos = 0;
|
2021-05-26 18:02:27 +02:00
|
|
|
while ((pos = text.find(" ", pos)) != string::npos)
|
2021-05-24 16:23:07 +02:00
|
|
|
{
|
|
|
|
text.replace(pos, 2, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 18:02:27 +02:00
|
|
|
match_context context(const boost::match_results<string::const_iterator> &match,
|
|
|
|
std::uint64_t words)
|
2021-05-24 15:35:49 +02:00
|
|
|
{
|
2021-05-24 19:57:15 +02:00
|
|
|
if (words == 0)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-05-24 15:35:49 +02:00
|
|
|
++words;
|
|
|
|
|
2021-05-30 13:31:59 +02:00
|
|
|
const auto &rbegin_before{std::reverse_iterator(match.prefix().end())};
|
|
|
|
const auto &rend_before{std::reverse_iterator(match.prefix().begin())};
|
|
|
|
|
|
|
|
const auto &begin_after{match.suffix().begin()};
|
|
|
|
const auto &end_after{match.suffix().end()};
|
|
|
|
|
|
|
|
auto pos_before{rbegin_before};
|
|
|
|
auto pos_after{begin_after};
|
|
|
|
|
|
|
|
const std::array<char, 4> whitespace{' ', '\n', '\r', '\t'};
|
|
|
|
auto is_whitespace{
|
|
|
|
[&whitespace](char check)
|
|
|
|
{
|
|
|
|
return std::any_of(whitespace.begin(), whitespace.end(),
|
|
|
|
[&check](const char ws) { return check == ws; });
|
|
|
|
}};
|
|
|
|
|
2021-05-24 15:35:49 +02:00
|
|
|
while (words != 0)
|
|
|
|
{
|
2021-05-30 13:31:59 +02:00
|
|
|
if (pos_before != rend_before)
|
2021-05-24 15:35:49 +02:00
|
|
|
{
|
2021-05-30 13:31:59 +02:00
|
|
|
pos_before = std::find_first_of(pos_before, rend_before,
|
|
|
|
whitespace.begin(),
|
|
|
|
whitespace.end());
|
|
|
|
if (pos_before != rend_before)
|
2021-05-24 19:57:15 +02:00
|
|
|
{
|
2021-05-30 13:31:59 +02:00
|
|
|
while (is_whitespace(*pos_before))
|
|
|
|
{
|
|
|
|
++pos_before;
|
|
|
|
}
|
2021-05-24 19:57:15 +02:00
|
|
|
}
|
2021-05-24 15:35:49 +02:00
|
|
|
}
|
|
|
|
|
2021-05-30 13:31:59 +02:00
|
|
|
if (pos_after != end_after)
|
2021-05-24 15:35:49 +02:00
|
|
|
{
|
2021-05-30 13:31:59 +02:00
|
|
|
pos_after = std::find_first_of(pos_after, end_after,
|
|
|
|
whitespace.begin(),
|
|
|
|
whitespace.end());
|
|
|
|
if (pos_after != end_after)
|
2021-05-24 19:57:15 +02:00
|
|
|
{
|
2021-05-30 13:31:59 +02:00
|
|
|
while (is_whitespace(*pos_after))
|
|
|
|
{
|
|
|
|
++pos_after;
|
|
|
|
}
|
2021-05-24 19:57:15 +02:00
|
|
|
}
|
2021-05-24 15:35:49 +02:00
|
|
|
}
|
|
|
|
words -= 1;
|
|
|
|
}
|
|
|
|
|
2021-05-30 13:31:59 +02:00
|
|
|
const std::string prefix_reversed(rbegin_before, pos_before);
|
|
|
|
string prefix(prefix_reversed.rbegin(), prefix_reversed.rend());
|
|
|
|
std::string suffix(begin_after, pos_after);
|
|
|
|
while (is_whitespace(*prefix.begin()))
|
2021-05-24 19:57:15 +02:00
|
|
|
{
|
2021-05-30 13:31:59 +02:00
|
|
|
prefix.erase(0, 1);
|
2021-05-24 19:57:15 +02:00
|
|
|
}
|
2021-05-30 13:31:59 +02:00
|
|
|
while (is_whitespace(*suffix.rbegin()))
|
2021-05-24 19:57:15 +02:00
|
|
|
{
|
2021-05-30 13:31:59 +02:00
|
|
|
suffix.erase(suffix.size() - 1);
|
2021-05-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-30 13:31:59 +02:00
|
|
|
return {prefix, suffix};
|
2021-05-24 07:52:36 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 18:02:27 +02:00
|
|
|
string headline(const std::string_view prefix)
|
2021-05-24 17:18:10 +02:00
|
|
|
{
|
|
|
|
size_t pos{prefix.length()};
|
2021-05-24 18:08:40 +02:00
|
|
|
while ((pos = prefix.rfind("<H>", pos)) != std::string_view::npos)
|
2021-05-24 17:18:10 +02:00
|
|
|
{
|
2021-05-24 18:08:40 +02:00
|
|
|
pos += 3;
|
2021-05-26 18:02:27 +02:00
|
|
|
return string{prefix.substr(pos, prefix.find('<', pos) - pos)};
|
2021-05-24 17:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-05-26 18:02:27 +02:00
|
|
|
string page(const std::string_view prefix)
|
2021-05-24 18:56:43 +02:00
|
|
|
{
|
|
|
|
size_t pos{prefix.length()};
|
|
|
|
while ((pos = prefix.rfind("<PAGE ", pos)) != std::string_view::npos)
|
|
|
|
{
|
|
|
|
pos += 6;
|
2021-05-26 18:02:27 +02:00
|
|
|
return string{prefix.substr(pos, prefix.find('>', pos) - pos)};
|
2021-05-24 18:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-05-24 07:52:36 +02:00
|
|
|
} // namespace epubgrep::search
|