MarkdownDeep.TableSpec.Parse C# (CSharp) Метод

Parse() публичный статический Метод

public static Parse ( StringScanner p ) : TableSpec
p StringScanner
Результат TableSpec
        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
                ColumnAlignment col = ColumnAlignment.NA;
                if (AlignLeft && AlignRight)
                    col = ColumnAlignment.Center;
                else if (AlignLeft)
                    col = ColumnAlignment.Left;
                else if (AlignRight)
                    col = ColumnAlignment.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
            }
        }

Usage Example

Пример #1
0
        public static TableSpec ParseTableFromString(String s)
        {
            StringScanner scanner = new StringScanner(s);
            TableSpec     spec    = TableSpec.Parse(scanner);

            while (scanner.eof == false)
            {
                var row = spec.ParseRow(scanner);
                if (row != null)
                {
                    spec.Rows.Add(row);
                }
                else
                {
                    scanner.SkipForward(1);
                }
            }
            return(spec);
        }