Kate
htmlexporter.cpp
Go to the documentation of this file.
00001 00024 #include "htmlexporter.h" 00025 00026 #include <ktexteditor/document.h> 00027 00028 #include <QtGui/QTextDocument> 00029 00030 HTMLExporter::HTMLExporter(KTextEditor::View* view, QTextStream& output, const bool encapsulate) 00031 : AbstractExporter(view, output, encapsulate) 00032 { 00033 if ( m_encapsulate ) { 00034 // let's write the HTML header : 00035 m_output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl; 00036 m_output << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">" << endl; 00037 m_output << "<html xmlns=\"http://www.w3.org/1999/xhtml\">" << endl; 00038 m_output << "<head>" << endl; 00039 m_output << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />" << endl; 00040 m_output << "<meta name=\"Generator\" content=\"Kate, the KDE Advanced Text Editor\" />" << endl; 00041 // for the title, we write the name of the file (/usr/local/emmanuel/myfile.cpp -> myfile.cpp) 00042 m_output << "<title>" << view->document()->documentName() << "</title>" << endl; 00043 m_output << "</head>" << endl; 00044 m_output << "<body>" << endl; 00045 } 00046 00047 if ( !m_defaultAttribute ) { 00048 m_output << "<pre>" << endl; 00049 } else { 00050 m_output << QString("<pre style='%1%2%3%4'>") 00051 .arg(m_defaultAttribute->fontBold() ? "font-weight:bold;" : "") 00052 .arg(m_defaultAttribute->fontItalic() ? "text-style:italic;" : "") 00053 .arg("color:" + m_defaultAttribute->foreground().color().name() + ';') 00054 .arg("background-color:" + m_defaultAttribute->background().color().name() + ';') 00055 << endl; 00056 } 00057 } 00058 00059 HTMLExporter::~HTMLExporter() 00060 { 00061 m_output << "</pre>" << endl; 00062 00063 if ( m_encapsulate ) { 00064 m_output << "</body>" << endl; 00065 m_output << "</html>" << endl; 00066 } 00067 } 00068 00069 void HTMLExporter::openLine() 00070 { 00071 } 00072 00073 void HTMLExporter::closeLine(const bool lastLine) 00074 { 00075 if ( !lastLine ) { 00076 //we are inside a <pre>, so a \n is a new line 00077 m_output << "\n"; 00078 } 00079 } 00080 00081 void HTMLExporter::exportText(const QString& text, const KTextEditor::Attribute::Ptr& attrib) 00082 { 00083 if ( !attrib || !attrib->hasAnyProperty() || attrib == m_defaultAttribute ) { 00084 m_output << Qt::escape(text); 00085 return; 00086 } 00087 00088 if ( attrib->fontBold() ) { 00089 m_output << "<b>"; 00090 } 00091 if ( attrib->fontItalic() ) { 00092 m_output << "<i>"; 00093 } 00094 00095 bool writeForeground = attrib->hasProperty(QTextCharFormat::ForegroundBrush) 00096 && (!m_defaultAttribute || attrib->foreground().color() != m_defaultAttribute->foreground().color()); 00097 bool writeBackground = attrib->hasProperty(QTextCharFormat::BackgroundBrush) 00098 && (!m_defaultAttribute || attrib->background().color() != m_defaultAttribute->background().color()); 00099 00100 if ( writeForeground || writeBackground ) { 00101 m_output << QString("<span style='%1%2'>") 00102 .arg(writeForeground ? QString(QLatin1String("color:") + attrib->foreground().color().name() + QLatin1Char(';')) : QString()) 00103 .arg(writeBackground ? QString(QLatin1String("background:") + attrib->background().color().name() + QLatin1Char(';')) : QString()); 00104 } 00105 00106 m_output << Qt::escape(text); 00107 00108 if ( writeBackground || writeForeground ) { 00109 m_output << "</span>"; 00110 } 00111 if ( attrib->fontItalic() ) { 00112 m_output << "</i>"; 00113 } 00114 if ( attrib->fontBold() ) { 00115 m_output << "</b>"; 00116 } 00117 } 00118 00119 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference