自動提取C#類文件中的代碼註釋文字

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Runtime.Remoting" %>
<%@ Import Namespace="System.Runtime.Remoting.Lifetime" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat="server">
    string Folder = "C_BLL";//需要獲取的文件夾
    string pContent;

    string KeywordFormat(string inputString)
    {
        string ks = "public,private,static,class,virtual,override,new,void,string,int,bool,double,float,char,byte,DateTime,set,get";
        string[] ka = ks.Split(',');
        string outputString = inputString;
        foreach (string kg in ka)
        {
            outputString = outputString.Replace(kg + " ", "<font class=/"keyword/">" + kg + " </font>");
            outputString = outputString.Replace("(" + kg + ")", "(<font class=/"keyword/">" + kg + "</font>)");
            outputString = outputString.Replace(kg + "[", "<font class=/"keyword/">" + kg + "</font>[");
        }
        return outputString;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        pContent = "";
        // 用於保存結果
        StringBuilder sb = new StringBuilder();
        // 用於保存註釋
        StringBuilder sm = new StringBuilder();

        // 遍歷指定目錄下的全部 CS 文件
        DirectoryInfo csfile = new DirectoryInfo(Server.MapPath(Folder));
        foreach (System.IO.FileInfo fi in csfile.GetFiles())
        {
            int depth = 0;

            sb.AppendLine("<div style=/"padding:4px;margin:10px;padding-left:20px;border:solid 1px #AAAAAA;background-color:#F0F0F0/">");
            sb.AppendLine("<font class=/"file/"><b>" + Folder + "/" + fi.Name.ToString() + "</font> ");

            StreamReader TxtReader = new StreamReader(Server.MapPath(Folder + "/" + fi.Name.ToString()), System.Text.Encoding.Default);
            string FileContent = TxtReader.ReadToEnd();
            TxtReader.Close();

            // 按行分割
            string[] fLines = Regex.Split(FileContent, "/r/n");

            // 未進入註釋段
            int ok = 0;
            foreach (string ls in fLines)
            {
                string vls = ls;

                // 如果是 namespace
                if (vls.Length > 10)
                {
                    if (vls.Substring(0, 9) == "namespace")
                    {
                        sb.AppendLine(" ~~~ <font class=/"namespace/">" + vls.Substring(10) + "</font></b></div>");
                        depth = 1;
                        //sb.AppendLine("<div style=/"padding:4px;margin:10px;padding-left:" + (depth*40+20) + "px/">");
                        continue;
                    }
                }

                vls = vls.Trim();
                if (vls.Length >= 3)
                {
                    if (vls.Substring(0, 3) == "///")
                    {
                        //sb.AppendLine("<font class=/"summary/">");
                        // 如果是說明行,跳過即可
                        if (vls.Contains("<summary>"))
                        {
                            // 已進入註釋段
                            ok = 1;
                            sm.Append("功能:");
                            continue;
                        }

                        if (vls.Contains("</summary>"))
                        {
                            // 已進入註釋段
                            ok = 1;
                            continue;
                        }

                        string gls = vls.Replace("///", "").Trim();
                        if (gls.Length > 2)
                        {
                            vls = vls.Replace("<param name=/"", "參數:");
                            vls = vls.Replace("/">", " ");
                            vls = vls.Replace("</param>", "");
                            vls = vls.Replace("<returns></returns>", "");
                            vls = vls.Replace("<returns>", "返回:");
                            vls = vls.Replace("</returns>", "");
                            vls = vls.Replace("///", "").Trim();
                            if (vls != "")
                            {
                                sm.AppendLine(vls.Trim() + "<br>");
                            }
                        }
                        // 已進入註釋段
                        ok = 1;
                    }
                    else
                    {
                        // 如果已經進入註釋段
                        if (ok == 1)
                        {
                            // 如果是類
                            if (ls.Contains(" class "))
                            {
                                sb.AppendLine("<div style=/"padding:4px;margin:4px;padding-left:60px/">");
                                sb.AppendLine("<font class=/"summary/">" + sm.ToString() + "</font>");
                                sb.AppendLine("<font class=/"class/">" + KeywordFormat(ls.Trim()) + "</font>");
                                sb.AppendLine("</div>");
                            }
                            else
                            {
                                sb.AppendLine("<div style=/"padding:4px;margin:4px;padding-left:100px/">");
                                sb.AppendLine("<font class=/"summary/">" + sm.ToString() + "</font>");
                                string kls = ls.Trim();
                                if (kls.Substring(kls.Length - 1, 1) == ")")
                                    sb.AppendLine("<font class=/"function/">方法:" + KeywordFormat(kls) + "</font>");
                                else
                                    sb.AppendLine("<font class=/"property/">屬性:" + KeywordFormat(kls) + "</font>");
                                sb.AppendLine("</div>");
                            }
                            ok = 0;
                            sm.Remove(0, sm.Length);
                        }
                    }
                }
            }
            if (depth > 0) sb.AppendLine("</div>");
            if (depth > 1) sb.AppendLine("</div>");
            sb.AppendLine("</div>");
        }
        pContent = sb.ToString();
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>C#類地圖</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">
<!--

.keyword { font-size:12px;color:#0000EE;font-weight:normal; }
.file { font-size:16px;color:#000000; }
.namespace { font-size:16px;color:#990000; }
.class { font-size:14px;color:#AA0000;font-weight:bold; }
.function { font-size:12px;color:#333333;font-weight:bold; }
.property { font-size:12px;color:#888800;font-weight:bold; }
.summary { font-size:12px;color:#888888; }

-->
</style>
</head>
<body style="font-size:12px;font-family:arial,宋體;line-height:16px;">
<% =pContent %>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章