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); }
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); }