Parent: [b28eaf] (diff)

Child: [49554e] (diff)

Download this file

mh_html.cpp    181 lines (161 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright (C) 2005 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "cstr.h"
#include "mimehandler.h"
#include "debuglog.h"
#include "csguess.h"
#include "readfile.h"
#include "transcode.h"
#include "mimeparse.h"
#include "myhtmlparse.h"
#include "indextext.h"
#include "mh_html.h"
#include "smallut.h"
#include "md5.h"
#include <iostream>
#ifndef NO_NAMESPACES
using namespace std;
#endif /* NO_NAMESPACES */
bool MimeHandlerHtml::set_document_file(const string &fn)
{
LOGDEB0(("textHtmlToDoc: %s\n", fn.c_str()));
RecollFilter::set_document_file(fn);
string otext;
if (!file_to_string(fn, otext)) {
LOGINFO(("textHtmlToDoc: cant read: %s\n", fn.c_str()));
return false;
}
m_filename = fn;
return set_document_string(otext);
}
bool MimeHandlerHtml::set_document_string(const string& htext)
{
m_html = htext;
m_havedoc = true;
// We want to compute the md5 now because we may modify m_html later
string md5, xmd5;
MD5String(htext, md5);
m_metaData["md5"] = MD5HexPrint(md5, xmd5);
return true;
}
bool MimeHandlerHtml::next_document()
{
if (m_havedoc == false)
return false;
m_havedoc = false;
// If set_doc(fn), take note of file name.
string fn = m_filename;
m_filename.erase();
string charset = m_dfltInputCharset;
LOGDEB(("MHHtml::next_doc.: default supposed input charset: [%s]\n",
charset.c_str()));
// Override default input charset if someone took care to set one:
map<string,string>::const_iterator it = m_metaData.find(cstr_charset);
if (it != m_metaData.end() && !it->second.empty()) {
charset = it->second;
LOGDEB(("MHHtml: next_doc.: input charset from metadata: [%s]\n",
charset.c_str()));
}
// - We first try to convert from the supposed charset
// (which may depend of the current directory) to utf-8. If this
// fails, we keep the original text
// - During parsing, if we find a charset parameter, and it differs from
// what we started with, we abort and restart with the parameter value
// instead of the configuration one.
MyHtmlParser result;
for (int pass = 0; pass < 2; pass++) {
string transcoded;
LOGDEB(("Html::mkDoc: pass %d\n", pass));
MyHtmlParser p;
// Try transcoding. If it fails, use original text.
int ecnt;
if (!transcode(m_html, transcoded, charset, "UTF-8", &ecnt)) {
LOGDEB(("textHtmlToDoc: transcode failed from cs '%s' to UTF-8 for"
"[%s]", charset.c_str(), fn.empty()?"unknown":fn.c_str()));
transcoded = m_html;
// We don't know the charset, at all
p.reset_charsets();
charset.clear();
} else {
if (ecnt) {
if (pass == 0) {
LOGDEB(("textHtmlToDoc: init transcode had %d errors for "
"[%s]\n", ecnt, fn.empty()?"unknown":fn.c_str()));
} else {
LOGERR(("textHtmlToDoc: final transcode had %d errors for "
"[%s]\n", ecnt, fn.empty()?"unknown":fn.c_str()));
}
}
// charset has the putative source charset, transcoded is now
// in utf-8
p.set_charsets(charset, "utf-8");
}
try {
p.parse_html(transcoded);
// No exception: ok?
result = p;
break;
} catch (bool diag) {
result = p;
if (diag == true) {
// Parser throws true at end of text. ok
if (m_forPreview) {
// Save the html text
m_html = transcoded;
// In many cases, we need to change the charset decl,
// because the file was transcoded. It seems that just
// inserting one is enough (only the 1st one seems to
// be used by browsers/qtextedit).
string::size_type idx = m_html.find("<head>");
if (idx == string::npos)
idx = m_html.find("<HEAD>");
if (idx != string::npos)
m_html.replace(idx+6, 0,
"<meta http-equiv=\"content-type\" "
"content=\"text/html; charset=utf-8\">");
}
break;
}
LOGDEB(("textHtmlToDoc: charset [%s] doc charset [%s]\n",
charset.c_str(), result.get_charset().c_str()));
if (!result.get_charset().empty() &&
!samecharset(result.get_charset(), result.fromcharset)) {
LOGDEB(("textHtmlToDoc: reparse for charsets\n"));
// Set the origin charset as specified in document before
// transcoding again
charset = result.get_charset();
} else {
LOGERR(("textHtmlToDoc:: error: non charset exception\n"));
return false;
}
}
}
m_metaData["origcharset"] = result.get_charset();
m_metaData[cstr_content] = result.dump;
m_metaData[cstr_charset] = "utf-8";
// Avoid setting empty values which would crush ones possibly inherited
// from parent (if we're an attachment)
if (!result.dmtime.empty())
m_metaData["modificationdate"] = result.dmtime;
m_metaData[cstr_mimetype] = cstr_textplain;
for (map<string,string>::const_iterator it = result.meta.begin();
it != result.meta.end(); it++) {
if (!it->second.empty())
m_metaData[it->first] = it->second;
}
return true;
}