コード例 #1
0
ファイル: HtmlParser.cs プロジェクト: Verlic/BlueMoon
        /// <summary>
        /// Parses source code.
        /// </summary>
        /// <param name="source">The source code which will be parsed.</param>
        /// <param name="scannerResult">The result returned by the scanners after scanning the source code.</param>
        /// <returns>The highlighted source code.</returns>
        public override string Parse(string source, IList <Occurrence> scannerResult)
        {
            StringWriter    htmlStringWriter = new StringWriter();
            XhtmlTextWriter htmlWriter       = new XhtmlTextWriter(htmlStringWriter);

            int lastIndex = 0;

            for (int i = 0; i < scannerResult.Count; i++)
            {
                if ((scannerResult[i].Start - lastIndex) >= 0)
                {
                    if (scannerResult[i].Start > 0)
                    {
                        // Encode non-highlighted text first.
                        htmlWriter.WriteEncodedText(source.Substring(lastIndex, scannerResult[i].Start - lastIndex));
                    }

                    htmlWriter.Write(this.ParseToken(source.Substring(scannerResult[i].Start, scannerResult[i].Length), scannerResult[i].Node));
                }

                lastIndex = scannerResult[i].Start + scannerResult[i].Length;
            }

            // Encode the last bit of nonhighlighted text.
            if (lastIndex < source.Length)
            {
                htmlWriter.WriteEncodedText(source.Substring(lastIndex));
            }

            string result = htmlStringWriter.ToString();

            htmlWriter.Close();
            htmlStringWriter.Close();

            return(result);
        }
コード例 #2
0
        private void AddNode(UfDataNode node, UfElementDescriber elementDescriber, XhtmlTextWriter writer)
        {
            if (node.Name != string.Empty)
            {
                indentNum++;
                writer.Indent = indentNum;

                UfElementDescriber currentDescriber = elementDescriber;
                foreach (UfElementDescriber childElementDescriber in elementDescriber.Elements)
                {
                    if (node.Name == childElementDescriber.Name || node.Name == childElementDescriber.CompoundName)
                    {
                        currentDescriber = childElementDescriber;
                    }
                }

                if (currentDescriber.Attribute == "class")
                {
                    writer.WriteBeginTag("div");
                    if (currentDescriber.CompoundName == "")
                    {
                        writer.WriteAttribute("class", node.Name);
                    }
                    else
                    {
                        writer.WriteAttribute("class", node.Name + " " + currentDescriber.Name);
                    }

                    if (!string.IsNullOrEmpty(node.ElementId))
                    {
                        writer.WriteAttribute("id", node.ElementId);
                    }

                    writer.Write(HtmlTextWriter.TagRightChar);
                    writer.WriteEncodedText(node.Value);


                    foreach (UfDataNode child in node.Nodes)
                    {
                        writer.WriteLine();
                        AddNode(child, currentDescriber, writer);
                    }


                    if (node.Name != string.Empty)
                    {
                        writer.WriteEndTag("div");
                        writer.WriteLine();
                    }
                }

                if (currentDescriber.Attribute == "rel")
                {
                    writer.WriteBeginTag("a");
                    writer.WriteAttribute("href", node.DescendantValue("link"));
                    writer.WriteAttribute("rel", node.Name);
                    writer.Write(HtmlTextWriter.TagRightChar);

                    writer.WriteEncodedText(node.DescendantValue("text"));
                    writer.WriteEndTag("a");
                    writer.WriteLine();
                }

                indentNum--;
                writer.Indent = indentNum;
            }
        }