예제 #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>
        /// 将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());
        }