File indexing completed on 2024-04-28 07:46:22
0001 /* 0002 SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de> 0003 SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org> 0004 SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org> 0005 SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org> 0006 SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de> 0007 0008 SPDX-License-Identifier: LGPL-2.0-or-later 0009 */ 0010 0011 #include "htmlexporter.h" 0012 0013 #include <ktexteditor/document.h> 0014 0015 #include <QTextDocument> 0016 0017 static QString toHtmlRgbaString(const QColor &color) 0018 { 0019 if (color.alpha() == 0xFF) { 0020 return color.name(); 0021 } 0022 0023 QString rgba = QStringLiteral("rgba("); 0024 rgba.append(QString::number(color.red())); 0025 rgba.append(QLatin1Char(',')); 0026 rgba.append(QString::number(color.green())); 0027 rgba.append(QLatin1Char(',')); 0028 rgba.append(QString::number(color.blue())); 0029 rgba.append(QLatin1Char(',')); 0030 // this must be alphaF 0031 rgba.append(QString::number(color.alphaF())); 0032 rgba.append(QLatin1Char(')')); 0033 return rgba; 0034 } 0035 0036 HTMLExporter::HTMLExporter(KTextEditor::View *view, QTextStream &output, const bool encapsulate) 0037 : AbstractExporter(view, output, encapsulate) 0038 { 0039 if (m_encapsulate) { 0040 // let's write the HTML header : 0041 m_output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 0042 m_output << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">\n"; 0043 m_output << "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"; 0044 m_output << "<head>\n"; 0045 m_output << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"; 0046 m_output << "<meta name=\"Generator\" content=\"Kate, the KDE Advanced Text Editor\" />\n"; 0047 // for the title, we write the name of the file (/usr/local/emmanuel/myfile.cpp -> myfile.cpp) 0048 m_output << "<title>" << view->document()->documentName() << "</title>\n"; 0049 m_output << "</head>\n"; 0050 0051 // tell in comment which highlighting was used! 0052 m_output << "<!-- Highlighting: \"" << view->document()->highlightingMode() << "\" -->\n"; 0053 0054 m_output << "<body>\n"; 0055 } 0056 0057 if (!m_defaultAttribute) { 0058 m_output << "<pre>\n"; 0059 } else { 0060 m_output << QStringLiteral("<pre style='%1%2%3%4'>") 0061 .arg(m_defaultAttribute->fontBold() ? QStringLiteral("font-weight:bold;") : QString()) 0062 .arg(m_defaultAttribute->fontItalic() ? QStringLiteral("font-style:italic;") : QString()) 0063 .arg(QLatin1String("color:") + toHtmlRgbaString(m_defaultAttribute->foreground().color()) + QLatin1Char(';')) 0064 .arg(QLatin1String("background-color:") + toHtmlRgbaString(m_defaultAttribute->background().color()) + QLatin1Char(';')) 0065 << '\n'; 0066 } 0067 m_output.flush(); 0068 } 0069 0070 HTMLExporter::~HTMLExporter() 0071 { 0072 m_output << "</pre>\n"; 0073 0074 if (m_encapsulate) { 0075 m_output << "</body>\n"; 0076 m_output << "</html>\n"; 0077 } 0078 m_output.flush(); 0079 } 0080 0081 void HTMLExporter::openLine() 0082 { 0083 } 0084 0085 void HTMLExporter::closeLine(const bool lastLine) 0086 { 0087 if (!lastLine) { 0088 // we are inside a <pre>, so a \n is a new line 0089 m_output << "\n"; 0090 m_output.flush(); 0091 } 0092 } 0093 0094 void HTMLExporter::exportText(const QString &text, const KTextEditor::Attribute::Ptr &attrib) 0095 { 0096 if (!attrib || !attrib->hasAnyProperty() || attrib == m_defaultAttribute) { 0097 m_output << text.toHtmlEscaped(); 0098 return; 0099 } 0100 0101 if (attrib->fontBold()) { 0102 m_output << "<b>"; 0103 } 0104 if (attrib->fontItalic()) { 0105 m_output << "<i>"; 0106 } 0107 0108 bool writeForeground = attrib->hasProperty(QTextCharFormat::ForegroundBrush) 0109 && (!m_defaultAttribute || attrib->foreground().color() != m_defaultAttribute->foreground().color()); 0110 bool writeBackground = attrib->hasProperty(QTextCharFormat::BackgroundBrush) 0111 && (!m_defaultAttribute || attrib->background().color() != m_defaultAttribute->background().color()); 0112 0113 if (writeForeground || writeBackground) { 0114 m_output << QStringLiteral("<span style='%1%2'>") 0115 .arg(writeForeground ? QString(QLatin1String("color:") + toHtmlRgbaString(attrib->foreground().color()) + QLatin1Char(';')) : QString()) 0116 .arg(writeBackground ? QString(QLatin1String("background:") + toHtmlRgbaString(attrib->background().color()) + QLatin1Char(';')) 0117 : QString()); 0118 } 0119 0120 m_output << text.toHtmlEscaped(); 0121 0122 if (writeBackground || writeForeground) { 0123 m_output << "</span>"; 0124 } 0125 if (attrib->fontItalic()) { 0126 m_output << "</i>"; 0127 } 0128 if (attrib->fontBold()) { 0129 m_output << "</b>"; 0130 } 0131 m_output.flush(); 0132 }