测试语法高亮的 C# 代码的 html fragment 生成

测试语法高亮的 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 ( "
  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 ( "
" ) ;
      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 ) ;
    }
}
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值