Exemplo n.º 1
0
        public static void Parse(Md2MlEngine engine, string mdText)
        {
            var lineAndPattern = new List <KeyValuePair <ParaPattern, string[]> >();
            var lines          = mdText.Split('\n');

            foreach (var line in lines)
            {
                lineAndPattern.Add(PatternMatcher.GetParagraphType(line));
            }
            bool          OrderedList   = false;
            bool          UnorderedList = false;
            bool          Table         = false;
            List <string> BulletItems   = new List <string>();
            List <string> NumberItems   = new List <string>();
            List <string> TableData     = new List <string>();

            foreach (var line in lineAndPattern)
            {
                Paragraph para;
                switch (line.Key)
                {
                case ParaPattern.OrderedList:
                    if (Table)
                    {
                        ProcessTable(engine, TableData); Table = false;
                    }
                    if (UnorderedList)
                    {
                        ProcessBullets(engine, BulletItems, false); UnorderedList = false;
                    }
                    OrderedList = true;
                    NumberItems.Add(line.Value[1]);
                    break;

                case ParaPattern.UnorderedList:
                    if (Table)
                    {
                        ProcessTable(engine, TableData); Table = false;
                    }
                    if (OrderedList)
                    {
                        ProcessBullets(engine, NumberItems, true); OrderedList = false;
                    }
                    UnorderedList = true;
                    BulletItems.Add(line.Value[1]);
                    break;

                case ParaPattern.Image:
                    if (Table)
                    {
                        ProcessTable(engine, TableData); Table = false;
                    }
                    if (OrderedList || UnorderedList)
                    {
                        ProcessBullets(engine, OrderedList ? NumberItems : BulletItems, OrderedList); OrderedList = false; UnorderedList = false;
                    }
                    para = engine.CreateParagraph();
                    if (line.Value[2].StartsWith("http://") || line.Value[2].StartsWith("https://"))
                    {
                        engine.AddImage(new System.Net.WebClient().OpenRead(line.Value[2]));
                    }
                    else
                    {
                        engine.AddImage(System.IO.File.OpenRead(line.Value[2]));
                    }
                    break;

                case ParaPattern.Table:
                    if (OrderedList || UnorderedList)
                    {
                        ProcessBullets(engine, OrderedList ? NumberItems : BulletItems, OrderedList); OrderedList = false; UnorderedList = false;
                    }
                    Table = true;
                    TableData.Add(line.Value[1]);
                    break;

                case ParaPattern.CodeBlock:
                    if (Table)
                    {
                        ProcessTable(engine, TableData); Table = false;
                    }
                    if (OrderedList || UnorderedList)
                    {
                        ProcessBullets(engine, OrderedList ? NumberItems : BulletItems, OrderedList); OrderedList = false; UnorderedList = false;
                    }
                    para = engine.CreateParagraph(new ParaProperties()
                    {
                        StyleName = "CodeBlock"
                    });
                    engine.WriteText(para, line.Value[1]);
                    break;

                case ParaPattern.Heading1:
                    if (Table)
                    {
                        ProcessTable(engine, TableData); Table = false;
                    }
                    if (OrderedList || UnorderedList)
                    {
                        ProcessBullets(engine, OrderedList ? NumberItems : BulletItems, OrderedList); OrderedList = false; UnorderedList = false;
                    }
                    para = engine.CreateParagraph(new ParaProperties()
                    {
                        StyleName = "Heading1"
                    });
                    engine.WriteText(para, line.Value[1]);
                    break;

                case ParaPattern.Heading2:
                    if (Table)
                    {
                        ProcessTable(engine, TableData); Table = false;
                    }
                    if (OrderedList || UnorderedList)
                    {
                        ProcessBullets(engine, OrderedList ? NumberItems : BulletItems, OrderedList); OrderedList = false; UnorderedList = false;
                    }
                    para = engine.CreateParagraph(new ParaProperties()
                    {
                        StyleName = "Heading2"
                    });
                    engine.WriteText(para, line.Value[1]);
                    break;

                case ParaPattern.Heading3:
                    if (Table)
                    {
                        ProcessTable(engine, TableData); Table = false;
                    }
                    if (OrderedList || UnorderedList)
                    {
                        ProcessBullets(engine, OrderedList ? NumberItems : BulletItems, OrderedList); OrderedList = false; UnorderedList = false;
                    }
                    para = engine.CreateParagraph(new ParaProperties()
                    {
                        StyleName = "Heading3"
                    });
                    engine.WriteText(para, line.Value[1]);
                    break;

                case ParaPattern.Quote:
                    if (Table)
                    {
                        ProcessTable(engine, TableData); Table = false;
                    }
                    if (OrderedList || UnorderedList)
                    {
                        ProcessBullets(engine, OrderedList ? NumberItems : BulletItems, OrderedList); OrderedList = false; UnorderedList = false;
                    }
                    para = engine.CreateParagraph(new ParaProperties()
                    {
                        StyleName = "Quote"
                    });
                    engine.WriteText(para, line.Value[1]);
                    break;

                case ParaPattern.CommanBlock:
                default:
                    if (Table)
                    {
                        ProcessTable(engine, TableData); Table = false;
                    }
                    if (OrderedList || UnorderedList)
                    {
                        ProcessBullets(engine, OrderedList ? NumberItems : BulletItems, OrderedList); OrderedList = false; UnorderedList = false;
                    }
                    para = engine.CreateParagraph();
                    FormatText(engine, para, line.Value[1], new FontProperties());
                    //core.WriteText(para, line.Value[1]);
                    break;
                }
            }
            if (Table)
            {
                ProcessTable(engine, TableData); Table = false;
            }
            if (OrderedList || UnorderedList)
            {
                ProcessBullets(engine, OrderedList ? NumberItems : BulletItems, OrderedList); OrderedList = false; UnorderedList = false;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parse the content, and detect all real lines breaks.
        /// In fact, within markdown it is possible to write a unique paragraph, with line breaks.
        /// If there is not a double space before the line break, it's the same paragraph, and the line break is not interpreted as is.
        ///
        /// So in this parsing method, cut the content by "block" with same nature.
        ///		1| Detect the paragraph type by the start of the line
        ///		2| Process the content by its type
        ///		3| Then remove the processed content, and continue to parse the next content
        ///
        /// A block is detected by a <see cref="ParaPattern"/> which have an associated regex.
        ///
        /// </summary>
        /// <param name="engine">Describe an openXML object by code</param>
        /// <param name="mdText">The markdown text to parse</param>
        internal static void Parse(Md2MlEngine engine, string mdText)
        {
            while (!string.IsNullOrEmpty(mdText))
            {
                var       firstLine      = GetFirstLine(mdText);
                var       matchedPattern = PatternMatcher.GetMarkdownMatch(firstLine);
                Paragraph para;
                (int counter, string textBlock)rebuildText = default;
                switch (matchedPattern.Key)
                {
                case ParaPattern.InfiniteHeading:
                    string titleChars = matchedPattern.Value.Groups[1].Value;
                    int    titleLvl   = titleChars.Count(c => c == '#');
                    titleLvl = titleLvl <= 9 ? titleLvl : 9;
                    para     = engine.CreateParagraph(new ParaProperties()
                    {
                        StyleName = string.Concat("Heading", titleLvl.ToString())
                    });
                    engine.WriteText(para, matchedPattern.Value.Groups[2].Value);
                    mdText = DeleteLines(mdText);
                    continue;

                case ParaPattern.Image:
                    var link = matchedPattern.Value.Groups[2].Value;
                    if (link.StartsWith("http://") || link.StartsWith("https://"))
                    {
                        engine.AddImage(new System.Net.WebClient().OpenRead(link));
                    }
                    else
                    {
                        engine.AddImage(ConvertRelativeToAbsolutePath(link, engine.GetFileDirectory()));
                    }
                    mdText = DeleteLines(mdText);
                    continue;

                case ParaPattern.Table:
                    //case ParaPattern.TableHeaderSeparation:
                    rebuildText = BuildWithoutBreakingLines(mdText, ParaPattern.Table);
                    ProcessTable(engine, rebuildText.textBlock);
                    mdText = DeleteLines(mdText, rebuildText.counter);
                    continue;

                case ParaPattern.OrderedList:
                case ParaPattern.UnorderedList:
                    // TODO : Ordered list can contain unordered items inside and vice versa
                    rebuildText = BuildWithoutBreakingLines(mdText, ParaPattern.OrderedList);
                    ProcessBullets(engine, rebuildText.textBlock);
                    mdText = DeleteLines(mdText, rebuildText.counter);
                    continue;

                case ParaPattern.CodeBlock:
                    // TODO : Improve the rendering - not a priority for my needs
                    rebuildText = BuildWithoutBreakingLines(mdText, ParaPattern.CodeBlock);
                    para        = engine.CreateParagraph(new ParaProperties()
                    {
                        StyleName = DocStyles.CodeBlock.ToDescriptionString()
                    });
                    FormatText(engine, para, rebuildText.textBlock, new StyleProperties());
                    mdText = DeleteLines(mdText, rebuildText.counter);
                    continue;

                case ParaPattern.Quote:
                    // Markdown supports nested quotes, but word does not
                    // So whatever, put the "nested" paragraph in the same quote
                    rebuildText = BuildWithoutBreakingLines(mdText, ParaPattern.Quote);
                    para        = engine.CreateParagraph(new ParaProperties()
                    {
                        StyleName = DocStyles.Quote.ToDescriptionString()
                    });
                    foreach (var text in Regex.Split(rebuildText.textBlock, "\r\n|\r|\n"))
                    {
                        para.AppendChild(new Break());
                        FormatText(engine, para, text, new StyleProperties());
                    }
                    mdText = DeleteLines(mdText, rebuildText.counter);
                    continue;

                case ParaPattern.AnyChar:
                default:
                    rebuildText = BuildWithoutBreakingLines(mdText, matchedPattern.Key);
                    para        = engine.CreateParagraph();
                    FormatText(engine, para, rebuildText.textBlock, new StyleProperties());
                    // engine.WriteText(para, text.textBlock);
                    mdText = DeleteLines(mdText, rebuildText.counter);
                    continue;
                }
            }
        }