File indexing completed on 2025-01-12 13:05:51
0001 /* This file is part of the KDE project 0002 Copyright (C) 2001 Eva Brucherseifer <eva@kde.org> 0003 Copyright (C) 2005 Bram Schoenmakers <bramschoenmakers@kde.nl> 0004 based on kspread csv export filter by David Faure 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License as published by the Free Software Foundation; either 0009 version 2 of the License, or (at your option) any later version. 0010 0011 This library is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 Library General Public License for more details. 0015 0016 You should have received a copy of the GNU Library General Public License 0017 along with this library; see the file COPYING.LIB. If not, write to 0018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 * Boston, MA 02110-1301, USA. 0020 */ 0021 0022 #include <htmlexport.h> 0023 #include <exportdialog.h> 0024 0025 #include <QFile> 0026 #include <QTextCodec> 0027 #include <QTextStream> 0028 #include <QByteArray> 0029 #include <QUrl> 0030 0031 #include <kdebug.h> 0032 #include <kpluginfactory.h> 0033 #include <KoFilterChain.h> 0034 #include <KoFilterManager.h> 0035 #include <KoDocumentInfo.h> 0036 #include <CalligraVersionWrapper.h> 0037 0038 #include <sheets/CellStorage.h> 0039 #include <sheets/Map.h> 0040 #include <sheets/Sheet.h> 0041 #include <sheets/part/Doc.h> 0042 #include <sheets/Util.h> 0043 0044 using namespace Calligra::Sheets; 0045 0046 K_PLUGIN_FACTORY_WITH_JSON(HTMLExportFactory, "calligra_filter_sheets2html.json", 0047 registerPlugin<HTMLExport>();) 0048 0049 const QString html_table_tag = "table"; 0050 const QString html_table_options = QString(" border=\"%1\" cellspacing=\"%2\""); 0051 const QString html_row_tag = "tr"; 0052 const QString html_row_options = ""; 0053 const QString html_cell_tag = "td"; 0054 const QString html_cell_options = ""; 0055 const QString html_bold = "b"; 0056 const QString html_italic = "i"; 0057 const QString html_underline = "u"; 0058 const QString html_right = "right"; 0059 const QString html_left = "left"; 0060 const QString html_center = "center"; 0061 const QString html_top = "top"; 0062 const QString html_bottom = "bottom"; 0063 const QString html_middle = "middle"; 0064 const QString html_h1 = "h1"; 0065 0066 HTMLExport::HTMLExport(QObject* parent, const QVariantList&) : 0067 KoFilter(parent), m_dialog(new ExportDialog()) 0068 { 0069 } 0070 0071 HTMLExport::~HTMLExport() 0072 { 0073 delete m_dialog; 0074 } 0075 0076 // HTML enitities, AFAIK we don't need to escape " to " (dnaber): 0077 const QString strAmp("&"); 0078 const QString nbsp(" "); 0079 const QString strLt("<"); 0080 const QString strGt(">"); 0081 0082 // The reason why we use the KoDocument* approach and not the QDomDocument 0083 // approach is because we don't want to export formulas but values ! 0084 KoFilter::ConversionStatus HTMLExport::convert(const QByteArray& from, const QByteArray& to) 0085 { 0086 if (to != "text/html" || from != "application/x-kspread") { 0087 kWarning(30501) << "Invalid mimetypes " << to << " " << from; 0088 return KoFilter::NotImplemented; 0089 } 0090 0091 KoDocument* document = m_chain->inputDocument(); 0092 0093 if (!document) 0094 return KoFilter::StupidError; 0095 0096 if (!::qobject_cast<const Calligra::Sheets::Doc *>(document)) { // it's safer that way :) 0097 kWarning(30501) << "document isn't a Calligra::Sheets::Doc but a " << document->metaObject()->className(); 0098 return KoFilter::NotImplemented; 0099 } 0100 0101 const Doc * ksdoc = static_cast<const Doc *>(document); 0102 0103 if (ksdoc->mimeType() != "application/x-kspread") { 0104 kWarning(30501) << "Invalid document mimetype " << ksdoc->mimeType(); 0105 return KoFilter::NotImplemented; 0106 } 0107 0108 QString filenameBase = m_chain->outputFile(); 0109 filenameBase = filenameBase.left(filenameBase.lastIndexOf('.')); 0110 0111 QStringList sheets; 0112 foreach(Sheet* sheet, ksdoc->map()->sheetList()) { 0113 int rows = 0; 0114 int columns = 0; 0115 detectFilledCells(sheet, rows, columns); 0116 m_rowmap[ sheet->sheetName()] = rows; 0117 m_columnmap[ sheet->sheetName()] = columns; 0118 0119 if (rows > 0 && columns > 0) { 0120 sheets.append(sheet->sheetName()); 0121 } 0122 } 0123 m_dialog->setSheets(sheets); 0124 if (!m_chain->manager()->getBatchMode() ) { 0125 if (m_dialog->exec() == QDialog::Rejected) { 0126 return KoFilter::UserCancelled; 0127 } 0128 } 0129 0130 Sheet* sheet = 0; 0131 sheets = m_dialog->sheets(); 0132 QString str; 0133 for (int i = 0; i < sheets.count() ; ++i) { 0134 sheet = ksdoc->map()->findSheet(sheets[i]); 0135 if (!sheet) 0136 continue; 0137 0138 QString file = fileName(filenameBase, sheet->sheetName(), sheets.count() > 1); 0139 0140 if (m_dialog->separateFiles() || sheets[i] == sheets.first()) { 0141 str.clear(); 0142 openPage(sheet, document, str); 0143 writeTOC(sheets, filenameBase, str); 0144 } 0145 0146 convertSheet(sheet, str, m_rowmap[ sheet->sheetName()], m_columnmap[ sheet->sheetName()]); 0147 0148 if (m_dialog->separateFiles() || sheets[i] == sheets.last()) { 0149 closePage(str); 0150 QFile out(file); 0151 if (!out.open(QIODevice::WriteOnly)) { 0152 kError(30501) << "Unable to open output file!" << endl; 0153 out.close(); 0154 return KoFilter::FileNotFound; 0155 } 0156 QTextStream streamOut(&out); 0157 streamOut.setCodec(m_dialog->encoding()); 0158 streamOut << str << endl; 0159 out.close(); 0160 } 0161 0162 if (!m_dialog->separateFiles()) { 0163 createSheetSeparator(str); 0164 } 0165 0166 } 0167 0168 emit sigProgress(100); 0169 return KoFilter::OK; 0170 } 0171 0172 void HTMLExport::openPage(Sheet *sheet, KoDocument *document, QString &str) 0173 { 0174 QString title; 0175 KoDocumentInfo *info = document->documentInfo(); 0176 if (info && !info->aboutInfo("title").isEmpty()) 0177 title = info->aboutInfo("title") + " - "; 0178 title += sheet->sheetName(); 0179 0180 // header 0181 str = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" " 0182 " \"http://www.w3.org/TR/html4/loose.dtd\"> \n" 0183 "<html>\n" 0184 "<head>\n" 0185 "<meta http-equiv=\"Content-Type\" " + 0186 QString("content=\"text/html; charset=%1\">\n").arg(QString(m_dialog->encoding()->name())) + 0187 "<meta name=\"Generator\" " 0188 "content=\"KSpread HTML Export Filter Version = " + 0189 CalligraVersionWrapper::versionString() + 0190 "\">\n"; 0191 0192 // Insert stylesheet 0193 if (!m_dialog->customStyleURL().isEmpty()) { 0194 str += "<link ref=\"stylesheet\" type=\"text/css\" href=\"" + 0195 m_dialog->customStyleURL().url() + 0196 "\" title=\"Style\" >\n"; 0197 } 0198 0199 str += "<title>" + title + "</title>\n" 0200 "</head>\n" + 0201 QString("<body bgcolor=\"#FFFFFF\" dir=\"%1\">\n").arg( 0202 (sheet->layoutDirection() == Qt::RightToLeft) ? "rtl" : "ltr") + 0203 0204 "<a name=\"__top\">\n"; 0205 } 0206 0207 void HTMLExport::closePage(QString &str) 0208 { 0209 str += "<p align=\"" + html_center + "\"><a href=\"#__top\">" + i18n("Top") + "</a></p>\n" 0210 "</body>\n" 0211 "</html>\n\n"; 0212 } 0213 0214 void HTMLExport::convertSheet(Sheet *sheet, QString &str, int iMaxUsedRow, int iMaxUsedColumn) 0215 { 0216 QString emptyLines; 0217 0218 // Either we get hold of KSpreadTable::m_dctCells and apply the old method below (for sorting) 0219 // or, cleaner and already sorted, we use KSpreadTable's API (slower probably, though) 0220 int iMaxRow = sheet->cellStorage()->rows(); 0221 0222 if (!m_dialog->separateFiles()) 0223 str += "<a name=\"" + sheet->sheetName().toLower().trimmed() + "\">\n"; 0224 0225 str += ("<h1>" + sheet->sheetName() + "</h1><br>\n"); 0226 0227 // this is just a bad approximation which fails for documents with less than 50 rows, but 0228 // we don't need any progress stuff there anyway :) (Werner) 0229 int value = 0; 0230 int step = iMaxRow > 50 ? iMaxRow / 50 : 1; 0231 int i = 1; 0232 0233 str += '<' + html_table_tag + html_table_options.arg(m_dialog->useBorders() ? "1" : "0").arg(m_dialog->pixelsBetweenCells()) + 0234 QString("dir=\"%1\">\n").arg((sheet->layoutDirection() == Qt::RightToLeft) ? "rtl" : "ltr"); 0235 0236 unsigned int nonempty_cells_prev = 0; 0237 0238 for (int currentrow = 1 ; currentrow <= iMaxUsedRow ; ++currentrow, ++i) { 0239 if (i > step) { 0240 value += 2; 0241 emit sigProgress(value); 0242 i = 0; 0243 } 0244 0245 QString separators; 0246 QString line; 0247 unsigned int nonempty_cells = 0; 0248 0249 for (int currentcolumn = 1 ; currentcolumn <= iMaxUsedColumn ; currentcolumn++) { 0250 Cell cell(sheet, currentcolumn, currentrow); 0251 const Style style = cell.effectiveStyle(); 0252 if (cell.needsPrinting()) 0253 nonempty_cells++; 0254 QString text; 0255 // FIXME: some formatting seems to be missing with cell.userInput(), e.g. 0256 // "208.00" in KSpread will be "208" in HTML (not always?!) 0257 bool link = false; 0258 0259 if (!cell.link().isEmpty()) { 0260 if (Util::localReferenceAnchor(cell.link())) { 0261 text = cell.userInput(); 0262 } else { 0263 text = " <A href=\"" + cell.link() + "\">" + cell.userInput() + "</A>"; 0264 link = true; 0265 } 0266 } else 0267 text = cell.displayText(); 0268 #if 0 0269 switch (cell.content()) { 0270 case Cell::Text: 0271 text = cell.userInput(); 0272 break; 0273 case Cell::RichText: 0274 case Cell::VisualFormula: 0275 text = cell.userInput(); // untested 0276 break; 0277 case Cell::Formula: 0278 cell.calc(true); // Incredible, cells are not calculated if the document was just opened 0279 text = cell.valueString(); 0280 break; 0281 } 0282 text = cell.prefix(currentrow, currentcolumn) + ' ' + text + ' ' 0283 + cell.postfix(currentrow, currentcolumn); 0284 #endif 0285 line += " <" + html_cell_tag + html_cell_options; 0286 if (text.isRightToLeft() != (sheet->layoutDirection() == Qt::RightToLeft)) 0287 line += QString(" dir=\"%1\" ").arg(text.isRightToLeft() ? "rtl" : "ltr"); 0288 const QColor bgcolor = style.backgroundColor(); 0289 if (bgcolor.isValid() && bgcolor.name() != "#ffffff") // change color only for non-white cells 0290 line += " bgcolor=\"" + bgcolor.name() + "\""; 0291 0292 switch ((Style::HAlign)cell.effectiveAlignX()) { 0293 case Style::Left: 0294 line += " align=\"" + html_left + "\""; 0295 break; 0296 case Style::Right: 0297 line += " align=\"" + html_right + "\""; 0298 break; 0299 case Style::Center: 0300 line += " align=\"" + html_center + "\""; 0301 break; 0302 case Style::HAlignUndefined: 0303 case Style::Justified: 0304 break; 0305 } 0306 switch ((Style::VAlign) style.valign()) { 0307 case Style::Top: 0308 line += " valign=\"" + html_top + "\""; 0309 break; 0310 case Style::Middle: 0311 line += " valign=\"" + html_middle + "\""; 0312 break; 0313 case Style::Bottom: 0314 line += " valign=\"" + html_bottom + "\""; 0315 break; 0316 case Style::VAlignUndefined: 0317 case Style::VJustified: 0318 case Style::VDistributed: 0319 break; 0320 } 0321 line += " width=\"" + QString::number(cell.width()) + "\""; 0322 line += " height=\"" + QString::number(cell.height()) + "\""; 0323 0324 if (cell.mergedXCells() > 0) { 0325 QString tmp; 0326 int extra_cells = cell.mergedXCells(); 0327 line += " colspan=\"" + tmp.setNum(extra_cells + 1) + "\""; 0328 currentcolumn += extra_cells; 0329 } 0330 text = text.trimmed(); 0331 if (!text.isEmpty() && text.at(0) == '!') { 0332 // this is supposed to be markup, just remove the '!': 0333 text = text.right(text.length() - 1); 0334 } else if (!link) { 0335 // Escape HTML characters. 0336 text.replace('&' , strAmp) 0337 .replace('<' , strLt) 0338 .replace('>' , strGt) 0339 .replace(' ' , nbsp); 0340 } 0341 line += ">\n"; 0342 0343 if (style.bold()) { 0344 text.insert(0, '<' + html_bold + '>'); 0345 text.append("</" + html_bold + '>'); 0346 } 0347 if (style.italic()) { 0348 text.insert(0, '<' + html_italic + '>'); 0349 text.append("</" + html_italic + '>'); 0350 } 0351 if (style.underline()) { 0352 text.insert(0, '<' + html_underline + '>'); 0353 text.append("</" + html_underline + '>'); 0354 } 0355 QColor textColor = style.fontColor(); 0356 if (textColor.isValid() && textColor.name() != "#000000") { // change color only for non-default text 0357 text.insert(0, "<font color=\"" + textColor.name() + "\">"); 0358 text.append("</font>"); 0359 } 0360 line += ' ' + text + 0361 "\n </" + html_cell_tag + ">\n"; 0362 } 0363 0364 if (nonempty_cells == 0 && nonempty_cells_prev == 0) { 0365 nonempty_cells_prev = nonempty_cells; 0366 // skip line if there's more than one empty line 0367 continue; 0368 } else { 0369 nonempty_cells_prev = nonempty_cells; 0370 str += emptyLines + 0371 '<' + html_row_tag + html_row_options + ">\n" + 0372 line + 0373 "</" + html_row_tag + '>'; 0374 emptyLines.clear(); 0375 // Append a CR, but in a temp string -> if no other real line, 0376 // then those will be dropped 0377 emptyLines += '\n'; 0378 } 0379 } 0380 str += "\n</" + html_table_tag + ">\n<br>\n"; 0381 } 0382 0383 void HTMLExport::createSheetSeparator(QString &str) 0384 { 0385 str += "<p align=\"" + html_center + "\"><a href=\"#__top\">" + i18n("Top") + "</a></p>\n" 0386 "<hr width=\"80%\">\n"; 0387 } 0388 0389 void HTMLExport::writeTOC(const QStringList &sheets, const QString &base, QString &str) 0390 { 0391 // don't create TOC for 1 sheet 0392 if (sheets.count() == 1) 0393 return; 0394 0395 str += "<p align=\"" + html_center + "\">\n"; 0396 0397 for (int i = 0 ; i < sheets.count() ; ++i) { 0398 str += "<a href=\""; 0399 0400 if (m_dialog->separateFiles()) { 0401 str += fileName(base, sheets[i], sheets.count() > 1); 0402 } else { 0403 str += '#' + sheets[i].toLower().trimmed(); 0404 } 0405 0406 str += "\">" + sheets[i] + "</a>\n"; 0407 if (i != sheets.count() - 1) 0408 str += " - "; 0409 } 0410 0411 str += "</p><hr width=\"80%\">\n"; 0412 } 0413 0414 QString HTMLExport::fileName(const QString &base, const QString &sheetName, bool multipleFiles) 0415 { 0416 QString fileName = base; 0417 if (m_dialog->separateFiles() && multipleFiles) { 0418 fileName += '-' + sheetName; 0419 } 0420 fileName += ".html"; 0421 0422 return fileName; 0423 } 0424 0425 void HTMLExport::detectFilledCells(Sheet *sheet, int &rows, int &columns) 0426 { 0427 int iMaxColumn = sheet->cellStorage()->columns(); 0428 int iMaxRow = sheet->cellStorage()->rows(); 0429 0430 rows = 0; 0431 columns = 0; 0432 0433 for (int currentrow = 1 ; currentrow <= iMaxRow ; ++currentrow) { 0434 Cell cell; 0435 int iUsedColumn = 0; 0436 for (int currentcolumn = 1 ; currentcolumn <= iMaxColumn ; currentcolumn++) { 0437 cell = Cell(sheet, currentcolumn, currentrow); 0438 QString text; 0439 if (!cell.isDefault() && !cell.isEmpty()) { 0440 iUsedColumn = currentcolumn; 0441 } 0442 } 0443 if (!cell.isNull()) 0444 iUsedColumn += cell.mergedXCells(); 0445 if (iUsedColumn > columns) 0446 columns = iUsedColumn; 0447 if (iUsedColumn > 0) 0448 rows = currentrow; 0449 } 0450 } 0451 0452 #include <htmlexport.moc>