gui
[lbook_fbreader.git] / fbreader / src / formats / html / HtmlPlugin.cpp
blobebe10c4c383164025fffb5676ba208772284d7ee
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <ZLStringUtil.h>
21 #include <ZLFile.h>
22 #include <ZLInputStream.h>
24 #include "HtmlPlugin.h"
25 #include "HtmlDescriptionReader.h"
26 #include "HtmlBookReader.h"
27 #include "../txt/PlainTextFormat.h"
28 #include "../../description/BookDescription.h"
29 #include "../util/MiscUtil.h"
31 bool HtmlPlugin::acceptsFile(const ZLFile &file) const {
32 const std::string &extension = file.extension();
33 return ZLStringUtil::stringEndsWith(extension, "html") || (extension == "htm");
36 bool HtmlPlugin::readDescription(const std::string &path, BookDescription &description) const {
37 ZLFile file(path);
38 shared_ptr<ZLInputStream> stream = file.inputStream();
39 if (stream.isNull()) {
40 return false;
43 detectEncodingAndLanguage(description, *stream);
44 if (description.encoding().empty()) {
45 return false;
47 HtmlDescriptionReader(description).readDocument(*stream);
49 return true;
52 bool HtmlPlugin::readModel(const BookDescription &description, BookModel &model) const {
53 std::string fileName = description.fileName();
54 shared_ptr<ZLInputStream> stream = ZLFile(fileName).inputStream();
55 if (stream.isNull()) {
56 return false;
59 PlainTextFormat format(fileName);
60 if (!format.initialized()) {
61 PlainTextFormatDetector detector;
62 detector.detect(*stream, format);
65 std::string directoryPrefix = MiscUtil::htmlDirectoryPrefix(fileName);
66 HtmlBookReader reader(directoryPrefix, model, format, description.encoding());
67 reader.setFileName(MiscUtil::htmlFileName(fileName));
68 reader.readDocument(*stream);
70 return true;
73 const std::string &HtmlPlugin::iconName() const {
74 static const std::string ICON_NAME = "html";
75 return ICON_NAME;
78 FormatInfoPage *HtmlPlugin::createInfoPage(ZLOptionsDialog &dialog, const std::string &fileName) {
79 return new PlainTextInfoPage(dialog, fileName, ZLResourceKey("<PRE>"), false);