bool KateDocument::exportDocumentToHTML(const QString& targetFileName)
{
ASSERT(targetFileName != NULL);
// start by opening the target file :
QFile outputFile( targetFileName );
if ( !outputFile.open( IO_WriteOnly ) )
return false;
// Error

QTextStream outputStream(&outputFile);
// let's write the HTML header :
outputStream << "<html>" << endl;
outputStream <<
"<head>" << endl;
outputStream <<
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />" << endl;
outputStream <<
"<meta name=\"Generator\" content=\"Kate, the KDE Advanced Text Editor\" />" << endl;
outputStream <<
"</head>" << endl;

outputStream <<
"<body><pre>" << endl; // the <pre> is there for obvious reasons.. maybe there is a more elegant solution though?
// for each line :

// some variables :
bool previousCharacterWasBold = false;
bool previousCharacterWasItalic = false;
QColor previousCharacterColor(
0,0,0); // default color of HTML characters is black
outputStream << "<font color=\"#000000\">";

for (
int curLine=0;curLine<getTextLineCount();curLine++)
{
// html-export that line :
TextLine::Ptr textLine = getTextLine(curLine);
ASSERT(textLine != NULL);
// for each character of the line : (curPos is the position in the line)
for (int curPos=0;curPos<textLine->length();curPos++)
{
Attribute *charAttributes = &m_attribs[textLine->getAttr(curPos)];
ASSERT(charAttributes != NULL);
// let's give the color for that character :
if ( (charAttributes->col != previousCharacterColor))
{
// the new character has a different color :

// close the previous font tag :
outputStream << "</font>";
// let's read that color :
int red, green, blue;
// getting the red, green, blue values of the color :
charAttributes->col.rgb(&red, &green, &blue);
outputStream <<
"<font color=\"#"
<< ( (red < 0x10)?"0":"") // need to put 0f, NOT f for instance. don't touch 1f.
<< QString::number(red, 16) // html wants the hex value here (hence the 16)
<< ( (green < 0x10)?"0":"")
<< QString::number(green,
16)
<< ( (blue <
0x10)?"0":"")
<< QString::number(blue,
16)
<<
"\">";
}
// bold status :
if (!previousCharacterWasBold && charAttributes->bold)
// we enter a bold section
outputStream << "<b>";
if (previousCharacterWasBold && !charAttributes->bold)
// we leave a bold section
outputStream << "</b>";
// italic status :
if (!previousCharacterWasItalic && charAttributes->italic)
// we enter an italic section
outputStream << "<i>";
if (previousCharacterWasItalic && !charAttributes->italic)
// we leave an italic section
outputStream << "</i>";
// write the actual character :
outputStream << HTMLEncode(textLine->getChar(curPos));

// save status for the next character :
previousCharacterWasItalic = charAttributes->italic;
previousCharacterWasBold = charAttributes->bold;
previousCharacterColor = charAttributes->col;
}
// finish the line :
outputStream << "<br/>" << endl;
}
// HTML document end :
outputStream << "</pre></body>";
outputStream <<
"</html>";
// close the file :
outputFile.close();
return (outputFile.status() == IO_Ok);
}

QString KateDocument::HTMLEncode(QChar theChar)
{
switch (theChar.latin
1())
{
case
'>':
return QString(
"&gt;");
case
'<':
return QString(
"&lt;");
case
'&':
return QString(
"&amp;");
};
return theChar;
}