[prev in list] [next in list] [prev in thread] [next in thread]
List: kwrite-devel
Subject: Re: [Kwrite-devel] [PATCH] nice feature for kate..
From: Emmanuel Touzery <emmanuel.touzery () wanadoo ! fr>
Date: 2001-12-16 14:51:09
[Download RAW message or body]
hello again,
here we are. the attached html file is XHTML-1.0 compliant according to
validator.w3.org (tell me if there's still anything wrong, though), and you
also have the relevant sources.
thank you,
emmanuel.
Le Dimanche 16 Décembre 2001 14:35, vous avez écrit :
> working on making it compliant. stay tuned!
>
> Le Dimanche 16 Décembre 2001 08:17, Daniel Naber a écrit :
> > On Sunday 16 December 2001 20:04, Emmanuel Touzery wrote:
> > > I know it's not really valid XHTML, actually i think that putting
> > > fonts colors and <b> and all in a <pre> tag is invalid no?
> >
> > Actually <font> is not allowed, but <i> and <b> are allowed. You can use
> > <span style="font-color:..."> instead of font.
> >
> > regards
> > daniel
--
dessine moi un mouton..
le ciel est vide sans imagination..
["patch2.html" (text/xml)]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Generator" content="Kate, the KDE Advanced Text Editor" />
<title>patch2.html</title>
</head>
<body><pre>
<span style='color=#000000'><br/>
</span><span style='color:#800000'>bool</span><span style='color:#000000'> \
KateDocument::exportDocumentToHTML(</span><span \
style='color:#800000'>const</span><span style='color:#000000'> QString& \
targetFileName)<br/> {<br/>
ASSERT(targetFileName != NULL);<br/>
</span><span style='color:#808080'><i>// start by opening the target file :<br/>
</i></span><span style='color:#000000'> QFile outputFile( targetFileName );<br/>
<b>if</b> ( !outputFile.open( IO_WriteOnly ) )<br/>
<b>return</b> <b>false</b>; </span><span style='color:#808080'><i>// Error<br/>
<br/>
</i></span><span style='color:#000000'> QTextStream \
outputStream(&outputFile);<br/> </span><span style='color:#808080'><i>// let's \
write the HTML header :<br/> </i></span><span style='color:#000000'> outputStream \
<< </span><span style='color:#ff0000'>"<?xml version=</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'>1.0</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'> encoding=</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'>UTF-8</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'>?>"</span><span \
style='color:#000000'> << endl;<br/> outputStream << </span><span \
style='color:#ff0000'>"<!DOCTYPE html PUBLIC </span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'>-//W3C//DTD XHTML 1.0 \
Strict//EN</span><span style='color:#ff00ff'>\"</span><span style='color:#ff0000'> \
</span><span style='color:#ff00ff'>\"</span><span \
style='color:#ff0000'>DTD/xhtml1-strict.dtd</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'>>"</span><span \
style='color:#000000'> << endl;<br/> outputStream << </span><span \
style='color:#ff0000'>"<html xmlns=</span><span \
style='color:#ff00ff'>\"</span><span \
style='color:#ff0000'>http://www.w3.org/1999/xhtml</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'>>"</span><span \
style='color:#000000'> << endl;<br/> outputStream << </span><span \
style='color:#ff0000'>"<head>"</span><span style='color:#000000'> << \
endl;<br/> outputStream << </span><span style='color:#ff0000'>"<meta \
http-equiv=</span><span style='color:#ff00ff'>\"</span><span \
style='color:#ff0000'>Content-Type</span><span style='color:#ff00ff'>\"</span><span \
style='color:#ff0000'> content=</span><span style='color:#ff00ff'>\"</span><span \
style='color:#ff0000'>text/html; charset=UTF-8</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'> />"</span><span \
style='color:#000000'> << endl;<br/> outputStream << </span><span \
style='color:#ff0000'>"<meta name=</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'>Generator</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'> content=</span><span \
style='color:#ff00ff'>\"</span><span style='color:#ff0000'>Kate, the KDE Advanced \
Text Editor</span><span style='color:#ff00ff'>\"</span><span style='color:#ff0000'> \
/>"</span><span style='color:#000000'> << endl;<br/> </span><span \
style='color:#808080'><i>// for the title, we write the name of the file \
(/usr/local/emmanuel/myfile.cpp -> myfile.cpp)<br/> </i></span><span \
style='color:#000000'> outputStream << </span><span \
style='color:#ff0000'>"<title>"</span><span style='color:#000000'> << \
targetFileName.right(targetFileName.length() - targetFileName.findRev(</span><span \
style='color:#ff00ff'>'/'</span><span style='color:#000000'>) -</span><span \
style='color:#0000ff'>1</span><span style='color:#000000'>) << </span><span \
style='color:#ff0000'>"</title>"</span><span style='color:#000000'> << \
endl;<br/> outputStream << </span><span \
style='color:#ff0000'>"</head>"</span><span style='color:#000000'> << \
endl;<br/> <br/>
outputStream << </span><span \
style='color:#ff0000'>"<body><pre>"</span><span style='color:#000000'> \
<< endl;<br/> </span><span style='color:#808080'><i>// for each line :<br/>
<br/>
</i></span><span style='color:#000000'> </span><span style='color:#808080'><i>// some \
variables :<br/> </i></span><span style='color:#000000'> </span><span \
style='color:#800000'>bool</span><span style='color:#000000'> \
previousCharacterWasBold = <b>false</b>;<br/> </span><span \
style='color:#800000'>bool</span><span style='color:#000000'> \
previousCharacterWasItalic = <b>false</b>;<br/> </span><span \
style='color:#808080'><i>// when entering a new color, we'll close all the <b> \
& <i> tags,<br/> </i></span><span style='color:#000000'> </span><span \
style='color:#808080'><i>// for HTML compliancy. that means right after that font \
tag, we'll<br/> </i></span><span style='color:#000000'> </span><span \
style='color:#808080'><i>// need to reinitialize the <b> and <i> \
tags.<br/> </i></span><span style='color:#000000'> </span><span \
style='color:#800000'>bool</span><span style='color:#000000'> needToReinitializeTags \
= <b>false</b>;<br/> QColor previousCharacterColor(</span><span \
style='color:#0000ff'>0</span><span style='color:#000000'>,</span><span \
style='color:#0000ff'>0</span><span style='color:#000000'>,</span><span \
style='color:#0000ff'>0</span><span style='color:#000000'>); </span><span \
style='color:#808080'><i>// default color of HTML characters is black<br/> \
</i></span><span style='color:#000000'> outputStream << </span><span \
style='color:#ff0000'>"<span style='color=#000000'>"</span><span \
style='color:#000000'>;<br/> <br/>
<b>for</b> (</span><span style='color:#800000'>int</span><span \
style='color:#000000'> curLine=</span><span style='color:#0000ff'>0</span><span \
style='color:#000000'>;curLine<getTextLineCount();curLine++)<br/> { </span><span \
style='color:#808080'><i>// html-export that line :<br/> </i></span><span \
style='color:#000000'> TextLine::Ptr textLine = getTextLine(curLine);<br/> \
ASSERT(textLine != NULL);<br/> </span><span style='color:#808080'><i>// for each \
character of the line : (curPos is the position in the line)<br/> </i></span><span \
style='color:#000000'> <b>for</b> (</span><span \
style='color:#800000'>int</span><span style='color:#000000'> curPos=</span><span \
style='color:#0000ff'>0</span><span \
style='color:#000000'>;curPos<textLine->length();curPos++)<br/> {<br/>
Attribute *charAttributes = &m_attribs[textLine->getAttr(curPos)];<br/>
ASSERT(charAttributes != NULL);<br/>
</span><span style='color:#808080'><i>// let's give the color for that character \
:<br/> </i></span><span style='color:#000000'> <b>if</b> ( (charAttributes->col \
!= previousCharacterColor))<br/> { </span><span style='color:#808080'><i>// the new \
character has a different color :<br/> </i></span><span \
style='color:#000000'> </span><span style='color:#808080'><i>// if we were in a \
bold or italic section, close it<br/> </i></span><span \
style='color:#000000'> <b>if</b> (previousCharacterWasBold)<br/>
outputStream << </span><span \
style='color:#ff0000'>"</b>"</span><span style='color:#000000'>;<br/> \
<b>if</b> (previousCharacterWasItalic)<br/>
outputStream << </span><span \
style='color:#ff0000'>"</i>"</span><span style='color:#000000'>;<br/> <br/>
</span><span style='color:#808080'><i>// close the previous font tag :<br/>
</i></span><span style='color:#000000'> outputStream << </span><span \
style='color:#ff0000'>"</span>"</span><span style='color:#000000'>;<br/> \
</span><span style='color:#808080'><i>// let's read that color :<br/> \
</i></span><span style='color:#000000'> </span><span \
style='color:#800000'>int</span><span style='color:#000000'> red, green, blue;<br/> \
</span><span style='color:#808080'><i>// getting the red, green, blue values of the \
color :<br/> </i></span><span \
style='color:#000000'> charAttributes->col.rgb(&red, &green, \
&blue);<br/> outputStream << </span><span style='color:#ff0000'>"<span \
style='color:#"<br/> </span><span style='color:#000000'> << ( (red < \
</span><span style='color:#008080'>0x10</span><span \
style='color:#000000'>)?</span><span style='color:#ff0000'>"0"</span><span \
style='color:#000000'>:</span><span style='color:#ff0000'>""</span><span \
style='color:#000000'>) </span><span style='color:#808080'><i>// need to put 0f, NOT \
f for instance. don't touch 1f.<br/> </i></span><span \
style='color:#000000'> << QString::number(red, </span><span \
style='color:#0000ff'>16</span><span style='color:#000000'>) </span><span \
style='color:#808080'><i>// html wants the hex value here (hence the 16)<br/> \
</i></span><span style='color:#000000'> << ( (green < </span><span \
style='color:#008080'>0x10</span><span style='color:#000000'>)?</span><span \
style='color:#ff0000'>"0"</span><span style='color:#000000'>:</span><span \
style='color:#ff0000'>""</span><span style='color:#000000'>)<br/>
<< QString::number(green, </span><span \
style='color:#0000ff'>16</span><span style='color:#000000'>)<br/> << ( (blue \
< </span><span style='color:#008080'>0x10</span><span \
style='color:#000000'>)?</span><span style='color:#ff0000'>"0"</span><span \
style='color:#000000'>:</span><span style='color:#ff0000'>""</span><span \
style='color:#000000'>)<br/>
<< QString::number(blue, </span><span \
style='color:#0000ff'>16</span><span style='color:#000000'>)<br/>
<< </span><span style='color:#ff0000'>"'>"</span><span \
style='color:#000000'>;<br/> </span><span style='color:#808080'><i>// we need to \
reinitialize the bold/italic status, since we closed all the tags<br/> \
</i></span><span style='color:#000000'> needToReinitializeTags = <b>true</b>;<br/> \
}<br/> </span><span style='color:#808080'><i>// bold status :<br/>
</i></span><span style='color:#000000'> <b>if</b> ( (needToReinitializeTags \
&& charAttributes->bold) ||<br/>
(!previousCharacterWasBold && charAttributes->bold) )<br/>
</span><span style='color:#808080'><i>// we enter a bold section<br/>
</i></span><span style='color:#000000'> outputStream << </span><span \
style='color:#ff0000'>"<b>"</span><span style='color:#000000'>;<br/> <b>if</b> \
( !needToReinitializeTags && (previousCharacterWasBold && \
!charAttributes->bold) )<br/> </span><span style='color:#808080'><i>// we leave a \
bold section<br/> </i></span><span style='color:#000000'> outputStream << \
</span><span style='color:#ff0000'>"</b>"</span><span \
style='color:#000000'>;<br/> <br/>
</span><span style='color:#808080'><i>// italic status :<br/>
</i></span><span style='color:#000000'> <b>if</b> ( (needToReinitializeTags \
&& charAttributes->italic) ||<br/>
(!previousCharacterWasItalic && charAttributes->italic) )<br/>
</span><span style='color:#808080'><i>// we enter an italic section<br/>
</i></span><span style='color:#000000'> outputStream << </span><span \
style='color:#ff0000'>"<i>"</span><span style='color:#000000'>;<br/> <b>if</b> \
( !needToReinitializeTags && (previousCharacterWasItalic && \
!charAttributes->italic) )<br/> </span><span style='color:#808080'><i>// we leave \
an italic section<br/> </i></span><span style='color:#000000'> outputStream \
<< </span><span style='color:#ff0000'>"</i>"</span><span \
style='color:#000000'>;<br/> <br/>
</span><span style='color:#808080'><i>// write the actual character :<br/>
</i></span><span style='color:#000000'> outputStream << \
HTMLEncode(textLine->getChar(curPos));<br/> <br/>
</span><span style='color:#808080'><i>// save status for the next character :<br/>
</i></span><span style='color:#000000'> previousCharacterWasItalic = \
charAttributes->italic;<br/> previousCharacterWasBold = \
charAttributes->bold;<br/> previousCharacterColor = charAttributes->col;<br/>
needToReinitializeTags = <b>false</b>;<br/>
}<br/>
</span><span style='color:#808080'><i>// finish the line :<br/>
</i></span><span style='color:#000000'> outputStream << </span><span \
style='color:#ff0000'>"<br/>"</span><span style='color:#000000'> << \
endl;<br/> }<br/>
</span><span style='color:#808080'><i>// HTML document end :<br/>
</i></span><span style='color:#000000'> outputStream << </span><span \
style='color:#ff0000'>"</span>"</span><span style='color:#000000'>; \
</span><span style='color:#808080'><i>// i'm guaranteed a span is started (i started \
one at the beginning of the output).<br/> </i></span><span \
style='color:#000000'> outputStream << </span><span \
style='color:#ff0000'>"</pre></body>"</span><span \
style='color:#000000'>;<br/> outputStream << </span><span \
style='color:#ff0000'>"</html>"</span><span style='color:#000000'>;<br/> \
</span><span style='color:#808080'><i>// close the file :<br/> </i></span><span \
style='color:#000000'> outputFile.close();<br/> <b>return</b> (outputFile.status() \
== IO_Ok);<br/> }<br/>
<br/>
QString KateDocument::HTMLEncode(QChar theChar)<br/>
{<br/>
<b>switch</b> (theChar.latin</span><span style='color:#0000ff'>1</span><span \
style='color:#000000'>())<br/> {<br/>
<b>case</b> </span><span style='color:#ff00ff'>'>'</span><span \
style='color:#000000'>:<br/> <b>return</b> QString(</span><span \
style='color:#ff0000'>"&gt;"</span><span style='color:#000000'>);<br/> \
<b>case</b> </span><span style='color:#ff00ff'>'<'</span><span \
style='color:#000000'>:<br/> <b>return</b> QString(</span><span \
style='color:#ff0000'>"&lt;"</span><span style='color:#000000'>);<br/> \
<b>case</b> </span><span style='color:#ff00ff'>'&'</span><span \
style='color:#000000'>:<br/> <b>return</b> QString(</span><span \
style='color:#ff0000'>"&amp;"</span><span style='color:#000000'>);<br/> };<br/>
<b>return</b> theChar;<br/>
}<br/>
</span></pre></body></html>
["patch2.cpp" (text/x-c++)]
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 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
outputStream << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \
\"DTD/xhtml1-strict.dtd\">" << endl; outputStream << "<html \
xmlns=\"http://www.w3.org/1999/xhtml\">" << 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; // for the title, we \
write the name of the file (/usr/local/emmanuel/myfile.cpp -> myfile.cpp) \
outputStream << "<title>" << targetFileName.right(targetFileName.length() - \
targetFileName.findRev('/') -1) << "</title>" << endl; outputStream << "</head>" << \
endl;
outputStream << "<body><pre>" << endl;
// for each line :
// some variables :
bool previousCharacterWasBold = false;
bool previousCharacterWasItalic = false;
// when entering a new color, we'll close all the <b> & <i> tags,
// for HTML compliancy. that means right after that font tag, we'll
// need to reinitialize the <b> and <i> tags.
bool needToReinitializeTags = false;
QColor previousCharacterColor(0,0,0); // default color of HTML characters is black
outputStream << "<span style='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 :
// if we were in a bold or italic section, close it
if (previousCharacterWasBold)
outputStream << "</b>";
if (previousCharacterWasItalic)
outputStream << "</i>";
// close the previous font tag :
outputStream << "</span>";
// 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 << "<span style='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)
<< "'>";
// we need to reinitialize the bold/italic status, since we closed all the tags
needToReinitializeTags = true;
}
// bold status :
if ( (needToReinitializeTags && charAttributes->bold) ||
(!previousCharacterWasBold && charAttributes->bold) )
// we enter a bold section
outputStream << "<b>";
if ( !needToReinitializeTags && (previousCharacterWasBold && \
!charAttributes->bold) ) // we leave a bold section
outputStream << "</b>";
// italic status :
if ( (needToReinitializeTags && charAttributes->italic) ||
(!previousCharacterWasItalic && charAttributes->italic) )
// we enter an italic section
outputStream << "<i>";
if ( !needToReinitializeTags && (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;
needToReinitializeTags = false;
}
// finish the line :
outputStream << "<br/>" << endl;
}
// HTML document end :
outputStream << "</span>"; // i'm guaranteed a span is started (i started one at \
the beginning of the output). outputStream << "</pre></body>";
outputStream << "</html>";
// close the file :
outputFile.close();
return (outputFile.status() == IO_Ok);
}
QString KateDocument::HTMLEncode(QChar theChar)
{
switch (theChar.latin1())
{
case '>':
return QString(">");
case '<':
return QString("<");
case '&':
return QString("&");
};
return theChar;
}
_______________________________________________
kwrite-devel mailing list
kwrite-devel@mail.kde.org
http://mail.kde.org/mailman/listinfo/kwrite-devel
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic