예제 #1
0
        /// <summary>
        /// 转换指定文本特殊字符,并消除上下空行和左空白
        /// </summary>
        /// <param name="para">待转换的字符串</param>
        /// <param name="trimLength">消除左空白的长度</param>
        /// <returns>转换后的字符串</returns>
        static public string TrimParagraph(string para, int trimLength)
        {
            if (string.IsNullOrEmpty(para))
            {
                return(para);
            }
            HtmlText ret = new HtmlText();

            ret.AppendParagraphAndTrim(para, trimLength);
            return(ret.ToString());
        }
        /// <summary>
        /// 生成命名空间页面
        /// </summary>
        /// <param name="typeItems">命名空间中的所有类型</param>
        /// <param name="ns">命名空间包含信息(名称,页面文件名等)</param>
        protected override void CreateNamespacePage(IGetTypes typeItems, ContentTreeItem ns)
        {
            HtmlText content = new HtmlText();

            foreach (string category in typeItems.GetAllCategory())
            {
                string   title      = NameToTitleDictionary[category] as string;
                HtmlText secContent = new HtmlText();
                foreach (ContentTreeItem typeItem in typeItems.GetMembers(category))
                {
                    Type             type = typeItem.Info as Type;
                    MemberXmlElement data = DataProvider.GetMemberXmlNode(type);
                    string           summary;
                    summary = GetChildNodeInnerText(data, "summary");
                    if (string.IsNullOrEmpty(summary))
                    {
                        summary = "&nbsp;";
                    }
                    string strRow = tempTypeTable_Row.Render(
                        tempImg.Render(NameToTitleDictionary[category], category + ".png"),
                        typeItem.FileName,
                        HtmlText.HtmlFormat(typeItem.Name),
                        summary
                        );
                    secContent.AppendPre(strRow);
                }
                content.AppendPre(CreateTableSection(title, title, secContent.ToString()));
            }

            Hashtable values = new Hashtable();

            values["CollapseAll"] = Resources.strCollapseAll;
            values["ExpandAll"]   = Resources.strExpandAll;
            values["PIC"]         = Resources.strPic;
            values["Title"]       = HtmlText.HtmlFormat(ns.Name);
            values["Summary"]     = "";
            values["Content"]     = content.ToString();
            values["Encoding"]    = TargetEncoding.HeaderName;
            tempPage.SaveAs(HtmlFileDirectory + "\\" + ns.FileName, TargetEncoding, values);
        }
        /// <summary>
        /// 将XML注释所有子级转换成HTML
        /// </summary>
        /// <param name="node">指定的XML注释</param>
        /// <param name="xmlFile">elem所在的xml文件的路径</param>
        /// <returns>所有子级的html</returns>
        protected string GetInnerTags(XmlElement node, string xmlFile)
        {
            HtmlText strText = new HtmlText();

            foreach (XmlNode subNode in node.ChildNodes)
            {
                switch (subNode.NodeType)
                {
                case XmlNodeType.Text:
                {
                    strText.AppendParagraphAndTrim(subNode.InnerText);
                    break;
                }

                case XmlNodeType.Element:
                {
                    strText.AppendPre(GetTag(subNode as XmlElement, xmlFile));
                    break;
                }
                }
            }
            return(strText.ToString());
        }
        /// <summary>
        /// 将XML注释转换成页面上的HTML代码,继承类可以重写此方法已扩展XML注释标志
        /// </summary>
        /// <param name="elem">要转成HTML的XML元素</param>
        /// <param name="xmlFile">elem所在的XML文件的路径</param>
        /// <returns>XML注释对应的的HTML代码</returns>
        protected virtual string GetTag(XmlElement elem, String xmlFile)
        {
            HtmlText strText = new HtmlText();

            switch (elem.Name)
            {
            case "code":
            {
                if (elem.HasAttribute("src"))
                {
                    string encoding = "";
                    if (elem.HasAttribute("encoding"))
                    {
                        encoding = elem.GetAttribute("encoding");
                    }
                    else
                    {
                        encoding = "GBK";
                    }
                    StreamReader sr = null;
                    try
                    {
                        sr = new StreamReader(
                            xmlFile + "\\" + elem.GetAttribute("src"),
                            Encoding.GetEncoding(encoding)
                            );
                    }
                    catch
                    {
                        break;
                    }
                    try
                    {
                        HtmlText code     = new HtmlText(sr.ReadToEnd());
                        string   language = elem.GetAttribute("language");
                        if (string.IsNullOrEmpty(language))
                        {
                            language = "C#";
                        }
                        strText.AppendPre(
                            tempExample_Code.Render(
                                language,
                                code
                                )
                            );
                    }
                    finally
                    {
                        sr.Close();
                    }
                }
                else
                {
                    HtmlText code = new HtmlText();
                    code.AppendParagraphAndTrim(elem.InnerText);
                    strText.AppendPre(
                        tempExample_Code.Render(
                            elem.GetAttribute("language"),
                            code
                            )
                        );
                }
                break;
            }

            case "see":
            {
                string cref = elem.GetAttribute("cref");
                if (cref != null)
                {
                    string href  = GetFileName(cref);
                    int    index = cref.LastIndexOfAny(new char[] { '.', ':' });
                    if (string.IsNullOrEmpty(href))
                    {
                        strText.Append(cref.Substring(index + 1));
                    }
                    else
                    {
                        strText.AppendPre(string.Format("<a target=\"_self\" href=\"{0}\">{1}</a>", href, cref.Substring(index + 1)));
                    }
                }
                break;
            }

            case "para":
            {
                strText.AppendPre("<p>");
                strText.AppendPre(GetInnerTags(elem as XmlElement, xmlFile));
                strText.AppendPre("</p>");
                break;
            }

            case "value":
            case "returns":
            case "summary":
            case "param":
            case "remarks":
            {
                strText.AppendPre(GetInnerTags(elem, xmlFile));
                break;
            }

            default:
            {
                if (!string.IsNullOrEmpty(elem.InnerXml))
                {
                    strText.AppendPre(GetElementBeginTag(elem));
                    strText.AppendPre(GetInnerTags(elem as XmlElement, xmlFile));
                    strText.AppendPre(GetElementEndTag(elem));
                }
                else
                {
                    strText.AppendPre(elem.OuterXml);
                }
                break;
            }
            }
            return(strText.ToString());
        }
        /// <summary>
        /// 创建成员页面
        /// </summary>
        /// <param name="memberItem">成员的信息</param>
        protected override void CreateMemberPage(ContentTreeItem memberItem)
        {
            MemberInfo       info       = memberItem.Info;
            MemberXmlElement memberData = DataProvider.GetMemberXmlNode(info);

            HtmlText content = new HtmlText();
            int      secID   = 0;

            foreach (PageSection section in MemberPageSections)
            {
                string secText = null;
                switch (section.SectionType)
                {
                case PageSectionType.Syntax:
                {
                    string parameters = "";
                    string ret        = "";
                    string val        = "";

                    if (memberData != null)
                    {
                        XmlNodeList ns;

                        ns = memberData.Data.GetElementsByTagName("param");
                        foreach (XmlElement p in ns)
                        {
                            parameters += tempParam.Render(
                                HtmlText.HtmlFormat(p.GetAttribute("name")),
                                GetInnerTags(p, memberData.Owner)
                                );
                        }

                        ret = GetChildNodeInnerText(memberData, "returns");
                        val = GetChildNodeInnerText(memberData, "value");
                    }

                    HtmlText secContent = new HtmlText(), declaration = new HtmlText();

                    declaration.AppendPre(HtmlText.HtmlFormat(CreateMemberDeclaration(info, memberData)));

                    secContent.AppendPre(tempDeclaration.Render("C#", declaration));

                    switch (info.MemberType)
                    {
                    case MemberTypes.Constructor:
                    case MemberTypes.Method:
                    {
                        if (!string.IsNullOrEmpty(parameters))
                        {
                            secContent.AppendPre(tempParamsBlock.Render(Resources.String46, parameters));
                        }
                        if (!string.IsNullOrEmpty(ret))
                        {
                            secContent.AppendPre(tempParamsBlock.Render(Resources.String47, ret));
                        }
                        break;
                    }

                    case MemberTypes.Property:
                    {
                        if (!string.IsNullOrEmpty(val))
                        {
                            secContent.AppendPre(tempParamsBlock.Render(Resources.String48, val));
                        }
                        break;
                    }

                    default:
                        break;
                    }

                    Hashtable sectionValues = new Hashtable();
                    sectionValues["ID"]      = "Syntex";
                    sectionValues["Title"]   = section.Name;
                    sectionValues["Content"] = secContent.ToString();
                    secText = tempSection.Render(sectionValues);
                    break;
                }

                case PageSectionType.Remarks:
                {
                    if (memberData != null)
                    {
                        string remarks = GetChildNodeInnerText(memberData, "remarks");
                        if (!string.IsNullOrEmpty(remarks))
                        {
                            Hashtable sectionValues = new Hashtable();
                            sectionValues["ID"]      = "Remarks";
                            sectionValues["Title"]   = section.Name;
                            sectionValues["Content"] = tempRemarks.Render(remarks);
                            secText = tempSection.Render(sectionValues);
                        }
                    }
                    break;
                }

                case PageSectionType.Exception:
                {
                    if (memberData != null)
                    {
                        XmlNodeList ns;
                        ns = memberData.Data.GetElementsByTagName("exception");
                        HtmlText ecx = new HtmlText();
                        foreach (XmlElement n in ns)
                        {
                            int index = n.GetAttribute("cref").LastIndexOfAny(new char[] { ':', '.', '+' });
                            ecx.AppendPre(
                                tempExceptionTable_Row.Render(
                                    n.GetAttribute("cref").Substring(index + 1),
                                    n.InnerText
                                    )
                                );
                        }
                        if (ecx.Length > 0)
                        {
                            Hashtable sectionValues = new Hashtable();
                            sectionValues["ID"]      = "Exception";
                            sectionValues["Title"]   = section.Name;
                            sectionValues["Content"] = tempExceptionTable.Render(ecx);
                            secText = tempSection.Render(sectionValues);
                        }
                    }
                    break;
                }

                case PageSectionType.Example:
                {
                    if (memberData != null)
                    {
                        string exampleText = GetChildNodeInnerText(memberData, "example");
                        if (!string.IsNullOrEmpty(exampleText))
                        {
                            Hashtable sectionValues = new Hashtable();
                            sectionValues["ID"]      = "Exception";
                            sectionValues["Title"]   = section.Name;
                            sectionValues["Content"] = exampleText;
                            secText = tempSection.Render(sectionValues);
                        }
                    }
                    break;
                }

                case PageSectionType.FromXML:
                {
                    if (memberData != null)
                    {
                        string text = GetChildNodeInnerText(memberData, section.XmlSource);
                        if (!string.IsNullOrEmpty(text))
                        {
                            Hashtable sectionValues = new Hashtable();
                            sectionValues["ID"]      = "SEC" + secID.ToString("00");
                            sectionValues["Title"]   = section.Name;
                            sectionValues["Content"] = text;
                            secText = tempSection.Render(sectionValues);
                        }
                    }
                    break;
                }
                }
                if (!string.IsNullOrEmpty(secText))
                {
                    content.AppendPre(secText);
                }
                secID++;
            }

            string summary = GetChildNodeInnerText(memberData, "summary");

            Hashtable values = new Hashtable();

            values["CollapseAll"] = Resources.strCollapseAll;
            values["ExpandAll"]   = Resources.strExpandAll;
            values["PIC"]         = Resources.strPic;
            if (info.MemberType == MemberTypes.Constructor)
            {
                values["Title"] = HtmlText.HtmlFormat(memberItem.Name) + " " + NameToTypeDictionary[memberItem.Parent.Name];
            }
            else
            {
                values["Title"] = HtmlText.HtmlFormat(info.DeclaringType.Name + "." + info.Name) + " " + NameToTypeDictionary[memberItem.Parent.Name];
            }
            values["Summary"]  = summary;
            values["Content"]  = content;
            values["Encoding"] = TargetEncoding.HeaderName;

            tempPage.SaveAs(
                HtmlFileDirectory + "\\" + memberItem.FileName,
                TargetEncoding,
                values
                );
        }
        private string GetTypeMemberCategorySection(IGetMembers memberItems, Type type, MemberXmlElement typeData, string category)
        {
            if (memberItems.GetMembers(category) != null)
            {
                string   secText        = null;
                string   sectionTitle   = NameToTitleDictionary[category] as string;
                HtmlText sectionContent = new HtmlText();
                foreach (ContentTreeItem memberItem in memberItems.GetMembers(category))
                {
                    MemberXmlElement memberData = DataProvider.GetMemberXmlNode(memberItem.Info);
                    string           summary    = "";
                    summary = GetChildNodeInnerText(memberData, "summary");
                    if (memberItem.Info.DeclaringType != type)
                    {
                        summary += string.Format(Resources.String56, memberItem.Info.DeclaringType.Name);
                    }
                    if (string.IsNullOrEmpty(summary))
                    {
                        summary = "&nbsp;";
                    }
                    HtmlText imgs = new HtmlText();
                    imgs.AppendPre(tempImg.Render(NameToTitleDictionary[category], category + ".bmp"));
                    if (IsStatic(memberItem.Info))
                    {
                        imgs.AppendPre("&nbsp;");
                        imgs.AppendPre(tempImg.Render("static", "static.bmp"));
                    }

                    string mname = "";
                    mname += HtmlText.HtmlFormat(memberItem.Name);
                    MethodInfo method = memberItem.Info as MethodInfo;
                    //if (method != null)
                    if (method != null)
                    {
                        //泛型方法
                        //if (method.IsGenericMethod == true)
                        //{

                        if (memberData != null && memberData.Data != null)
                        {
                            XmlNodeList lst = memberData.Data.GetElementsByTagName("typeparam");

                            if (lst.Count > 0)
                            {
                                mname += HtmlText.HtmlFormat("<");
                                mname += (lst[0] as XmlElement).GetAttribute("name");
                                for (int i = 1; i < lst.Count; i++)
                                {
                                    mname += ("," + (lst[i] as XmlElement).GetAttribute("name"));
                                }
                                //泛型方法处理
                                //name = name.Replace("``" + elem.GetElementsByTagName("typeparam").Count, "");
                                mname += HtmlText.HtmlFormat(">");
                            }
                        }
                        //}

                        ParameterInfo[] parameters = method.GetParameters();
                        if (parameters.Length > 0)
                        {
                            string strParams = "";
                            foreach (ParameterInfo param in parameters)
                            {
                                if (string.IsNullOrEmpty(strParams))
                                {
                                    strParams = "" + SimpleParamName(DocumentBuilderUtility.GetTypeDefinition(param.ParameterType)) + "";
                                }
                                else
                                {
                                    strParams += "," + SimpleParamName(DocumentBuilderUtility.GetTypeDefinition(param.ParameterType)) + "";
                                }
                            }
                            mname += ("(" + strParams + ")");
                        }
                        else
                        {
                            mname += ("()");
                        }
                    }


                    string strRow = tempTypeTable_Row.Render(
                        imgs,
                        memberItem.FileName,
                        //HtmlText.HtmlFormat(memberItem.Name),
                        mname,
                        summary
                        );
                    sectionContent.AppendPre(strRow);
                }
                if (sectionContent.Length > 0)
                {
                    secText = CreateTableSection(category, sectionTitle, sectionContent.ToString());
                }
                return(secText);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// 生成类型页面
        /// </summary>
        /// <param name="memberItems">该类型包含的所有成员</param>
        /// <param name="typeItem">类型包含信息(名称,页面文件名等)</param>
        protected override void CreateTypePage(IGetMembers memberItems, ContentTreeItem typeItem)
        {
            HtmlText pageContent = new HtmlText();
            Type     type        = typeItem.Info as Type;

            MemberXmlElement typeData = DataProvider.GetMemberXmlNode(type);
            int secID = 0;

            foreach (PageSection section in TypePageSections)
            {
                string secText = null;
                switch (section.SectionType)
                {
                case PageSectionType.Constructor:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "Constructor");
                    break;

                case PageSectionType.PublicMethod:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PublicMethod");
                    break;

                case PageSectionType.ProtectedMethod:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "ProtectedMethod");
                    break;

                case PageSectionType.PrivateMethod:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PrivateMethod");
                    break;

                case PageSectionType.PublicProperty:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PublicProperty");
                    break;

                case PageSectionType.ProtectedProperty:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "ProtectedProperty");
                    break;

                case PageSectionType.PrivateProperty:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PrivateProperty");
                    break;

                case PageSectionType.PublicField:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PublicField");
                    break;

                case PageSectionType.ProtectedField:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "ProtectedField");
                    break;

                case PageSectionType.PrivateField:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PrivateField");
                    break;

                case PageSectionType.Event:
                    secText = GetTypeMemberCategorySection(memberItems, type, typeData, "Event");
                    break;

                case PageSectionType.Remarks:
                {
                    if (typeData != null)
                    {
                        HtmlText remarks = new HtmlText();
                        remarks.AppendPre(GetChildNodeInnerText(typeData, "remarks"));
                        if (remarks.Length > 0)
                        {
                            Hashtable sectionValues = new Hashtable();
                            sectionValues["ID"]      = "remarks";
                            sectionValues["Title"]   = Resources.String42;
                            sectionValues["Content"] = tempRemarks.Render(remarks);
                            secText = tempSection.Render(sectionValues);
                        }
                    }
                    break;
                }

                case PageSectionType.Example:
                {
                    if (typeData != null)
                    {
                        HtmlText example = new HtmlText();
                        example.AppendPre(GetChildNodeInnerText(typeData, "example"));
                        if (example.Length > 0)
                        {
                            Hashtable sectionValues = new Hashtable();
                            sectionValues["ID"]      = "example";
                            sectionValues["Title"]   = Resources.String43;
                            sectionValues["Content"] = tempRemarks.Render(example);
                            secText = tempSection.Render(sectionValues);
                        }
                    }
                    break;
                }

                case PageSectionType.FromXML:
                {
                    if (typeData != null)
                    {
                        string text = GetChildNodeInnerText(typeData, section.XmlSource);
                        if (!string.IsNullOrEmpty(text))
                        {
                            Hashtable sectionValues = new Hashtable();
                            sectionValues["ID"]      = "SEC" + secID.ToString("00");
                            sectionValues["Title"]   = section.Name;
                            sectionValues["Content"] = text;
                            secText = tempSection.Render(sectionValues);
                        }
                    }
                    break;
                }
                }
                if (!string.IsNullOrEmpty(secText))
                {
                    pageContent.AppendPre(secText);
                }
                secID++;
            }

            Hashtable values = new Hashtable();

            values["CollapseAll"] = Resources.strCollapseAll;
            values["ExpandAll"]   = Resources.strExpandAll;
            values["PIC"]         = Resources.strPic;
            values["Title"]       = HtmlText.HtmlFormat(DocumentBuilderUtility.GetTypeDefinition(type, false)) + " " + NameToTypeDictionary[typeItem.Parent.Name];
            values["Summary"]     = GetChildNodeInnerText(typeData, "summary");
            values["Content"]     = pageContent;
            values["Encoding"]    = TargetEncoding.HeaderName;
            tempPage.SaveAs(
                HtmlFileDirectory + "\\" + typeItem.FileName,
                TargetEncoding,
                values
                );
        }