[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&amp; \
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(&amp;outputFile);<br/>  </span><span style='color:#808080'><i>// let's \
write the HTML header :<br/> </i></span><span style='color:#000000'>	outputStream \
&lt;&lt; </span><span style='color:#ff0000'>"&lt;?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'>?&gt;"</span><span \
style='color:#000000'> &lt;&lt; endl;<br/>  outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;!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'>&gt;"</span><span \
style='color:#000000'> &lt;&lt; endl;<br/>  outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;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'>&gt;"</span><span \
style='color:#000000'> &lt;&lt; endl;<br/>  outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;head&gt;"</span><span style='color:#000000'> &lt;&lt; \
endl;<br/>  outputStream &lt;&lt; </span><span style='color:#ff0000'>"&lt;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'> /&gt;"</span><span \
style='color:#000000'> &lt;&lt; endl;<br/>  outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;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'> \
/&gt;"</span><span style='color:#000000'> &lt;&lt; endl;<br/>  </span><span \
style='color:#808080'><i>// for the title, we write the name of the file \
(/usr/local/emmanuel/myfile.cpp -&gt; myfile.cpp)<br/> </i></span><span \
style='color:#000000'>	outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;title&gt;"</span><span style='color:#000000'> &lt;&lt; \
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'>) &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;/title&gt;"</span><span style='color:#000000'> &lt;&lt; \
endl;<br/>  outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;/head&gt;"</span><span style='color:#000000'> &lt;&lt; \
endl;<br/> <br/>
	outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;body&gt;&lt;pre&gt;"</span><span style='color:#000000'> \
&lt;&lt; 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 &lt;b&gt; \
&amp; &lt;i&gt; 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 &lt;b&gt; and &lt;i&gt; \
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 &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;span style='color=#000000'&gt;"</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&lt;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&lt;textLine-&gt;length();curPos++)<br/>  {<br/>
			Attribute *charAttributes = &amp;m_attribs[textLine-&gt;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-&gt;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 &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;/b&gt;"</span><span style='color:#000000'>;<br/>  \
                <b>if</b> (previousCharacterWasItalic)<br/>
					outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;/i&gt;"</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 &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;/span&gt;"</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-&gt;col.rgb(&amp;red, &amp;green, \
&amp;blue);<br/>  outputStream &lt;&lt; </span><span style='color:#ff0000'>"&lt;span \
style='color:#"<br/> </span><span style='color:#000000'>							&lt;&lt; ( (red &lt; \
</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'>							&lt;&lt; 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'>							&lt;&lt; ( (green &lt; </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/>
							&lt;&lt; QString::number(green, </span><span \
style='color:#0000ff'>16</span><span style='color:#000000'>)<br/>  &lt;&lt; ( (blue \
&lt; </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/>
							&lt;&lt; QString::number(blue, </span><span \
                style='color:#0000ff'>16</span><span style='color:#000000'>)<br/>
							&lt;&lt; </span><span style='color:#ff0000'>"'&gt;"</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 \
                &amp;&amp; charAttributes-&gt;bold) ||<br/>
			    (!previousCharacterWasBold &amp;&amp; charAttributes-&gt;bold) )<br/>
				</span><span style='color:#808080'><i>// we enter a bold section<br/>
</i></span><span style='color:#000000'>				outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;b&gt;"</span><span style='color:#000000'>;<br/>  <b>if</b> \
( !needToReinitializeTags &amp;&amp; (previousCharacterWasBold &amp;&amp; \
!charAttributes-&gt;bold) )<br/>  </span><span style='color:#808080'><i>// we leave a \
bold section<br/> </i></span><span style='color:#000000'>				outputStream &lt;&lt; \
</span><span style='color:#ff0000'>"&lt;/b&gt;"</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 \
                &amp;&amp; charAttributes-&gt;italic) ||<br/>
			     (!previousCharacterWasItalic &amp;&amp; charAttributes-&gt;italic) )<br/>
				</span><span style='color:#808080'><i>// we enter an italic section<br/>
</i></span><span style='color:#000000'>				outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;i&gt;"</span><span style='color:#000000'>;<br/>  <b>if</b> \
( !needToReinitializeTags &amp;&amp; (previousCharacterWasItalic &amp;&amp; \
!charAttributes-&gt;italic) )<br/>  </span><span style='color:#808080'><i>// we leave \
an italic section<br/> </i></span><span style='color:#000000'>				outputStream \
&lt;&lt; </span><span style='color:#ff0000'>"&lt;/i&gt;"</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 &lt;&lt; \
HTMLEncode(textLine-&gt;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-&gt;italic;<br/>  previousCharacterWasBold = \
charAttributes-&gt;bold;<br/>  previousCharacterColor = charAttributes-&gt;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 &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;br/&gt;"</span><span style='color:#000000'> &lt;&lt; \
endl;<br/>  }<br/>
	</span><span style='color:#808080'><i>// HTML document end :<br/>
</i></span><span style='color:#000000'>	outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;/span&gt;"</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 &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;/pre&gt;&lt;/body&gt;"</span><span \
style='color:#000000'>;<br/>  outputStream &lt;&lt; </span><span \
style='color:#ff0000'>"&lt;/html&gt;"</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'>'&gt;'</span><span \
style='color:#000000'>:<br/>  <b>return</b> QString(</span><span \
style='color:#ff0000'>"&amp;gt;"</span><span style='color:#000000'>);<br/>  \
<b>case</b> </span><span style='color:#ff00ff'>'&lt;'</span><span \
style='color:#000000'>:<br/>  <b>return</b> QString(</span><span \
style='color:#ff0000'>"&amp;lt;"</span><span style='color:#000000'>);<br/>  \
<b>case</b> </span><span style='color:#ff00ff'>'&amp;'</span><span \
style='color:#000000'>:<br/>  <b>return</b> QString(</span><span \
style='color:#ff0000'>"&amp;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("&gt;");
	case '<':
		return QString("&lt;");
	case '&':
		return QString("&amp;");
	};
	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