| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de> |
| 3 | SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org> |
| 4 | SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org> |
| 5 | SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org> |
| 6 | SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de> |
| 7 | |
| 8 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | #include "htmlexporter.h" |
| 12 | |
| 13 | #include <ktexteditor/document.h> |
| 14 | |
| 15 | #include <QTextDocument> |
| 16 | |
| 17 | static QString toHtmlRgbaString(const QColor &color) |
| 18 | { |
| 19 | if (color.alpha() == 0xFF) { |
| 20 | return color.name(); |
| 21 | } |
| 22 | |
| 23 | QString rgba = QStringLiteral("rgba(" ); |
| 24 | rgba.append(s: QString::number(color.red())); |
| 25 | rgba.append(c: QLatin1Char(',')); |
| 26 | rgba.append(s: QString::number(color.green())); |
| 27 | rgba.append(c: QLatin1Char(',')); |
| 28 | rgba.append(s: QString::number(color.blue())); |
| 29 | rgba.append(c: QLatin1Char(',')); |
| 30 | // this must be alphaF |
| 31 | rgba.append(s: QString::number(color.alphaF())); |
| 32 | rgba.append(c: QLatin1Char(')')); |
| 33 | return rgba; |
| 34 | } |
| 35 | |
| 36 | HTMLExporter::HTMLExporter(KTextEditor::View *view, QTextStream &output, const bool encapsulate) |
| 37 | : AbstractExporter(view, output, encapsulate) |
| 38 | { |
| 39 | if (m_encapsulate) { |
| 40 | // let's write the HTML header : |
| 41 | m_output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ; |
| 42 | m_output << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">\n" ; |
| 43 | m_output << "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" ; |
| 44 | m_output << "<head>\n" ; |
| 45 | m_output << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" ; |
| 46 | m_output << "<meta name=\"Generator\" content=\"Kate, the KDE Advanced Text Editor\" />\n" ; |
| 47 | // for the title, we write the name of the file (/usr/local/emmanuel/myfile.cpp -> myfile.cpp) |
| 48 | m_output << "<title>" << view->document()->documentName() << "</title>\n" ; |
| 49 | m_output << "</head>\n" ; |
| 50 | |
| 51 | // tell in comment which highlighting was used! |
| 52 | m_output << "<!-- Highlighting: \"" << view->document()->highlightingMode() << "\" -->\n" ; |
| 53 | |
| 54 | m_output << "<body>\n" ; |
| 55 | } |
| 56 | |
| 57 | if (!m_defaultAttribute) { |
| 58 | m_output << "<pre>\n" ; |
| 59 | } else { |
| 60 | m_output << QStringLiteral("<pre style='%1%2%3%4'>" ) |
| 61 | .arg(a: m_defaultAttribute->fontBold() ? QStringLiteral("font-weight:bold;" ) : QString()) |
| 62 | .arg(a: m_defaultAttribute->fontItalic() ? QStringLiteral("font-style:italic;" ) : QString()) |
| 63 | .arg(a: QLatin1String("color:" ) + toHtmlRgbaString(color: m_defaultAttribute->foreground().color()) + QLatin1Char(';')) |
| 64 | .arg(a: QLatin1String("background-color:" ) + toHtmlRgbaString(color: m_defaultAttribute->background().color()) + QLatin1Char(';')) |
| 65 | << '\n'; |
| 66 | } |
| 67 | m_output.flush(); |
| 68 | } |
| 69 | |
| 70 | HTMLExporter::~HTMLExporter() |
| 71 | { |
| 72 | m_output << "</pre>\n" ; |
| 73 | |
| 74 | if (m_encapsulate) { |
| 75 | m_output << "</body>\n" ; |
| 76 | m_output << "</html>\n" ; |
| 77 | } |
| 78 | m_output.flush(); |
| 79 | } |
| 80 | |
| 81 | void HTMLExporter::openLine() |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | void HTMLExporter::closeLine(const bool lastLine) |
| 86 | { |
| 87 | if (!lastLine) { |
| 88 | // we are inside a <pre>, so a \n is a new line |
| 89 | m_output << "\n" ; |
| 90 | m_output.flush(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void HTMLExporter::exportText(const QString &text, const KTextEditor::Attribute::Ptr &attrib) |
| 95 | { |
| 96 | if (!attrib || !attrib->hasAnyProperty() || attrib == m_defaultAttribute) { |
| 97 | m_output << text.toHtmlEscaped(); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if (attrib->fontBold()) { |
| 102 | m_output << "<b>" ; |
| 103 | } |
| 104 | if (attrib->fontItalic()) { |
| 105 | m_output << "<i>" ; |
| 106 | } |
| 107 | |
| 108 | bool writeForeground = attrib->hasProperty(propertyId: QTextCharFormat::ForegroundBrush) |
| 109 | && (!m_defaultAttribute || attrib->foreground().color() != m_defaultAttribute->foreground().color()); |
| 110 | bool writeBackground = attrib->hasProperty(propertyId: QTextCharFormat::BackgroundBrush) |
| 111 | && (!m_defaultAttribute || attrib->background().color() != m_defaultAttribute->background().color()); |
| 112 | |
| 113 | if (writeForeground || writeBackground) { |
| 114 | m_output << QStringLiteral("<span style='%1%2'>" ) |
| 115 | .arg(a: writeForeground ? QString(QLatin1String("color:" ) + toHtmlRgbaString(color: attrib->foreground().color()) + QLatin1Char(';')) : QString()) |
| 116 | .arg(a: writeBackground ? QString(QLatin1String("background:" ) + toHtmlRgbaString(color: attrib->background().color()) + QLatin1Char(';')) |
| 117 | : QString()); |
| 118 | } |
| 119 | |
| 120 | m_output << text.toHtmlEscaped(); |
| 121 | |
| 122 | if (writeBackground || writeForeground) { |
| 123 | m_output << "</span>" ; |
| 124 | } |
| 125 | if (attrib->fontItalic()) { |
| 126 | m_output << "</i>" ; |
| 127 | } |
| 128 | if (attrib->fontBold()) { |
| 129 | m_output << "</b>" ; |
| 130 | } |
| 131 | m_output.flush(); |
| 132 | } |
| 133 | |