Пример #1
0
        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);
        }
Пример #3
0
        private BlockState ParseRowSeparator(BlockProcessor state, GridTableState tableState, Table gridTable)
        {
            // A grid table must start with a line like this:
            // + ------------- + ------------ + ---------------------------------------- +
            // Spaces are optional

            var  line          = state.Line;
            var  c             = line.CurrentChar;
            bool isFirst       = true;
            var  delimiterChar = '\0';

            while (true)
            {
                if (c == '+')
                {
                    line.NextChar();
                    if (line.IsEmptyOrWhitespace())
                    {
                        if (isFirst)
                        {
                            return(BlockState.None);
                        }
                        break;
                    }

                    TableColumnAlign align;
                    if (TableHelper.ParseColumnHeaderDetect(ref line, ref delimiterChar, out align))
                    {
                        isFirst = false;
                        c       = line.CurrentChar;
                        continue;
                    }
                }

                // If we have any other characters, this is an invalid line
                return(BlockState.None);
            }

            // If we have an header row
            var isHeader = delimiterChar == '=';

            // Terminate the current row
            TerminateLastRow(state, tableState, gridTable, false);

            // If we had a header row separator, we can mark all rows since last row separator
            // to be header rows
            if (isHeader)
            {
                for (int i = tableState.StartRowGroup; i < gridTable.Count; i++)
                {
                    var row = (TableRow)gridTable[i];
                    row.IsHeader = true;
                }
            }

            // Makr the next start row group continue on the next row
            tableState.StartRowGroup = gridTable.Count;

            // We don't keep the line
            return(BlockState.ContinueDiscard);
        }
Пример #4
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;

            // A grid table must start with a line like this:
            // + ------------- + ------------ + ---------------------------------------- +
            // Spaces are optional

            GridTableState tableState    = null;
            var            c             = line.CurrentChar;
            var            startPosition = processor.Start;

            while (true)
            {
                if (c == '+')
                {
                    var startCharacter = line.Start;
                    line.NextChar();
                    if (line.IsEmptyOrWhitespace())
                    {
                        if (tableState == null)
                        {
                            return(BlockState.None);
                        }
                        break;
                    }

                    TableColumnAlign align;
                    if (TableHelper.ParseColumnHeader(ref line, '-', out align))
                    {
                        if (tableState == null)
                        {
                            tableState = new GridTableState()
                            {
                                Start     = processor.Start,
                                ExpectRow = true,
                            };
                        }
                        tableState.AddColumn(startCharacter - startPosition, line.Start - 1 - startPosition, align);

                        c = line.CurrentChar;
                        continue;
                    }
                }

                // If we have any other characters, this is an invalid line
                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);

            // Create the grid table
            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;
            }

            // 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) * 100.0f / totalWidth,
                    Alignment = columnSlice.Align,
                };
                table.ColumnDefinitions.Add(columnDefinition);
            }

            processor.NewBlocks.Push(table);

            return(BlockState.ContinueDiscard);
        }