Example #1
0
        public static TableSpec Parse(StringScanner p)
        {
            // Leading line space allowed
            p.SkipLinespace();

            // Quick check for typical case
            if (p.current != '|' && p.current != ':' && p.current != '-')
                return null;

            // Don't create the spec until it at least looks like one
            TableSpec spec = null;

            // Leading bar, looks like a table spec
            if (p.SkipChar('|'))
            {
                spec=new TableSpec();
                spec.LeadingBar=true;
            }

            // Process all columns
            while (true)
            {
                // Parse column spec
                p.SkipLinespace();

                // Must have something in the spec
                if (p.current == '|')
                    return null;

                bool AlignLeft = p.SkipChar(':');
                while (p.current == '-')
                    p.SkipForward(1);
                bool AlignRight = p.SkipChar(':');
                p.SkipLinespace();

                // Work out column alignment
                TableCellAlignment col = TableCellAlignment.NA;
                if (AlignLeft && AlignRight)
                    col = TableCellAlignment.Center;
                else if (AlignLeft)
                    col = TableCellAlignment.Left;
                else if (AlignRight)
                    col = TableCellAlignment.Right;

                if (p.eol)
                {
                    // Not a spec?
                    if (spec == null)
                        return null;

                    // Add the final spec?
                    spec.Columns.Add(col);
                    return spec;
                }

                // We expect a vertical bar
                if (!p.SkipChar('|'))
                    return null;

                // Create the table spec
                if (spec==null)
                    spec=new TableSpec();

                // Add the column
                spec.Columns.Add(col);

                // Check for trailing vertical bar
                p.SkipLinespace();
                if (p.eol)
                {
                    spec.TrailingBar = true;
                    return spec;
                }

                // Next column
            }
        }
        internal bool StartTable(TableSpec spec, List<Block> lines)
        {
            // Rewind, parse the header row then fast forward back to current pos
            if (lines.Count >= 1)
            {
                int savepos = position;
                position = lines[0].lineStart;
                for (var i = 0; i < lines.Count; i++) {
                    var row = spec.ParseRow(this);
                    if (row == null) break;
                    spec.Headers.Add(row);
                }
                if (spec.Headers.Count != lines.Count) return false;
                position = savepos;
                lines.Clear();
            }

            // Parse all rows
            while (true)
            {
                int savepos = position;

                var row=spec.ParseRow(this);
                if (row!=null)
                {
                    spec.Rows.Add(row);
                    continue;
                }

                position = savepos;
                break;
            }

            return true;
        }