KTextEditor

htmlexporter.cpp
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
17static 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(QString::number(color.red()));
25 rgba.append(QLatin1Char(','));
26 rgba.append(QString::number(color.green()));
27 rgba.append(QLatin1Char(','));
28 rgba.append(QString::number(color.blue()));
29 rgba.append(QLatin1Char(','));
30 // this must be alphaF
31 rgba.append(QString::number(color.alphaF()));
32 rgba.append(QLatin1Char(')'));
33 return rgba;
34}
35
36HTMLExporter::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(m_defaultAttribute->fontBold() ? QStringLiteral("font-weight:bold;") : QString())
62 .arg(m_defaultAttribute->fontItalic() ? QStringLiteral("font-style:italic;") : QString())
63 .arg(QLatin1String("color:") + toHtmlRgbaString(m_defaultAttribute->foreground().color()) + QLatin1Char(';'))
64 .arg(QLatin1String("background-color:") + toHtmlRgbaString(m_defaultAttribute->background().color()) + QLatin1Char(';'))
65 << '\n';
66 }
67 m_output.flush();
68}
69
70HTMLExporter::~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
81void HTMLExporter::openLine()
82{
83}
84
85void 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
94void 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(QTextCharFormat::ForegroundBrush)
109 && (!m_defaultAttribute || attrib->foreground().color() != m_defaultAttribute->foreground().color());
110 bool writeBackground = attrib->hasProperty(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(writeForeground ? QString(QLatin1String("color:") + toHtmlRgbaString(attrib->foreground().color()) + QLatin1Char(';')) : QString())
116 .arg(writeBackground ? QString(QLatin1String("background:") + toHtmlRgbaString(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}
virtual QString documentName() const =0
Get this document's name.
virtual QString highlightingMode() const =0
Return the name of the currently used mode.
A text widget with KXMLGUIClient that represents a Document.
Definition view.h:244
virtual Document * document() const =0
Get the view's document, that means the view is a view of the returned document.
int alpha() const const
float alphaF() const const
int blue() const const
int green() const const
QString name(NameFormat format) const const
int red() const const
QString & append(QChar ch)
QString number(double n, char format, int precision)
QString toHtmlEscaped() const const
void flush()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.