19#include "tdehtmlreader.h"
21#include "tdehtmlreader.moc"
23#include <dom/dom_text.h>
24#include <dom/dom2_views.h>
25#include <dom/dom_doc.h>
27#include <dom/dom_element.h>
28#include <dom/html_table.h>
29#include <tdehtmlview.h>
31#include <tdeapplication.h>
32#include <dom/html_misc.h>
35TDEHTMLReader::TDEHTMLReader(KWDWriter *w)
37 _html=
new TDEHTMLPart();
43void tqt_enter_modal( TQWidget *widget );
44void tqt_leave_modal( TQWidget *widget );
47bool TDEHTMLReader::filter(KURL url)
49 kdDebug(30503) <<
"TDEHTMLReader::filter" << endl;
50 TQObject::connect(_html,TQ_SIGNAL(completed()),
this,TQ_SLOT(completed()));
55 _html->view()->resize(600,530);
56 _html->setAutoloadImages(
false);
57 _html->setJScriptEnabled(
false);
58 _html->setPluginsEnabled(
false);
59 _html->setJavaEnabled(
false);
60 _html->setMetaRefreshEnabled(
false);
61 if (_html->openURL(url) ==
false)
63 kdWarning(30503) <<
"openURL returned false" << endl;
68 TQWidget dummy(0,0,WType_Dialog | WShowModal);
69 tqt_enter_modal(&dummy);
71 tqt_leave_modal(&dummy);
75HTMLReader_state *TDEHTMLReader::state()
77 if (_state.count() == 0)
79 HTMLReader_state *s=
new HTMLReader_state;
80 s->frameset=_writer->mainFrameset();
81 s->paragraph = _writer->addParagraph(s->frameset);
82 s->format=_writer->currentFormat(s->paragraph,
true);
83 s->layout=_writer->currentLayout(s->paragraph);
84 s->in_pre_mode =
false;
90HTMLReader_state *TDEHTMLReader::pushNewState()
92 HTMLReader_state *s=
new HTMLReader_state;
93 s->frameset=state()->frameset;
94 s->paragraph=state()->paragraph;
95 s->format=state()->format;
96 s->layout=state()->layout;
97 s->in_pre_mode=state()->in_pre_mode;
98 _writer->cleanUpParagraph(s->paragraph);
104void TDEHTMLReader::popState()
106 kdDebug(30503) <<
"Entering popState" << endl;
108 HTMLReader_state *s=_state.pop();
118 if (s->frameset == state()->frameset)
120 state()->paragraph=s->paragraph;
121 if ((state()->layout != s->layout))
123 if (_writer->getText(state()->paragraph).length()!=0) startNewLayout(
false,state()->layout);
125 state()->format=_writer->startFormat(state()->paragraph, state()->format);
130void TDEHTMLReader::startNewLayout(
bool startNewFormat)
133 startNewLayout(startNewFormat,layout);
136void TDEHTMLReader::startNewLayout(
bool startNewFormat, TQDomElement layout)
138 kdDebug() <<
"entering startNewLayout" << endl;
139 startNewParagraph(startNewFormat,
true);
140 state()->layout=_writer->setLayout(state()->paragraph,layout);
143void TDEHTMLReader::completed()
145 kdDebug(30503) <<
"TDEHTMLReader::completed" << endl;
147 DOM::Document doc=_html->document();
148 DOM::NodeList list=doc.getElementsByTagName(
"body");
149 DOM::Node docbody=list.item(0);
151 if (docbody.isNull())
153 kdWarning(30503) <<
"no <BODY>, giving up" << endl;
161 list = doc.getElementsByTagName(
"head");
162 DOM::Node dochead=list.item(0);
163 if (!dochead.isNull())
166 kdWarning(30503) <<
"WARNING: no html <HEAD> section" << endl;
168 _writer->cleanUpParagraph(state()->paragraph);
169 _it_worked=_writer->writeDoc();
173void TDEHTMLReader::parseNode(DOM::Node node)
175 kdDebug(30503) <<
"Entering parseNode" << endl;
180 _writer->addText(state()->paragraph,t.data().string(),1,state()->in_pre_mode);
185 state()->format=_writer->currentFormat(state()->paragraph,
true);
186 state()->layout=_writer->currentLayout(state()->paragraph);
191 bool go_recursive=
true;
198 go_recursive=parseTag(e);
202 for (DOM::Node q=node.firstChild(); !q.isNull(); q=q.nextSibling())
210void TDEHTMLReader::parse_head(DOM::Element e)
212 for (DOM::Element items=e.firstChild();!items.isNull();items=items.nextSibling()) {
213 if (items.tagName().string().lower() ==
"title") {
214 DOM::Text t=items.firstChild();
216 _writer->createDocInfo(
"HTML import filter",t.data().string());
222#define _PF(x,a,b,c) { \
223 if (e.tagName().lower() == #x) \
225 _writer->formatAttribute(state()->paragraph, #a,#b,#c); \
233#define _PL(x,a,b,c) { \
234 if (e.tagName().lower() == #x) \
236 state()->layout=_writer->setLayout(state()->paragraph,state()->layout);\
237 if (!(_writer->getText(state()->paragraph).isEmpty())) \
238 startNewParagraph(false,false); \
239 _writer->layoutAttribute(state()->paragraph, #a,#b,#c); \
245bool TDEHTMLReader::parseTag(DOM::Element e)
247 kdDebug(30503) <<
"Entering parseTag for " << e.tagName().lower() << endl;
248 if (e.tagName().lower()==
"a") {
return parse_a(e); }
249 if (e.tagName().lower()==
"p") {
return parse_p(e); }
250 if (e.tagName().lower()==
"br") {
return parse_br(e); }
251 if (e.tagName().lower()==
"ul") {
return parse_ul(e); }
252 if (e.tagName().lower()==
"ol") {
return parse_ol(e); }
253 if (e.tagName().lower()==
"hr") {
return parse_hr(e); }
254 if (e.tagName().lower()==
"pre") {
return parse_pre(e); }
255 if (e.tagName().lower()==
"font") {
return parse_font(e); }
256 if (e.tagName().lower()==
"table") {
return parse_table(e); }
260 _PF(b,WEIGHT,value,75);
261 _PF(strong,WEIGHT,value,75);
262 _PF(u,UNDERLINE,value,1);
263 _PF(i,ITALIC,value,1);
265 _PL(center,FLOW,align,center);
266 _PL(right,FLOW,align,right);
267 _PL(left,FLOW,align,left);
269 _PL(h1,NAME,value,h1);
270 _PL(h2,NAME,value,h2);
271 _PL(h3,NAME,value,h3);
272 _PL(h4,NAME,value,h4);
273 _PL(h5,NAME,value,h5);
274 _PL(h6,NAME,value,h6);
275 kdDebug(30503) <<
"Leaving parseTag" << endl;
278 if(e.nodeType() == DOM::Node::COMMENT_NODE || e.tagName().lower() ==
"script")
286void TDEHTMLReader::startNewParagraph(
bool startnewformat,
bool startnewlayout)
288 kdDebug() <<
"Entering startNewParagraph" << endl;
290 TQDomElement qf=state()->format;
291 TQDomElement ql=state()->layout;
293 _writer->cleanUpParagraph(state()->paragraph);
295 if ((startnewlayout==
true) || ql.isNull())
296 {state()->paragraph=_writer->addParagraph(state()->frameset);}
299 _writer->addParagraph(state()->frameset,state()->layout);}
303 if (qf.isNull() || (startnewformat==
true))
305 state()->format=_writer->startFormat(state()->paragraph);
309 state()->format=_writer->startFormat(state()->paragraph,qf);
317 TQString ct=_writer->getLayoutAttribute(state()->paragraph,
"COUNTER",
"type");
318 if ((!ct.isNull()) && (ct !=
"0"))
320 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"type",
"0");
321 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"numberingtype",
"0");
322 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"righttext",
"");
323 int currdepth=(_writer->getLayoutAttribute(state()->paragraph,
"COUNTER",
"depth")).toInt();
324 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"depth",TQString(
"%1").arg(currdepth+1));
328TDEHTMLReader::~TDEHTMLReader(){
342bool TDEHTMLReader::parse_CommonAttributes(DOM::Element e)
344 kdDebug(30503) <<
"entering TDEHTMLReader::parse_CommonAttributes" << endl;
345 kdDebug(30503) <<
"tagName is " << e.tagName().string() << endl;
346 TQString s=e.getAttribute(
"align").string();
349 _writer->formatAttribute(state()->paragraph,
"FLOW",
"align",s);
351 TQRegExp rx(
"h[0-9]+" );
352 if ( 0 == rx.search( e.getAttribute(
"class").string() ) )
355 kdDebug(30503) <<
"laying out with " << e.getAttribute(
"class").string() << endl;
356 _writer->layoutAttribute(state()->paragraph,
"NAME",
"value",e.getAttribute(
"class").string());
358 if ( e.getAttribute(
"class").string()==
"Standard" )
360 kdDebug(30503) <<
"laying out with " << e.getAttribute(
"class").string() << endl;
361 _writer->layoutAttribute(state()->paragraph,
"NAME",
"value",e.getAttribute(
"class").string());
363 kdDebug(30503) <<
"leaving parse_CommonAttributes" << endl;
367bool TDEHTMLReader::parse_a(DOM::Element e) {
368 TQString url = e.getAttribute(
"href").string();
372 DOM::Text t = e.firstChild();
377 linkName = t.data().string().simplifyWhiteSpace();
378 t.setData(DOM::DOMString(
"#"));
379 _writer->createLink(state()->paragraph, linkName, url);
384bool TDEHTMLReader::parse_p(DOM::Element e)
388 kdDebug() <<
"entering parse_p" << endl;
389 if (!(_writer->getText(state()->paragraph).isEmpty()))
390 startNewParagraph(
false,
false);
391 parse_CommonAttributes(e);
392 kdDebug() <<
"leaving parse_p" << endl;
396bool TDEHTMLReader::parse_hr(DOM::Element )
399 _writer->createHR(state()->paragraph);
404bool TDEHTMLReader::parse_br(DOM::Element )
406 startNewParagraph(
false,
false);
410static const TQColor parsecolor(
const TQString& colorstring)
413 if (colorstring[0]==
'#')
416 colorstring.mid(1,2).toInt(0,16),
417 colorstring.mid(3,2).toInt(0,16),
418 colorstring.mid(5,2).toInt(0,16)
423 TQString colorlower=colorstring.lower();
425 if (colorlower==
"black")
427 else if (colorlower==
"white")
428 color.setRgb(255,255,255);
429 else if (colorlower==
"silver")
430 color.setRgb(0xc0,0xc0,0xc0);
431 else if (colorlower==
"gray")
432 color.setRgb(128,128,128);
434 else if (colorlower==
"red")
435 color.setRgb(255,0,0);
436 else if (colorlower==
"lime")
437 color.setRgb(0,255,0);
438 else if (colorlower==
"blue")
439 color.setRgb(0,0,255);
440 else if (colorlower==
"yellow")
441 color.setRgb(255,255,0);
442 else if (colorlower==
"fuchsia")
443 color.setRgb(255,0,255);
444 else if (colorlower==
"aqua")
445 color.setRgb(0,255,255);
447 else if (colorlower==
"maroon")
448 color.setRgb(128,0,0);
449 else if (colorlower==
"green")
450 color.setRgb(0,128,0);
451 else if (colorlower==
"navy")
452 color.setRgb(0,0,128);
453 else if (colorlower==
"olive")
454 color.setRgb(128,128,0);
455 else if (colorlower==
"purple")
456 color.setRgb(128,0,128);
457 else if (colorlower==
"teal")
458 color.setRgb(0,128,128);
463 color.setNamedColor(colorstring);
469void TDEHTMLReader::parseStyle(DOM::Element e)
474 kdDebug(30503) <<
"entering parseStyle" << endl;
475 DOM::CSSStyleDeclaration s1=e.style();
476 DOM::Document doc=_html->document();
477 DOM::CSSStyleDeclaration s2=doc.defaultView().getComputedStyle(e,
"");
478 kdDebug(30503) <<
"font-weight=" << s1.getPropertyValue(
"font-weight").string() << endl;
479 if ( s1.getPropertyValue(
"font-weight").string() ==
"bolder" )
481 _writer->formatAttribute(state()->paragraph,
"WEIGHT",
"value",
"75");
483 if ( s1.getPropertyValue(
"font-weight").string() ==
"bold" )
485 _writer->formatAttribute(state()->paragraph,
"WEIGHT",
"value",
"75");
489 if ( s1.getPropertyValue(
"color").string() != TQString() )
491 TQColor c=parsecolor(s1.getPropertyValue(
"color").string());
492 _writer->formatAttribute(state()->paragraph,
"COLOR",
"red",TQString::number(c.red()));
493 _writer->formatAttribute(state()->paragraph,
"COLOR",
"green",TQString::number(c.green()));
494 _writer->formatAttribute(state()->paragraph,
"COLOR",
"blue",TQString::number(c.blue()));
498 if ( s1.getPropertyValue(
"font-size").string() != TQString() )
500 TQString size=s1.getPropertyValue(
"font-size").string();
501 if (size.endsWith(
"pt"))
503 size=size.left(size.length()-2);
505 _writer->formatAttribute(state()->paragraph,
"SIZE",
"value",size);
509 if ( s1.getPropertyValue(
"text-align").string() != TQString() && s1.getPropertyValue(
"text-align").string() != TQString(
"left") )
511 state()->layout=_writer->setLayout(state()->paragraph,state()->layout);
512 _writer->layoutAttribute(state()->paragraph,
"FLOW",
"align",s1.getPropertyValue(
"text-align").string());
532bool TDEHTMLReader::parse_table(DOM::Element e)
534 if(_writer->isInTable()) {
537 for (DOM::Node rows=e.firstChild().firstChild();!rows.isNull();rows=rows.nextSibling())
538 if (!rows.isNull() && rows.nodeName().string().lower() ==
"tr")
539 for (DOM::Node cols=rows.firstChild();!cols.isNull();cols=cols.nextSibling())
545 DOM::Element table_body=e.firstChild();
546 if(table_body.isNull()) {
553 int tableno=_writer->createTable();
556 bool has_borders=
false;
557 TQColor bgcolor=parsecolor(
"#FFFFFF");
559 if (!table_body.getAttribute(
"bgcolor").string().isEmpty())
560 bgcolor=parsecolor(table_body.getAttribute(
"bgcolor").string());
561 if ((e.getAttribute(
"border").string().toInt() > 0))
566 for (DOM::Node rowsnode=table_body.firstChild();!rowsnode.isNull();rowsnode=rowsnode.nextSibling()) {
567 DOM::Element rows = rowsnode;
568 if (!rows.isNull() && rows.tagName().string().lower() ==
"tr") {
569 TQColor obgcolor=bgcolor;
570 if (!rows.getAttribute(
"bgcolor").string().isEmpty())
571 bgcolor=parsecolor(rows.getAttribute(
"bgcolor").string());
574 for (DOM::Node colsnode=rows.firstChild();!colsnode.isNull();colsnode=colsnode.nextSibling()) {
575 DOM::Element cols = colsnode;
576 const TQString nodename = cols.isNull() ? TQString() : cols.nodeName().string().lower();
577 if (nodename ==
"td" || nodename ==
"th") {
578 TQColor bbgcolor=bgcolor;
579 if (!cols.getAttribute(
"bgcolor").string().isEmpty())
580 bgcolor=parsecolor(cols.getAttribute(
"bgcolor").string());
583 TQRect colrect=cols.getRect();
584 state()->frameset=_writer->createTableCell(tableno,nrow,ncol,1,colrect);
585 state()->frameset.firstChild().toElement().setAttribute(
"bkRed",bgcolor.red());
586 state()->frameset.firstChild().toElement().setAttribute(
"bkGreen",bgcolor.green());
587 state()->frameset.firstChild().toElement().setAttribute(
"bkBlue",bgcolor.blue());
589 state()->frameset.firstChild().toElement().setAttribute(
"lWidth",1);
590 state()->frameset.firstChild().toElement().setAttribute(
"rWidth",1);
591 state()->frameset.firstChild().toElement().setAttribute(
"bWidth",1);
592 state()->frameset.firstChild().toElement().setAttribute(
"tWidth",1);
596 state()->paragraph=_writer->addParagraph(state()->frameset);
598 _writer->cleanUpParagraph(state()->paragraph);
608 _writer->finishTable(tableno);
609 startNewParagraph(
false,
false);
610 _writer->createInline(state()->paragraph,_writer->fetchTableCell(tableno,0,0));
611 startNewParagraph(
false,
false);
615bool TDEHTMLReader::parse_img(DOM::Element )
621bool TDEHTMLReader::parse_pre(DOM::Element e)
626 DOM::HTMLElement htmlelement(e);
627 if(! htmlelement.isNull())
628 _writer->addText(state()->paragraph,htmlelement.innerHTML().string(),1);
634 state()->in_pre_mode=
true;
635 TQString face=e.getAttribute(
"face").string();
636 _writer->formatAttribute(state()->paragraph,
"FONT",
"name",TQString(
"Courier"));
637 for (DOM::Node q=e.firstChild(); !q.isNull(); q=q.nextSibling())
642 _writer->formatAttribute(state()->paragraph,
"FONT",
"name",face);
647bool TDEHTMLReader::parse_ol(DOM::Element e)
652bool TDEHTMLReader::parse_font(DOM::Element e)
654 kdDebug(30503) <<
"Entering parse_font" << endl;
656 TQString face=e.getAttribute(
"face").string();
657 TQColor color=parsecolor(
"#000000");
658 if (!e.getAttribute(
"color").string().isEmpty())
659 color=parsecolor(e.getAttribute(
"color").string());
660 TQString size=e.getAttribute(
"size").string();
662 if (size.startsWith(
"+"))
663 isize=12+size.right(size.length()-1).toInt();
664 else if (size.startsWith(
"-"))
665 isize=12-size.right(size.length()-1).toInt();
667 isize=12+size.toInt();
669 _writer->formatAttribute(state()->paragraph,
"FONT",
"name",face);
670 if ((isize>=0) && (isize != 12))
671 _writer->formatAttribute(state()->paragraph,
"SIZE",
"value",TQString(
"%1").arg(isize));
673 _writer->formatAttribute(state()->paragraph,
"COLOR",
"red",TQString(
"%1").arg(color.red()));
674 _writer->formatAttribute(state()->paragraph,
"COLOR",
"green",TQString(
"%1").arg(color.green()));
675 _writer->formatAttribute(state()->paragraph,
"COLOR",
"blue",TQString(
"%1").arg(color.blue()));
679bool TDEHTMLReader::parse_ul(DOM::Element e)
682 kdDebug(30503) <<
"Entering TDEHTMLReader::parse_ul" << endl;
683 kdDebug(30503) <<
"_writer->getText(state()->paragraph)=" << _writer->getText(state()->paragraph) << endl;
685 if (e.firstChild().nodeName().string().lower() ==
"#text")
687 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"depth",TQString(
"%1").arg(_list_depth-1));
690 for (DOM::Node items=e.firstChild();!items.isNull();items=items.nextSibling())
692 if (items.nodeName().string().lower() ==
"li")
694 if (!(_writer->getText(state()->paragraph).isEmpty())) startNewLayout();
695 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"numberingtype",
"1");
696 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"righttext",
".");
697 if (e.tagName().string().lower() ==
"ol")
699 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"type",
"1");
700 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"numberingtype",
"1");
701 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"righttext",
".");
705 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"type",
"10");
706 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"numberingtype",
"");
707 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"righttext",
"");
709 _writer->layoutAttribute(state()->paragraph,
"COUNTER",
"depth",TQString(
"%1").arg(_list_depth-1));
714 kdDebug(30503) <<
"Leaving TDEHTMLReader::parse_ul" << endl;