15 #include <kcharsets.h>
20 time_t parseISO8601Date(
const TQString &s)
23 if (s.stripWhiteSpace().left(4).toInt() < 1000)
27 if (s.find(
'T') != -1)
28 return KRFCDate::parseDateISO8601(s);
30 return KRFCDate::parseDateISO8601(s +
"T12:00:00");
33 TQString childNodesAsXML(
const TQDomNode& parent)
35 TQDomNodeList list = parent.childNodes();
37 TQTextStream ts( &str, IO_WriteOnly );
38 for (uint i = 0; i < list.count(); ++i)
40 return str.stripWhiteSpace();
43 static TQString plainTextToHtml(
const TQString& plainText)
45 TQString str(plainText);
46 str.replace(
"&",
"&");
47 str.replace(
"\"",
""");
48 str.replace(
"<",
"<");
50 str.replace(
"\n",
"<br/>");
54 enum ContentFormat { Text, HTML, XML, Binary };
56 static ContentFormat mapTypeToFormat(
const TQString& modep,
const TQString& typep,
const TQString& src)
58 TQString mode = modep.isNull() ?
"escaped" : modep;
59 TQString type = typep;
64 if (type.isNull() && src.isEmpty())
65 type = TQString::fromUtf8(
"text");
67 if (type == TQString::fromUtf8(
"html")
68 || type == TQString::fromUtf8(
"text/html"))
71 if (type == TQString::fromUtf8(
"text")
72 || (type.startsWith(TQString::fromUtf8(
"text/"),
false)
73 && !type.startsWith(TQString::fromUtf8(
"text/xml"),
false))
77 TQStringList xmltypes;
78 xmltypes.append(TQString::fromUtf8(
"xhtml"));
80 xmltypes.append(TQString::fromUtf8(
"text/xml"));
81 xmltypes.append(TQString::fromUtf8(
"application/xml"));
82 xmltypes.append(TQString::fromUtf8(
"text/xml-external-parsed-entity"));
83 xmltypes.append(TQString::fromUtf8(
"application/xml-external-parsed-entity"));
84 xmltypes.append(TQString::fromUtf8(
"application/xml-dtd"));
87 if (xmltypes.contains(type)
88 || type.endsWith(TQString::fromUtf8(
"+xml"),
false)
89 || type.endsWith(TQString::fromUtf8(
"/xml"),
false))
95 static TQString extractAtomContent(
const TQDomElement& e)
97 ContentFormat format = mapTypeToFormat(e.attribute(
"mode"),
105 const bool hasPre = e.text().contains(
"<pre>",
false ) || e.text().contains(
"<pre ",
false );
106 return KCharsets::resolveEntities( hasPre ? e.text() : e.text().simplifyWhiteSpace() );
109 return plainTextToHtml(e.text().stripWhiteSpace());
111 return childNodesAsXML(e).simplifyWhiteSpace();
120 TQString extractNode(
const TQDomNode &parent,
const TQString &elemName,
bool isInlined)
122 TQDomNode node = parent.namedItem(elemName);
126 TQDomElement e = node.toElement();
127 TQString result = e.text().stripWhiteSpace();
129 if (elemName ==
"content")
131 result = extractAtomContent(e);
135 bool hasPre = result.contains(
"<pre>",
false) || result.contains(
"<pre ",
false);
136 bool hasHtml = hasPre || result.contains(
"<");
137 if(!isInlined && !hasHtml)
138 result = result = result.replace(TQChar(
'\n'),
"<br />");
140 result = result.simplifyWhiteSpace();
143 return result.isEmpty() ? TQString() : result;
146 TQString extractTitle(
const TQDomNode & parent)
148 TQDomNode node = parent.namedItem(TQString::fromLatin1(
"title"));
152 TQString result = node.toElement().text();
154 result = KCharsets::resolveEntities(KCharsets::resolveEntities(result).replace(TQRegExp(
"<[^>]*>"),
"").remove(
"\\"));
155 result = result.simplifyWhiteSpace();
157 if (result.isEmpty())
163 static void authorFromString(
const TQString& strp, TQString& name, TQString& email)
165 TQString str = strp.stripWhiteSpace();
172 TQRegExp remail(
"<?([^@\\s<]+@[^>\\s]+)>?");
175 int pos = remail.search(str);
178 TQString all = remail.cap(0);
179 email = remail.cap(1);
180 str.replace(all,
"");
185 name = str.simplifyWhiteSpace();
193 TQRegExp rename(
"^\\(([^\\)]*)\\)");
195 pos = rename.search(name);
199 name = rename.cap(1);
202 name = name.isEmpty() ? TQString() : name;
203 email = email.isEmpty() ? TQString() : email;
206 TQString parseItemAuthor(
const TQDomElement& element, Format format, Version version)
211 TQDomElement dcCreator = element.namedItem(
"dc:creator").toElement();
213 if (!dcCreator.isNull())
214 authorFromString(dcCreator.text(), name, email);
215 else if (format == AtomFeed)
217 TQDomElement atomAuthor = element.namedItem(
"author").toElement();
218 if (atomAuthor.isNull())
219 atomAuthor = element.namedItem(
"atom:author").toElement();
220 if (!atomAuthor.isNull())
222 TQDomElement atomName = atomAuthor.namedItem(
"name").toElement();
223 if (atomName.isNull())
224 atomName = atomAuthor.namedItem(
"atom:name").toElement();
225 name = atomName.text().stripWhiteSpace();
227 TQDomElement atomEmail = atomAuthor.namedItem(
"email").toElement();
228 if (atomEmail.isNull())
229 atomEmail = atomAuthor.namedItem(
"atom:email").toElement();
230 email = atomEmail.text().stripWhiteSpace();
233 else if (format == RSSFeed)
235 authorFromString(element.namedItem(
"author").toElement().text(), name, email);
242 return TQString(
"<a href=\"mailto:%1\">%2</a>").arg(email).arg(name);