Ejemplo n.º 1
0
        public string Convert(INode node)
        {
            HtmlTraversal traversal = new HtmlTraversal();

            traversal.Traverse(node);

            return(traversal.GetString().Trim());
        }
Ejemplo n.º 2
0
        private void HandleElement(IElement element)
        {
            this.justClosedDiv = false;

            string tagName = element.TagName.ToUpper();

            switch (tagName)
            {
            case "BR":
                Write("\n");
                break;

            case "H1":
            case "H2":
            case "H3":
                Write("\n\n");

                HtmlTraversal headingTrav = new HtmlTraversal();
                headingTrav.TraverseChildren(element);

                string headingText = headingTrav.GetString().Trim();

                if (string.IsNullOrEmpty(headingText))
                {
                    break;
                }

                int    dividerLength = headingText.Split('\n').Max(x => x.Length);
                string divider;

                if (tagName == "H1")
                {
                    divider = new string('+', dividerLength);
                }
                else
                {
                    divider = new string('-', dividerLength);
                }

                if (tagName == "H3")
                {
                    Write(headingText);
                    Write("\n");
                    Write(divider);
                }
                else
                {
                    Write(divider);
                    Write("\n");
                    Write(headingText);
                    Write("\n");
                    Write(divider);
                }

                Write("\n\n");
                break;

            case "H4":
            case "H5":
            case "H6":
                Write("\n\n");
                TraverseChildren(element);
                Write("\n\n");
                break;

            case "DIV":
                if (this.lineLength > 0)
                {
                    Write("\n");
                }

                TraverseChildren(element);

                if (!this.justClosedDiv)     // avoid writing new lines twice
                {
                    Write("\n");
                }

                this.justClosedDiv = true;

                break;

            case "LI":
                HtmlTraversal trav = new HtmlTraversal();
                trav.TraverseChildren(element);

                string itemText = trav.GetString().Trim();

                if (!string.IsNullOrWhiteSpace(itemText))
                {
                    Write("* ");
                    Write(itemText);
                    Write("\n");
                }

                break;

            case "P":
            case "UL":
                Write("\n\n");
                TraverseChildren(element);
                Write("\n\n");
                break;

            case "IMG":
                string altText = element.GetAttribute("alt");

                if (!string.IsNullOrWhiteSpace(altText))
                {
                    Write("[IMG: ");
                    Write(altText);
                    Write("] ");
                }

                break;

            case "STYLE":
            case "SCRIPT":
            case "HEAD":
                break;

            case "TR":
                // Start of table row
                Write("| ");

                TraverseChildren(element);

                // Separate rows with an empty line
                Write("\n\n");

                break;

            case "TD":
            case "TH":
                TraverseChildren(element);

                // Separate table columns with a symbol
                Write(" | ");

                break;

            default:
                TraverseChildren(element);
                break;
            }
        }