2 * @brief Extract text and metadata using libe-book.
4 /* Copyright (C) 2019 Bruno Baruffaldi
5 * Copyright (C) 2022,2023 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
24 #include "stringutils.h"
28 #include <librevenge-generators/librevenge-generators.h>
29 #include <librevenge-stream/librevenge-stream.h>
30 #include <libe-book/libe-book.h>
32 #define HANDLE_FIELD(START, END, FIELD, OUT...) \
33 handle_field((START), (END), (FIELD), (CONST_STRLEN(FIELD)), OUT)
35 using libebook::EBOOKDocument
;
36 using namespace librevenge
;
39 // Handle a field for which we only take a single value - we avoid copying in
42 handle_field(const char* start
,
49 if (size_t(end
- start
) > len
&& memcmp(start
, field
, len
) == 0) {
51 while (start
!= end
&& isspace(*start
)) start
++;
52 if (start
!= end
&& (end
[-1] != '\r' || --end
!= start
)) {
54 out_len
= end
- start
;
59 // Handle a field for which we process multiple instances. We just send each
60 // occurrence as we see it.
62 handle_field(const char* start
,
68 if (size_t(end
- start
) > len
&& memcmp(start
, field
, len
) == 0) {
70 while (start
!= end
&& isspace(*start
)) start
++;
71 if (start
!= end
&& (end
[-1] != '\r' || --end
!= start
)) {
72 send_field(code
, start
, end
- start
);
78 parse_metadata(const char* data
, size_t len
)
81 size_t author_len
= 0;
84 const char* end
= p
+ len
;
87 const char* start
= p
;
88 p
= static_cast<const char*>(memchr(p
, '\n', end
- start
));
94 if ((end
- start
) > 5 && memcmp(start
, "meta:", 5) == 0) {
98 // Use dc:creator in preference to meta:initial-creator.
100 HANDLE_FIELD(start
, eol
, "initial-creator",
105 HANDLE_FIELD(start
, eol
, "keyword", FIELD_KEYWORDS
);
109 } else if ((end
- start
) > 3 && memcmp(start
, "dc:", 3) == 0) {
113 // Use dc:creator in preference to meta:initial-creator.
114 HANDLE_FIELD(start
, eol
, "creator", author
, author_len
);
118 HANDLE_FIELD(start
, eol
, "subject", FIELD_KEYWORDS
);
122 HANDLE_FIELD(start
, eol
, "title", FIELD_TITLE
);
126 } else if ((end
- start
) > 8 && memcmp(start
, "dcterms:", 8) == 0) {
128 HANDLE_FIELD(start
, eol
, "available", FIELD_KEYWORDS
);
133 send_field(FIELD_AUTHOR
, author
, author_len
);
144 extract(const string
& filename
, const string
&)
146 unique_ptr
<RVNGInputStream
> input
;
147 RVNGString dump
, metadata
;
148 const char* file
= filename
.c_str();
150 if (RVNGDirectoryStream::isDirectory(file
))
151 input
.reset(new RVNGDirectoryStream(file
));
153 input
.reset(new RVNGFileStream(file
));
155 EBOOKDocument::Type type
= EBOOKDocument::TYPE_UNKNOWN
;
156 auto confidence
= EBOOKDocument::isSupported(input
.get(), &type
);
158 if (confidence
!= EBOOKDocument::CONFIDENCE_EXCELLENT
&&
159 confidence
!= EBOOKDocument::CONFIDENCE_WEAK
) {
160 send_field(FIELD_ERROR
, "Format not supported");
165 RVNGTextTextGenerator
metadata_gen(metadata
, true);
166 if (EBOOKDocument::parse(input
.get(), &metadata_gen
, type
) !=
167 EBOOKDocument::RESULT_OK
) {
168 send_field(FIELD_ERROR
, "Failed to extract metadata");
171 parse_metadata(metadata
.cstr(), metadata
.size());
173 // Extract body text.
174 RVNGTextTextGenerator
content(dump
, false);
175 if (EBOOKDocument::parse(input
.get(), &content
, type
) !=
176 EBOOKDocument::RESULT_OK
) {
177 send_field(FIELD_ERROR
, "Failed to extract text");
180 send_field(FIELD_BODY
, dump
.cstr(), dump
.size());