Beispiel #1
0
        internal void AddElement(MElement e)
        {
            ValidateElement(e);

            if (e.Parent != null)
            {
                e = e.Clone();
            }
            else
            {
                var p = this;

                while (p.Parent != null)
                {
                    p = p.Parent;
                }

                if (e == p)
                {
                    e = e.Clone();
                }
            }

            ConvertTextToElement();
            AppendElement(e);
        }
Beispiel #2
0
        internal void RemoveElement(MElement e)
        {
            var p = (MElement)content;

            while (p.next != e)
            {
                p = p.next;
            }

            if (p == e)
            {
                content = null;
            }
            else
            {
                if (content == e)
                {
                    content = p;
                }

                p.next = e.next;
            }

            e.Parent = null;
            e.next   = null;
        }
Beispiel #3
0
        public void RemoveAll()
        {
            if (content == null)
            {
                return;
            }

            if (content is string)
            {
                content = null;
                return;
            }

            var e = (MElement)content;

            do
            {
                MElement n = e.next;

                e.Parent = null;
                e.next   = null;

                e = n;
            } while (e != content);

            content = null;
        }
        private static List <TableColumnInfo> Analyze(
            IEnumerator <MElement> en,
            MarkdownWriterSettings settings,
            MarkdownStringWriter writer)
        {
            MElement header = null;

            var columns = new List <TableColumnInfo>();

            header = en.Current;

            if (header is MContainer container)
            {
                WriteHeaderCells(container, settings, writer, columns);
            }
            else
            {
                writer.Write(header);
                columns.Add(TableColumnInfo.Create(header, writer));
            }

            if (settings.Format.FormatTableContent)
            {
                int index = writer.Length;

                while (en.MoveNext())
                {
                    int columnCount = columns.Count;

                    MElement row = en.Current;

                    if (row is MContainer rowContainer)
                    {
                        int i = 0;
                        foreach (MElement cell in rowContainer.Elements())
                        {
                            writer.Write(cell);
                            columns[i] = columns[i].UpdateWidthIfGreater(writer.Length - index);
                            index      = writer.Length;
                            i++;

                            if (i == columnCount)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        writer.Write(row);
                        columns[0] = columns[0].UpdateWidthIfGreater(writer.Length - index);
                        index      = writer.Length;
                    }
                }
            }

            return(columns);
        }
Beispiel #5
0
        internal override void ValidateElement(MElement element)
        {
            if (element.Kind == MarkdownKind.TableRow)
            {
                return;
            }

            base.ValidateElement(element);
        }
        private static void WriteHeaderCells(
            MContainer header,
            MarkdownWriterSettings settings,
            MarkdownStringWriter writer,
            List <TableColumnInfo> columns)
        {
            int index = 0;

            bool isFirst = true;
            bool isLast  = false;

            int i = 0;

            using (IEnumerator <MElement> en = header.Elements().GetEnumerator())
            {
                if (en.MoveNext())
                {
                    MElement curr = en.Current;

                    isLast = !en.MoveNext();

                    WriteHeaderCell(curr);

                    if (!isLast)
                    {
                        isFirst = false;

                        do
                        {
                            curr   = en.Current;
                            isLast = !en.MoveNext();
                            i++;

                            WriteHeaderCell(curr);
                        }while (!isLast);
                    }
                }
            }

            void WriteHeaderCell(MElement cellContent)
            {
                if (isFirst ||
                    isLast ||
                    settings.Format.FormatTableHeader)
                {
                    writer.Write(cellContent);
                }

                columns.Add(TableColumnInfo.Create(cellContent, writer, index));
                index = writer.Length;
            }
        }
Beispiel #7
0
        public IEnumerable <MElement> Elements()
        {
            MElement e = LastElement;

            if (e != null)
            {
                do
                {
                    e = e.next;
                    yield return(e);
                } while (e.Parent == this && e != content);
            }
        }
Beispiel #8
0
        internal void AppendElement(MElement e)
        {
            e.Parent = this;

            if (content == null ||
                content is string)
            {
                e.next = e;
            }
            else
            {
                var x = (MElement)content;
                e.next = x.next;
                x.next = e;
            }

            content = e;
        }
Beispiel #9
0
        internal override void ValidateElement(MElement element)
        {
            switch (element.Kind)
            {
            case MarkdownKind.Text:
            case MarkdownKind.Raw:
            case MarkdownKind.InlineCode:
            case MarkdownKind.CharEntity:
            case MarkdownKind.EntityRef:
            case MarkdownKind.Comment:
            case MarkdownKind.Bold:
            case MarkdownKind.Italic:
            case MarkdownKind.Strikethrough:
                return;
            }

            Error.InvalidContent(this, element);
        }
Beispiel #10
0
        internal IEnumerable <MElement> GetDescendants(bool self)
        {
            MElement e = this;

            if (self)
            {
                yield return(e);
            }

            var c = this;

            while (true)
            {
                MElement first = c?.FirstElement;

                if (first != null)
                {
                    e = first;
                }
                else
                {
                    while (e != this &&
                           e == e.Parent.content)
                    {
                        e = e.Parent;
                    }

                    if (e == this)
                    {
                        break;
                    }

                    e = e.next;
                }

                if (e != null)
                {
                    yield return(e);
                }

                c = e as MContainer;
            }
        }
Beispiel #11
0
        public override void WriteTo(MarkdownWriter writer)
        {
            IEnumerable <MElement> rows = Elements();

            IReadOnlyList <TableColumnInfo> columns = (writer as ITableAnalyzer)?.AnalyzeTable(rows);

            if (columns != null)
            {
                writer.WriteStartTable(columns);
            }
            else
            {
                MElement header = rows.FirstOrDefault();

                if (header == null)
                {
                    return;
                }

                int columnCount = (header as MContainer)?.Elements().Count() ?? 1;

                writer.WriteStartTable(columnCount);
            }

            using (IEnumerator <MElement> en = rows.GetEnumerator())
            {
                if (en.MoveNext())
                {
                    writer.WriteTableRow(en.Current);
                    writer.WriteTableHeaderSeparator();

                    while (en.MoveNext())
                    {
                        writer.WriteTableRow(en.Current);
                    }
                }
            }

            writer.WriteEndTable();
        }
Beispiel #12
0
        internal virtual void ValidateElement(MElement element)
        {
            switch (element.Kind)
            {
            case MarkdownKind.Text:
            case MarkdownKind.Raw:
            case MarkdownKind.Link:
            case MarkdownKind.LinkReference:
            case MarkdownKind.Image:
            case MarkdownKind.ImageReference:
            case MarkdownKind.Autolink:
            case MarkdownKind.InlineCode:
            case MarkdownKind.CharEntity:
            case MarkdownKind.EntityRef:
            case MarkdownKind.Comment:
            case MarkdownKind.Bold:
            case MarkdownKind.Italic:
            case MarkdownKind.Strikethrough:
            case MarkdownKind.Inline:
                return;
            }

            Error.InvalidContent(this, element);
        }