src/Tables/GridTable.cs
/// <summary>
/// Writes the block to the given TextWriter.
/// </summary>
/// <param name="writer">writer</param>
/// <param name="formatting">optional formatting</param>
Writes the block to the given TextWriter.
Writes the block to the given TextWriter .
public override void Write(TextWriter writer, MarkdownFormatting formatting)
        {
            if (!Children.Any()) return;
            var widths = CalculateWidths();
            var columns = new ColumnList();
            foreach (var row in Children)
            {
                var rowList = new List<string[]>();
                columns.Add(rowList);
                int colIndex = 0;
                for (var cellIndex = 0; cellIndex < widths.Length; cellIndex++)
                {
                    var cell = row.Count > cellIndex ? row[cellIndex] : null;
                    int width = widths.Length > colIndex ? widths[colIndex] : 0;
                    if (cell != null)
                    {
                        for (int x = 1; x < cell.ColumnSpan; x++)
                            width += widths[colIndex + x] + 1;
                        colIndex += cell.ColumnSpan;
                    }
                    var sw = new StringWriter();
                    var content = cell?.Content;
                    if (content != null)
                    {
                        content.Write(sw, formatting);
                        var block = sw.ToString().TrimEnd('\r', '\n');
                        var lines = SplitIntoWrappedText(block, width);
                        for (int i = 0; i < lines.Length; i++)
                            lines[i] = lines[i].Replace("|", @"&#124;");
                        rowList.Add(lines);
                    }
                    else
                    {
                        if (cell != null)
                            rowList.Add(new[] {new string(' ', width)});
                    }
                }
            }
            var sb = new StringBuilder();
            RenderRowSeparator(sb, widths, '-', true);
            for (var rowIndex = 0; rowIndex < columns.Count; rowIndex++)
            {
                var row = columns[rowIndex];
                int maxLines = row.Max(c => c.Length);
                for (int index = 0; index < maxLines; index++)
                {
                    sb.Append('|');
                    for (var colIndex = 0; colIndex < row.Count; colIndex++)
                    {
                        var cells = row[colIndex];
                        int width = widths[colIndex];
                        string text = cells.Length > index ? cells[index] : new string(' ', width);
                        sb.Append(text).Append('|');
                    }
                    sb.AppendLine();
                }
                RenderRowSeparator(sb, widths, rowIndex == 0 && HasHeader ? '=' : '-', false);
            }
            sb.AppendLine();
            writer.WriteLine(sb.ToString());
        }
public override void Write ( TextWriter writer , MarkdownFormatting formatting ) { if ( ! Children . Any ( ) ) return ; var widths = CalculateWidths ( ) ; var columns = new ColumnList ( ) ; foreach ( var row in Children ) { var rowList = new List < string [ ] > ( ) ; columns . Add ( rowList ) ; int colIndex = 0 ; for ( var cellIndex = 0 ; cellIndex < widths . Length ; cellIndex ++ ) { var cell = row . Count > cellIndex ? row [ cellIndex ] : null ; int width = widths . Length > colIndex ? widths [ colIndex ] : 0 ; if ( cell != null ) { for ( int x = 1 ; x < cell . ColumnSpan ; x ++ ) width += widths [ colIndex + x ] + 1 ; colIndex += cell . ColumnSpan ; } var sw = new StringWriter ( ) ; var content = cell ? . Content ; if ( content != null ) { content . Write ( sw , formatting ) ; var block = sw . ToString ( ) . TrimEnd ( ' \r ' , ' \n ' ) ; var lines = SplitIntoWrappedText ( block , width ) ; for ( int i = 0 ; i < lines . Length ; i ++ ) lines [ i ] = lines [ i ] . Replace ( " " , @"&#124;" ) ; rowList . Add ( lines ) ; } else { if ( cell != null ) rowList . Add ( new [ ] { new string ( ' ' , width ) } ) ; } } } var sb = new StringBuilder ( ) ; RenderRowSeparator ( sb , widths , ' ' , true ) ; for ( var rowIndex = 0 ; rowIndex < columns . Count ; rowIndex ++ ) { var row = columns [ rowIndex ] ; int maxLines = row . Max ( c => c . Length ) ; for ( int index = 0 ; index < maxLines ; index ++ ) { sb . Append ( ' ' ) ; for ( var colIndex = 0 ; colIndex < row . Count ; colIndex ++ ) { var cells = row [ colIndex ] ; int width = widths [ colIndex ] ; string text = cells . Length > index ? cells [ index ] : new string ( ' ' , width ) ; sb . Append ( text ) . Append ( ' ' ) ; } sb . AppendLine ( ) ; } RenderRowSeparator ( sb , widths , rowIndex == 0 && HasHeader ? ' ' : ' ' , false ) ; } sb . AppendLine ( ) ; writer . WriteLine ( sb . ToString ( ) ) ; }
Writes the block to the given TextWriter.
Writes the block to the given TextWriter .
// Build each row -- this allows us to wrap on our max width across physical rows. // Determine the width of the cell, taking into account any column spanning. // Now split the block. // Build the row separator header // Now render each row.
writer
TextWriter
None
formatting
MarkdownFormatting
None