コード例 #1
0
ファイル: PipeTableParser.cs プロジェクト: dnndev/markdig
        private static bool ParseHeaderString(Inline inline, out TableColumnAlign?align)
        {
            align = 0;
            var literal = inline as LiteralInline;

            if (literal == null)
            {
                return(false);
            }

            // Work on a copy of the slice
            var line = literal.Content;

            if (TableHelper.ParseColumnHeader(ref line, '-', out align))
            {
                if (line.CurrentChar != '\0')
                {
                    return(false);
                }
                return(true);
            }

            return(false);
        }
コード例 #2
0
        public override BlockState TryOpen(BlockProcessor processor)
        {
            // A grid table cannot start more than an indent
            if (processor.IsCodeIndent)
            {
                return(BlockState.None);
            }

            var            line       = processor.Line;
            GridTableState tableState = null;

            // Match the first row that should be of the minimal form: +---------------
            var c         = line.CurrentChar;
            var lineStart = line.Start;

            while (c == '+')
            {
                var columnStart = line.Start;
                line.NextChar();
                line.TrimStart();

                // if we have reached the end of the line, exit
                c = line.CurrentChar;
                if (c == 0)
                {
                    break;
                }

                // Parse a column alignment
                TableColumnAlign?columnAlign;
                if (!TableHelper.ParseColumnHeader(ref line, '-', out columnAlign))
                {
                    return(BlockState.None);
                }

                tableState = tableState ?? new GridTableState {
                    Start = processor.Start, ExpectRow = true
                };
                tableState.AddColumn(columnStart - lineStart, line.Start - lineStart, columnAlign);

                c = line.CurrentChar;
            }

            if (c != 0 || tableState == null)
            {
                return(BlockState.None);
            }
            // Store the line (if we need later to build a ParagraphBlock because the GridTable was in fact invalid)
            tableState.AddLine(ref processor.Line);
            var table = new Table(this);

            table.SetData(typeof(GridTableState), tableState);

            // Calculate the total width of all columns
            int totalWidth = 0;

            foreach (var columnSlice in tableState.ColumnSlices)
            {
                totalWidth += columnSlice.End - columnSlice.Start - 1;
            }

            // Store the column width and alignment
            foreach (var columnSlice in tableState.ColumnSlices)
            {
                var columnDefinition = new TableColumnDefinition
                {
                    // Column width proportional to the total width
                    Width     = (float)(columnSlice.End - columnSlice.Start - 1) * 100.0f / totalWidth,
                    Alignment = columnSlice.Align
                };
                table.ColumnDefinitions.Add(columnDefinition);
            }

            processor.NewBlocks.Push(table);

            return(BlockState.ContinueDiscard);
        }