[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kwrite-devel
Subject:    [Kwrite-devel] [PATCH] nice feature for kate..
From:       Emmanuel Touzery <emmanuel.touzery () wanadoo ! fr>
Date:       2001-12-16 2:30:06
[Download RAW message or body]

Hello,

	Recently i discovered a nice feature from vim: export a syntax enlightened 
source in HTML.. great isn't it? i wrote a method for KateDocument that does 
that (it requires information that you can't have in a plugin).
	Here is it. I'd be glad if you integrate in kate.. NOTE: this method was 
written for kate from kde2.2.. that's why I didn't send a full-blown patch 
with new menus entries etc (well, appart from the fact i didn't do all of 
that in the first place ;o) ), since you'd probably have to throw half of it 
away.

	IMHO, and if you decide to take it in, the export to HTML should go in the 
submenu where you can choose the enlightenment mode (document->syntax 
enlightening). Also, a potential improvement would be to export the selection 
or the whole document, depending on the user's will, but I don't have the 
time for this at the moment... maybe soon :o)

	Attached: 
* the methods for KateDocument (I put the second one as static here. by the 
way, maybe KDE can do that for us -encode html entities- already?) 
* and the HTML exported for this code, so you get the idea :o)

Thank you for Kate,

emmanuel.
-- 
dessine moi un mouton..
le ciel est vide sans imagination..
["patch.html" (text/html)]

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Generator" content="Kate, the KDE Advanced Text Editor" />
</head>
<body><pre>
<font color="#000000"><br/>
</font><font color="#800000">bool</font><font color="#000000"> \
KateDocument::exportDocumentToHTML(</font><font color="#800000">const</font><font \
color="#000000"> QString&amp; targetFileName)<br/> {<br/>
	ASSERT(targetFileName != NULL);<br/>
	</font><font color="#808080"><i>//  start by opening the target file :<br/>
</font><font color="#000000"></i>	QFile outputFile( targetFileName );<br/>
	<b>if</b> ( !outputFile.open( IO_WriteOnly ) )<br/>
		<b>return</b> <b>false</b>; </font><font color="#808080"><i>// Error<br/>
<br/>
</font><font color="#000000"></i>	QTextStream outputStream(&amp;outputFile);<br/>
	</font><font color="#808080"><i>// let's write the HTML header :<br/>
</font><font color="#000000"></i>	outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;html&gt;"</font><font color="#000000"> &lt;&lt; endl;<br/>  \
outputStream &lt;&lt; </font><font color="#ff0000">"&lt;head&gt;"</font><font \
color="#000000"> &lt;&lt; endl;<br/>  outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;meta http-equiv=</font><font color="#ff00ff">\"</font><font \
color="#ff0000">Content-Type</font><font color="#ff00ff">\"</font><font \
color="#ff0000"> content=</font><font color="#ff00ff">\"</font><font \
color="#ff0000">text/html; charset=UTF-8</font><font color="#ff00ff">\"</font><font \
color="#ff0000"> /&gt;"</font><font color="#000000"> &lt;&lt; endl;<br/>  \
outputStream &lt;&lt; </font><font color="#ff0000">"&lt;meta name=</font><font \
color="#ff00ff">\"</font><font color="#ff0000">Generator</font><font \
color="#ff00ff">\"</font><font color="#ff0000"> content=</font><font \
color="#ff00ff">\"</font><font color="#ff0000">Kate, the KDE Advanced Text \
Editor</font><font color="#ff00ff">\"</font><font color="#ff0000"> /&gt;"</font><font \
color="#000000"> &lt;&lt; endl;<br/>  outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;/head&gt;"</font><font color="#000000"> &lt;&lt; endl;<br/> \
<br/>  outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;body&gt;&lt;pre&gt;"</font><font color="#000000"> &lt;&lt; endl; \
</font><font color="#808080"><i>// the &lt;pre&gt; is there for obvious reasons.. \
maybe there is a more elegant solution though?<br/> </font><font \
color="#000000"></i>	</font><font color="#808080"><i>// for each line :<br/> <br/>
</font><font color="#000000"></i>	</font><font color="#808080"><i>// some variables \
:<br/> </font><font color="#000000"></i>	</font><font \
color="#800000">bool</font><font color="#000000"> previousCharacterWasBold = \
<b>false</b>;<br/>  </font><font color="#800000">bool</font><font color="#000000"> \
previousCharacterWasItalic = <b>false</b>;<br/>  QColor \
previousCharacterColor(</font><font color="#0000ff">0</font><font \
color="#000000">,</font><font color="#0000ff">0</font><font \
color="#000000">,</font><font color="#0000ff">0</font><font color="#000000">); \
</font><font color="#808080"><i>// default color of HTML characters is black<br/> \
</font><font color="#000000"></i>	outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;font color=</font><font color="#ff00ff">\"</font><font \
color="#ff0000">#000000</font><font color="#ff00ff">\"</font><font \
color="#ff0000">&gt;"</font><font color="#000000">;<br/> <br/>
	<b>for</b> (</font><font color="#800000">int</font><font color="#000000"> \
curLine=</font><font color="#0000ff">0</font><font \
color="#000000">;curLine&lt;getTextLineCount();curLine++)<br/>  { </font><font \
color="#808080"><i>// html-export that line :<br/> </font><font \
color="#000000"></i>		TextLine::Ptr textLine = getTextLine(curLine);<br/>  \
ASSERT(textLine != NULL);<br/>  </font><font color="#808080"><i>// for each character \
of the line : (curPos is the position in the line)<br/> </font><font \
color="#000000"></i>		<b>for</b> (</font><font color="#800000">int</font><font \
color="#000000"> curPos=</font><font color="#0000ff">0</font><font \
color="#000000">;curPos&lt;textLine-&gt;length();curPos++)<br/>  {<br/>
			Attribute *charAttributes = &amp;m_attribs[textLine-&gt;getAttr(curPos)];<br/>
			ASSERT(charAttributes != NULL);<br/>
			</font><font color="#808080"><i>// let's give the color for that character :<br/>
</font><font color="#000000"></i>			<b>if</b> ( (charAttributes-&gt;col != \
previousCharacterColor))<br/>  {	</font><font color="#808080"><i>// the new character \
has a different color :<br/> <br/>
</font><font color="#000000"></i>				</font><font color="#808080"><i>// close the \
previous font tag :<br/> </font><font color="#000000"></i>				outputStream &lt;&lt; \
</font><font color="#ff0000">"&lt;/font&gt;"</font><font color="#000000">;<br/>  \
</font><font color="#808080"><i>// let's read that color :<br/> </font><font \
color="#000000"></i>				</font><font color="#800000">int</font><font color="#000000"> \
red, green, blue;<br/>  </font><font color="#808080"><i>// getting the red, green, \
blue values of the color :<br/> </font><font \
color="#000000"></i>				charAttributes-&gt;col.rgb(&amp;red, &amp;green, \
&amp;blue);<br/>  outputStream &lt;&lt; </font><font color="#ff0000">"&lt;font \
color=</font><font color="#ff00ff">\"</font><font color="#ff0000">#"<br/> \
</font><font color="#000000">							&lt;&lt; ( (red &lt; </font><font \
color="#008080">0x10</font><font color="#000000">)?</font><font \
color="#ff0000">"0"</font><font color="#000000">:</font><font \
color="#ff0000">""</font><font color="#000000">)  </font><font color="#808080"><i>// \
need to put 0f, NOT f for instance. don't touch 1f.<br/> </font><font \
color="#000000"></i>							&lt;&lt; QString::number(red, </font><font \
color="#0000ff">16</font><font color="#000000">) </font><font color="#808080"><i>// \
html wants the hex value here (hence the 16)<br/> </font><font \
color="#000000"></i>							&lt;&lt; ( (green &lt; </font><font \
color="#008080">0x10</font><font color="#000000">)?</font><font \
color="#ff0000">"0"</font><font color="#000000">:</font><font \
                color="#ff0000">""</font><font color="#000000">)<br/>
							&lt;&lt; QString::number(green, </font><font color="#0000ff">16</font><font \
color="#000000">)<br/>  &lt;&lt; ( (blue &lt; </font><font \
color="#008080">0x10</font><font color="#000000">)?</font><font \
color="#ff0000">"0"</font><font color="#000000">:</font><font \
                color="#ff0000">""</font><font color="#000000">)<br/>
							&lt;&lt; QString::number(blue, </font><font color="#0000ff">16</font><font \
                color="#000000">)<br/>
							&lt;&lt; </font><font color="#ff0000">"</font><font \
color="#ff00ff">\"</font><font color="#ff0000">&gt;"</font><font \
color="#000000">;<br/>  }<br/>
			</font><font color="#808080"><i>// bold status :<br/>
</font><font color="#000000"></i>			<b>if</b> (!previousCharacterWasBold &amp;&amp; \
charAttributes-&gt;bold)<br/>  </font><font color="#808080"><i>// we enter a bold \
section<br/> </font><font color="#000000"></i>				outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;b&gt;"</font><font color="#000000">;<br/>  <b>if</b> \
(previousCharacterWasBold &amp;&amp; !charAttributes-&gt;bold)<br/>  </font><font \
color="#808080"><i>// we leave a bold section<br/> </font><font \
color="#000000"></i>				outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;/b&gt;"</font><font color="#000000">;<br/>  </font><font \
color="#808080"><i>// italic status :<br/> </font><font \
color="#000000"></i>			<b>if</b> (!previousCharacterWasItalic &amp;&amp; \
charAttributes-&gt;italic)<br/>  </font><font color="#808080"><i>// we enter an \
italic section<br/> </font><font color="#000000"></i>				outputStream &lt;&lt; \
</font><font color="#ff0000">"&lt;i&gt;"</font><font color="#000000">;<br/>  \
<b>if</b> (previousCharacterWasItalic &amp;&amp; !charAttributes-&gt;italic)<br/>  \
</font><font color="#808080"><i>// we leave an italic section<br/> </font><font \
color="#000000"></i>				outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;/i&gt;"</font><font color="#000000">;<br/>  </font><font \
color="#808080"><i>// write the actual character :<br/> </font><font \
color="#000000"></i>			outputStream &lt;&lt; \
HTMLEncode(textLine-&gt;getChar(curPos));<br/> <br/>
			</font><font color="#808080"><i>// save status for the next character :<br/>
</font><font color="#000000"></i>			previousCharacterWasItalic = \
charAttributes-&gt;italic;<br/>  previousCharacterWasBold = \
charAttributes-&gt;bold;<br/>  previousCharacterColor = charAttributes-&gt;col;<br/>
		}<br/>
		</font><font color="#808080"><i>// finish the line :<br/>
</font><font color="#000000"></i>		outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;br/&gt;"</font><font color="#000000"> &lt;&lt; endl;<br/>  \
}<br/>  </font><font color="#808080"><i>// HTML document end :<br/>
</font><font color="#000000"></i>	outputStream &lt;&lt; </font><font \
color="#ff0000">"&lt;/pre&gt;&lt;/body&gt;"</font><font color="#000000">;<br/>  \
outputStream &lt;&lt; </font><font color="#ff0000">"&lt;/html&gt;"</font><font \
color="#000000">;<br/>  </font><font color="#808080"><i>// close the file :<br/>
</font><font color="#000000"></i>	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</font><font color="#0000ff">1</font><font \
color="#000000">())<br/>  {<br/>
	<b>case</b> </font><font color="#ff00ff">'&gt;'</font><font color="#000000">:<br/>
		<b>return</b> QString(</font><font color="#ff0000">"&amp;gt;"</font><font \
color="#000000">);<br/>  <b>case</b> </font><font color="#ff00ff">'&lt;'</font><font \
color="#000000">:<br/>  <b>return</b> QString(</font><font \
color="#ff0000">"&amp;lt;"</font><font color="#000000">);<br/>  <b>case</b> \
</font><font color="#ff00ff">'&amp;'</font><font color="#000000">:<br/>  \
<b>return</b> QString(</font><font color="#ff0000">"&amp;amp;"</font><font \
color="#000000">);<br/>  };<br/>
	<b>return</b> theChar;<br/>
}<br/>
<br/>
</body></pre></html>


["patch.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 << "<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.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