22#include <tqtextcodec.h>
24#include <tqfileinfo.h>
32#include <KWEFBaseWorker.h>
34#include "ExportFilter.h"
36TQString HtmlWorker::escapeHtmlText(
const TQString& strText)
const
40 return KWEFUtil::EscapeSgmlText(getCodec(),strText,
true,
false);
43bool HtmlWorker::makeTable(
const FrameAnchor& anchor)
45 *m_streamOut <<
"<table>\n";
46 *m_streamOut <<
"<tbody>\n";
48 TQValueList<TableCell>::ConstIterator itCell;
51 *m_streamOut <<
"<tr>\n";
54 for (itCell=anchor.table.cellList.begin();
55 itCell!=anchor.table.cellList.end(); itCell++)
57 if (rowCurrent!=(*itCell).row)
59 rowCurrent=(*itCell).row;
60 *m_streamOut <<
"</tr>\n<tr>\n";
63 *m_streamOut <<
"<td";
64 if ( (*itCell).m_rows > 1 )
65 *m_streamOut <<
" rowspan=\"" << (*itCell).m_rows <<
"\"";
66 if ( (*itCell).m_cols > 1 )
67 *m_streamOut <<
" colspan=\"" << (*itCell).m_cols <<
"\"";
68 *m_streamOut <<
">\n";
70 if (!doFullAllParagraphs(*(*itCell).paraList))
75 *m_streamOut <<
"</td>\n";
78 *m_streamOut <<
"</tr>\n";
79 *m_streamOut <<
"</tbody>\n";
80 *m_streamOut <<
"</table>\n";
85TQString HtmlWorker::getAdditionalFileName(
const TQString& additionalName)
87 kdDebug(30503) <<
"HtmlWorker::getAdditionalFileName " << additionalName << endl;
89 TQDir dir(m_strFileDir);
90 kdDebug(30503) <<
"Base directory: " << m_strFileDir << endl;
92 if (!dir.exists(m_strSubDirectoryName))
95 kdDebug(30503) <<
"Creating directory: " << m_strSubDirectoryName << endl;
96 dir.mkdir(m_strSubDirectoryName);
99 TQString strFileName(m_strSubDirectoryName);
101 const int result=additionalName.findRev(
"/");
104 strFileName+=additionalName.mid(result+1);
108 strFileName+=additionalName;
113 TQString strBackupName(strFileName);
115 kdDebug(30503) <<
"Remove backup file: " << strBackupName << endl;
117 dir.remove(strBackupName);
118 kdDebug(30503) <<
"Moving file: " << additionalName <<
" => " << strBackupName << endl;
119 dir.rename(strFileName,strBackupName);
124bool HtmlWorker::makeImage(
const FrameAnchor& anchor)
126 const TQString strImageName(getAdditionalFileName(anchor.picture.koStoreName));
128 TQString strImagePath(m_strFileDir);
130 strImagePath+=strImageName;
134 kdDebug(30503) <<
"Image " << anchor.picture.koStoreName <<
" will be written in " << strImageName << endl;
136 if (loadSubFile(anchor.picture.koStoreName,image))
138 bool writePicture =
false;
140 const double height = anchor.frame.bottom - anchor.frame.top;
141 const double width = anchor.frame.right - anchor.frame.left;
143 const int pos = anchor.picture.koStoreName.findRev(
'.' );
146 extension = anchor.picture.koStoreName.mid( pos+1 ).lower();
148 if ( extension ==
"png" || extension ==
"jpeg" || extension ==
"jpg" || extension ==
"gif"
149 || extension ==
"bmp" )
151 *m_streamOut <<
"<img ";
152 *m_streamOut <<
"src=\"" << escapeHtmlText(strImageName) <<
"\" ";
153 *m_streamOut <<
"alt=\"" << escapeHtmlText(anchor.picture.key.filename()) <<
"\"";
154 *m_streamOut << (isXML()?
"/>":
">");
157 else if ( extension ==
"svg" )
160 *m_streamOut <<
"<object data=\"" << escapeHtmlText(strImageName) <<
"\"";
161 *m_streamOut <<
" type=\"image/svg+xml\"";
162 *m_streamOut <<
" height=\"" << height <<
"\" width=\"" << width <<
"\">\n";
163 *m_streamOut <<
"</object>";
166 else if ( extension ==
"qpic" )
171 TQIODevice* io=getSubFileDevice(anchor.picture.koStoreName);
179 if (picture.load(io))
183 *m_streamOut <<
"<object data=\"" << escapeHtmlText(strImageName) <<
"\"";
184 *m_streamOut <<
" type=\"image/svg+xml\"";
185 *m_streamOut <<
" height=\"" << height <<
"\" width=\"" << width <<
"\">\n";
186 *m_streamOut <<
"</object>";
189 kdDebug(30506) <<
"Trying to save clipart to " << strImageName << endl;
190 if (!picture.save(strImagePath,
"svg"))
192 kdError(30506) <<
"Could not save clipart: " << anchor.picture.koStoreName
193 <<
" to " << strImageName << endl;
203 if ( ! loadAndConvertToImage( anchor.picture.koStoreName, extension,
"PNG", image ) )
205 kdWarning(30503) <<
"Could not convert picture to PNG!" << endl;
208 *m_streamOut <<
"<img ";
209 *m_streamOut <<
"src=\"" << escapeHtmlText(strImageName) <<
"\" ";
210 *m_streamOut <<
"alt=\"" << escapeHtmlText(anchor.picture.key.filename()) <<
"\"";
211 *m_streamOut << (isXML()?
"/>":
">");
218 TQFile file(strImagePath);
220 if ( !file.open (IO_WriteOnly) )
222 kdError(30503) <<
"Unable to open image output file!" << endl;
226 file.writeBlock(image);
232 kdWarning(30503) <<
"Unable to load picture " << anchor.picture.koStoreName << endl;
238void HtmlWorker::formatTextParagraph(
const TQString& strText,
239 const FormatData& formatOrigin,
const FormatData& format)
241 TQString strEscaped(escapeHtmlText(strText));
245 TQString strBr(isXML()?TQString(
"<br />"):TQString(
"<br>"));
246 while ((pos=strEscaped.find(TQChar(10)))>-1)
248 strEscaped.replace(pos,1,strBr);
251 if (!format.text.missing)
254 openSpan(formatOrigin,format);
262 *m_streamOut <<
" ";
267 *m_streamOut << strEscaped;
270 if (!format.text.missing)
273 closeSpan(formatOrigin,format);
277void HtmlWorker::ProcessParagraphData (
const TQString& strTag,
const TQString ¶Text,
278 const LayoutData& layout,
const ValueListFormatData ¶FormatDataList)
280 if (paraText.isEmpty() && paraFormatDataList.first().id != 6)
282 openParagraph(strTag,layout);
283 *m_streamOut <<
" " ;
284 closeParagraph(strTag,layout);
288 bool paragraphNotOpened=
true;
290 ValueListFormatData::ConstIterator paraFormatDataIt;
292 TQString partialText;
294 for ( paraFormatDataIt = paraFormatDataList.begin ();
295 paraFormatDataIt != paraFormatDataList.end ();
298 if (1==(*paraFormatDataIt).id)
301 partialText=paraText.mid ( (*paraFormatDataIt).pos, (*paraFormatDataIt).len );
303 if (paragraphNotOpened)
305 openParagraph(strTag,layout,partialText.ref(0).direction());
306 paragraphNotOpened=
false;
308 formatTextParagraph(partialText,layout.formatData,*paraFormatDataIt);
310 else if (4==(*paraFormatDataIt).id)
313 if (paragraphNotOpened)
315 openParagraph(strTag,layout);
316 paragraphNotOpened=
false;
318 if (9==(*paraFormatDataIt).variable.m_type)
321 *m_streamOut <<
"<a href=\""
322 << escapeHtmlText((*paraFormatDataIt).variable.getHrefName())
324 << escapeHtmlText((*paraFormatDataIt).variable.getLinkName())
330 *m_streamOut << escapeHtmlText((*paraFormatDataIt).variable.m_text);
333 else if (6==(*paraFormatDataIt).id)
337 if (6==(*paraFormatDataIt).frameAnchor.type)
341 if (!paragraphNotOpened)
344 closeParagraph(strTag,layout);
346 makeTable((*paraFormatDataIt).frameAnchor);
348 paragraphNotOpened=
true;
352 else if ( ( 2 == (*paraFormatDataIt).frameAnchor.type )
353 || ( 5 == (*paraFormatDataIt).frameAnchor.type ) )
356 if (paragraphNotOpened)
358 openParagraph( strTag, layout,partialText.ref(0). direction() );
359 paragraphNotOpened=
false;
361 makeImage((*paraFormatDataIt).frameAnchor);
365 kdWarning(30503) <<
"Unknown anchor type: "
366 << (*paraFormatDataIt).frameAnchor.type << endl;
370 if (!paragraphNotOpened)
373 closeParagraph(strTag,layout);
378bool HtmlWorker::doFullParagraph(
const TQString& paraText,
379 const LayoutData& layout,
const ValueListFormatData& paraFormatDataList)
381 kdDebug(30503) <<
"Entering HtmlWorker::doFullParagraph" << endl << paraText << endl;
382 TQString strParaText=paraText;
387 const uint layoutDepth=layout.counter.depth+1;
388 const uint listDepth=m_listStack.size();
390 if (layoutDepth>listDepth)
393 newList.m_typeList=layout.counter.style;
394 for (uint i=listDepth; i<layoutDepth; i++)
396 *m_streamOut << getStartOfListOpeningTag(layout.counter.style,newList.m_orderedList);
397 m_listStack.push(newList);
400 else if (layoutDepth<listDepth)
402 for (uint i=listDepth; i>layoutDepth; i--)
404 ListInfo oldList=m_listStack.pop();
405 if (oldList.m_orderedList)
407 *m_streamOut <<
"</ol>\n";
411 *m_streamOut <<
"</ul>\n";
417 if ( layout.counter.style!=m_listStack.top().m_typeList)
420 ListInfo oldList=m_listStack.pop();
421 if (oldList.m_orderedList)
423 *m_streamOut <<
"</ol>\n";
427 *m_streamOut <<
"</ul>\n";
430 *m_streamOut << getStartOfListOpeningTag(layout.counter.style,newList.m_orderedList);
431 newList.m_typeList=layout.counter.style;
432 m_listStack.push(newList);
441 if (!m_listStack.isEmpty())
443 for (uint i=m_listStack.size(); i>0; i--)
445 ListInfo oldList=m_listStack.pop();
446 if (oldList.m_orderedList)
448 *m_streamOut <<
"</ol>\n";
452 *m_streamOut <<
"</ul>\n";
457 && (layout.counter.depth<6) )
459 strTag=TQString(
"h%1").arg(layout.counter.depth + 1);
467 ProcessParagraphData(strTag, strParaText, layout, paraFormatDataList);
469 kdDebug(30503) <<
"Quiting HtmlWorker::doFullParagraph" << endl;
473bool HtmlWorker::doOpenFile(
const TQString& filenameOut,
const TQString& )
475 m_ioDevice=
new TQFile(filenameOut);
479 kdError(30503) <<
"No output file! Aborting!" << endl;
483 if ( !m_ioDevice->open (IO_WriteOnly) )
485 kdError(30503) <<
"Unable to open output file!" << endl;
489 m_streamOut=
new TQTextStream(m_ioDevice);
493 kdError(30503) <<
"Could not create TQTextCodec! Aborting" << endl;
497 kdDebug(30503) <<
"Charset used: " << getCodec()->name() << endl;
499 m_streamOut->setCodec( getCodec() );
501 m_fileName=filenameOut;
502 TQFileInfo base(m_fileName);
503 m_strFileDir=base.dirPath();
504 m_strTitle=base.fileName();
505 m_strSubDirectoryName=base.fileName();
506 m_strSubDirectoryName+=
".dir";
511bool HtmlWorker::doCloseFile(
void)
513 kdDebug(30503) << __FILE__ <<
":" << __LINE__ << endl;
521void HtmlWorker::writeDocType(
void)
524 *m_streamOut <<
"<!DOCTYPE ";
527 *m_streamOut <<
"html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">\n";
531 *m_streamOut <<
"HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n";
535bool HtmlWorker::doOpenDocument(
void)
541 *m_streamOut <<
"<?xml version=\"1.0\" encoding=\""
542 << getCodec()->mimeName() <<
"\"?>" << endl;
549 *m_streamOut <<
"<html";
553 *m_streamOut <<
" xmlns=\"http://www.w3.org/1999/xhtml\"";
555 *m_streamOut <<
">\n";
559bool HtmlWorker::doCloseDocument(
void)
561 kdDebug(30503) << __FILE__ <<
":" << __LINE__ << endl;
562 *m_streamOut <<
"</html>\n";
566bool HtmlWorker::doFullDocumentInfo(
const KWEFDocumentInfo& docInfo)
568 TQString strText=docInfo.title;
569 if (!strText.isEmpty())
572 kdDebug(30503) <<
"Found new title " << m_strTitle << endl;
577bool HtmlWorker::doOpenHead(
void)
579 *m_streamOut <<
"<head>" << endl;
582 *m_streamOut <<
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=";
583 *m_streamOut << getCodec()->mimeName() <<
'"';
584 *m_streamOut << (isXML()?
" /":
"") <<
">\n" ;
587 TQString strVersion(
"$Revision: 466447 $");
590 *m_streamOut <<
"<meta name=\"Generator\" content=\"KWord HTML Export Filter Version"
591 << strVersion.mid( 10 ).remove(
'$' )
592 <<
"\""<< (isXML()?
" /":
"")
595 if (m_strTitle.isEmpty())
598 kdWarning(30503) <<
"Title still empty! (HtmlWorker::doOpenHead)" << endl;
599 m_strTitle=i18n(
"Untitled Document");
601 *m_streamOut <<
"<title>"<< escapeHtmlText(m_strTitle) <<
"</title>\n";
603 if( !customCSSURL().isEmpty() )
605 *m_streamOut <<
"<link ref=\"stylesheet\" type=\"text/css\" href=\"" << customCSSURL() <<
"\" title=\"Style\" >\n" << endl;
613bool HtmlWorker::doCloseHead(
void)
615 *m_streamOut <<
"</head>\n";
619bool HtmlWorker::doOpenBody(
void)
621 *m_streamOut <<
"<body>\n";
625bool HtmlWorker::doCloseBody(
void)
627 *m_streamOut <<
"</body>\n";
631bool HtmlWorker::doOpenTextFrameSet(
void)
636bool HtmlWorker::doCloseTextFrameSet(
void)
638 if (!m_listStack.isEmpty())
640 for (uint i=m_listStack.size(); i>0; i--)
642 ListInfo oldList=m_listStack.pop();
643 if (oldList.m_orderedList)
645 *m_streamOut <<
"</ol>\n";
649 *m_streamOut <<
"</ul>\n";
@ NUM_LIST
Numbered as a list item.
@ NUM_CHAPTER
Numbered as a heading.
This is basically <FRAMESET> tag.