예제 #1
0
        private static void ProcessTable(Md2MlEngine core, List <string> markdown)
        {
            var table = core.CreateTable(markdown.First().Trim('|').Split('|').Count());

            core.AddTableRow(table, markdown.First().Trim(new char[] { '|' }).Split('|').ToList());
            foreach (var data in markdown.Skip(2))
            {
                core.AddTableRow(table, data.Trim(new char[] { '|' }).Split('|').ToList());
            }
        }
예제 #2
0
        /// <summary>
        /// Process a full string block of a markdown table.
        /// Split it by rows
        /// Create the Table with correct number of columns
        /// Fill the openXML table
        /// </summary>
        /// <param name="engine"></param>
        /// <param name="markdown">The complete markdown table, must be correctly formatted</param>
        private static void ProcessTable(Md2MlEngine engine, string markdown)
        {
            var rows       = Regex.Split(markdown, "\r\n|\r|\n");
            var firstLine  = rows.First();
            var secondLine = rows.Length >= 2 ? rows.Skip(1).First() : null;
            var table      = engine.CreateTable(firstLine.Trim('|').Split('|').Count());

            engine.AddTableRow(table, firstLine.Trim('|').Split('|').ToList());
            var patternSecondLine = PatternMatcher.GetMarkdownMatch(secondLine).Key;

            if (string.IsNullOrEmpty(secondLine) ||
                (patternSecondLine != ParaPattern.TableHeaderSeparation && patternSecondLine != ParaPattern.TableHeaderSeparation)) // TODO : Throw error : Table not well formatted
            {
                return;
            }

            // Define the table alignment properties
            List <JustificationValues> cellJustification = new List <JustificationValues>();
            var nbCols          = secondLine.Trim('|').Split('|').Count();
            var secondLineCells = secondLine.Trim('|').Split('|').ToList();

            for (int i = 0; i < nbCols; i++)
            {
                var justification = JustificationValues.Left;
                if (secondLineCells[i].StartsWith(":") && secondLineCells[i].EndsWith(":"))
                {
                    justification = JustificationValues.Center;
                }
                else if (!secondLineCells[i].StartsWith(":") && secondLineCells[i].EndsWith(":"))
                {
                    justification = JustificationValues.Right;
                }

                cellJustification.Add(justification);
            }

            // Process the rest of the table
            foreach (var row in rows.Skip(2).ToList())
            {
                engine.AddTableRow(table, row.Trim('|').Split('|').ToList(), cellJustification);
            }
        }