测试语法高亮的 C# 代码的 html fragment 生成:
用csdn blog API 发布.
下面是:
public
class
HtmlWriter
{
static Dictionary < string , int > _colors ;
static int _colorNum ;
static StringBuilder _colorString ;
/// < summary >
///
/// < / summary >
/// < param name = " textArea " > < / param >
/// < returns > < / returns >
public static string GenerateRtf ( TextArea textArea )
{
_colors = new Dictionary < string , int > ( ) ;
_colorNum = 0 ;
_colorString = new StringBuilder ( ) ;
StringBuilder htmlBuilder = new StringBuilder ( ) ;
htmlBuilder . AppendLine ( "
return htmlBuilder . ToString ( ) ;
}
static string BuildFileContent ( TextArea textArea )
{
StringBuilder htmlBuilder = new StringBuilder ( ) ;
foreach ( ISelection selection in textArea . SelectionManager . SelectionCollection ) {
int selectionOffset = textArea . Document . PositionToOffset ( selection . StartPosition ) ;
int selectionEndOffset = textArea . Document . PositionToOffset ( selection . EndPosition ) ;
for ( int i = selection . StartPosition . Y ; i < = selection . EndPosition . Y ; + + i ) {
LineSegment line = textArea . Document . GetLineSegment ( i ) ;
int offset = line . Offset ;
bool lineStart = true ;
if ( line . Words = = null )
{
continue ;
}
int delimeter = line . DelimiterLength ;
foreach ( TextWord word in line . Words ) {
switch ( word . Type ) {
case TextWordType . Space :
if ( lineStart )
{
htmlBuilder . Append ( " " ) ;
}
else htmlBuilder . Append ( " " ) ;
+ + offset ;
break ;
case TextWordType . Tab :
if ( selection . ContainsOffset ( offset ) )
{
htmlBuilder . Append ( " " ) ;
}
+ + offset ;
break ;
case TextWordType . Word :
# region word
Color c = word . Color ;
if ( offset + word . Word . Length > selectionOffset & & offset < selectionEndOffset )
{
string colorstr = ColorToHtml ( c ) ;
// c . R + " , " + c . G + " , " + c . B ;
htmlBuilder . Append ( " ");
htmlBuilder.Append(" style=/"color:" + colorstr);
if (word.Font.Italic)
{
htmlBuilder.Append("; font-style:italic");
}
if (word.Font.Bold)
{
htmlBuilder.Append("; font-weight:bold");
}
htmlBuilder.Append("/">");
string printWord;
if (offset < selectionOffset)
{
printWord = word.Word.Substring(selectionOffset - offset);
}
else if (offset + word.Word.Length > selectionEndOffset) {
printWord = word.Word.Substring(0, (offset + word.Word.Length) - selectionEndOffset);
}
else
{
printWord = word.Word;
}
htmlBuilder.Append(printWord.Replace("<", "<").Replace(">", ">"));
htmlBuilder.Append(" " ) ;
}
offset + = word . Length ;
# endregion
break ;
}
lineStart = false ;
}
if ( offset < selectionEndOffset )
{
htmlBuilder . Append ( "
/> " ) ;
}
htmlBuilder . Append ( ' /n ' ) ;
}
}
// foreach ISelection
return htmlBuilder . ToString ( ) ;
}
static string ColorToHtml ( Color c )
{
return " # " + c . R . ToString ( " X2 " , null ) + c . G . ToString ( " X2 " , null ) + c . B . ToString ( " X2 " , null ) ;
}
}
{
static Dictionary < string , int > _colors ;
static int _colorNum ;
static StringBuilder _colorString ;
/// < summary >
///
/// < / summary >
/// < param name = " textArea " > < / param >
/// < returns > < / returns >
public static string GenerateRtf ( TextArea textArea )
{
_colors = new Dictionary < string , int > ( ) ;
_colorNum = 0 ;
_colorString = new StringBuilder ( ) ;
StringBuilder htmlBuilder = new StringBuilder ( ) ;
htmlBuilder . AppendLine ( "
style=/"
font-size:8pt;font-family:courier;border:2px
solid
blue/"
>
"
)
;
string fileContent = BuildFileContent ( textArea ) ;
// htmlBuilder . Append ( @ " /v iewkind4/u c1/p ard " ) ;
htmlBuilder . Append ( fileContent ) ;
htmlBuilder . AppendLine ( "
"
)
;
string fileContent = BuildFileContent ( textArea ) ;
// htmlBuilder . Append ( @ " /v iewkind4/u c1/p ard " ) ;
htmlBuilder . Append ( fileContent ) ;
htmlBuilder . AppendLine ( "
return htmlBuilder . ToString ( ) ;
}
static string BuildFileContent ( TextArea textArea )
{
StringBuilder htmlBuilder = new StringBuilder ( ) ;
foreach ( ISelection selection in textArea . SelectionManager . SelectionCollection ) {
int selectionOffset = textArea . Document . PositionToOffset ( selection . StartPosition ) ;
int selectionEndOffset = textArea . Document . PositionToOffset ( selection . EndPosition ) ;
for ( int i = selection . StartPosition . Y ; i < = selection . EndPosition . Y ; + + i ) {
LineSegment line = textArea . Document . GetLineSegment ( i ) ;
int offset = line . Offset ;
bool lineStart = true ;
if ( line . Words = = null )
{
continue ;
}
int delimeter = line . DelimiterLength ;
foreach ( TextWord word in line . Words ) {
switch ( word . Type ) {
case TextWordType . Space :
if ( lineStart )
{
htmlBuilder . Append ( " " ) ;
}
else htmlBuilder . Append ( " " ) ;
+ + offset ;
break ;
case TextWordType . Tab :
if ( selection . ContainsOffset ( offset ) )
{
htmlBuilder . Append ( " " ) ;
}
+ + offset ;
break ;
case TextWordType . Word :
# region word
Color c = word . Color ;
if ( offset + word . Word . Length > selectionOffset & & offset < selectionEndOffset )
{
string colorstr = ColorToHtml ( c ) ;
// c . R + " , " + c . G + " , " + c . B ;
htmlBuilder . Append ( " ");
htmlBuilder.Append(" style=/"color:" + colorstr);
if (word.Font.Italic)
{
htmlBuilder.Append("; font-style:italic");
}
if (word.Font.Bold)
{
htmlBuilder.Append("; font-weight:bold");
}
htmlBuilder.Append("/">");
string printWord;
if (offset < selectionOffset)
{
printWord = word.Word.Substring(selectionOffset - offset);
}
else if (offset + word.Word.Length > selectionEndOffset) {
printWord = word.Word.Substring(0, (offset + word.Word.Length) - selectionEndOffset);
}
else
{
printWord = word.Word;
}
htmlBuilder.Append(printWord.Replace("<", "<").Replace(">", ">"));
htmlBuilder.Append(" " ) ;
}
offset + = word . Length ;
# endregion
break ;
}
lineStart = false ;
}
if ( offset < selectionEndOffset )
{
htmlBuilder . Append ( "
/> " ) ;
}
htmlBuilder . Append ( ' /n ' ) ;
}
}
// foreach ISelection
return htmlBuilder . ToString ( ) ;
}
static string ColorToHtml ( Color c )
{
return " # " + c . R . ToString ( " X2 " , null ) + c . G . ToString ( " X2 " , null ) + c . B . ToString ( " X2 " , null ) ;
}
}