Example #1
0
        public static void FormatText(Md2MlEngine core, Paragraph paragraph, string markdown, FontProperties fontProperties)
        {
            var hasPattern = PatternMatcher.HasPatterns(markdown);

            while (hasPattern)
            {
                var s                 = PatternMatcher.GetPatternsAndNonPatternText(markdown);
                var count             = s.Value.Count();
                var NewFontProperties = new FontProperties();
                switch (s.Key)
                {
                case RunPattern.BoldAndItalic:
                    NewFontProperties.Bold   = true;
                    NewFontProperties.Italic = true;
                    FormatText(core, paragraph, s.Value[0], new FontProperties());
                    FormatText(core, paragraph, s.Value[1], NewFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "***"), new FontProperties());
                    break;

                case RunPattern.Bold:
                    NewFontProperties.Bold = true;
                    FormatText(core, paragraph, s.Value[0], new FontProperties());
                    FormatText(core, paragraph, s.Value[1], NewFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "**"), new FontProperties());
                    break;

                case RunPattern.Italic:
                    NewFontProperties.Italic = true;
                    FormatText(core, paragraph, s.Value[0], new FontProperties());
                    FormatText(core, paragraph, s.Value[1], NewFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "*"), new FontProperties());
                    break;

                case RunPattern.MonospaceOrCode:
                    NewFontProperties.StyleName = "InlineCodeChar";
                    FormatText(core, paragraph, s.Value[0], new FontProperties());
                    FormatText(core, paragraph, s.Value[1], NewFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "`"), new FontProperties());
                    break;

                case RunPattern.Strikethrough:
                    NewFontProperties.Strikeout = true;
                    FormatText(core, paragraph, s.Value[0], new FontProperties());
                    FormatText(core, paragraph, s.Value[1], NewFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "~~"), new FontProperties());
                    break;

                case RunPattern.Underline:
                    NewFontProperties.Underline = UnderlineValues.Single;
                    FormatText(core, paragraph, s.Value[0], new FontProperties());
                    FormatText(core, paragraph, s.Value[1], NewFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "__"), new FontProperties());
                    break;
                }
                return;
            }
            core.WriteText(paragraph, markdown, fontProperties);
        }
Example #2
0
        /// <summary>
        /// Format a paragraph which could contains some text styles, Bold, Italics, Images and so on...
        /// Split the markdown for each pattern found. Then append correctly that text or image into the same paragraph.
        /// </summary>
        /// <param name="core">The openXML object with a document, a body and a paragraph</param>
        /// <param name="paragraph">The Paragraph object previously created</param>
        /// <param name="markdown">The string to be processed</param>
        /// <param name="fontProperties">Style properties to apply to the text</param>
        internal static void FormatText(Md2MlEngine core, Paragraph paragraph, string markdown,
                                        StyleProperties fontProperties)
        {
            var hasPattern = PatternMatcher.HasPatterns(markdown);

            while (hasPattern)
            {
                var s = PatternMatcher.GetPatternsAndNonPatternText(markdown);
                var newFontProperties = new StyleProperties();

                switch (s.Key)
                {
                case StylePattern.BoldAndItalic:
                    newFontProperties.Bold   = true;
                    newFontProperties.Italic = true;
                    FormatText(core, paragraph, s.Value[0], new StyleProperties());
                    FormatText(core, paragraph, s.Value[1], newFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "***"), new StyleProperties());
                    break;

                case StylePattern.Bold:
                    newFontProperties.Bold = true;
                    FormatText(core, paragraph, s.Value[0], new StyleProperties());
                    FormatText(core, paragraph, s.Value[1], newFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "**"), new StyleProperties());
                    break;

                case StylePattern.Italic:
                    newFontProperties.Italic = true;
                    FormatText(core, paragraph, s.Value[0], new StyleProperties());
                    FormatText(core, paragraph, s.Value[1], newFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "*"), new StyleProperties());
                    break;

                case StylePattern.MonospaceOrCode:
                    newFontProperties.StyleName = DocStyles.CodeReference.ToDescriptionString();
                    FormatText(core, paragraph, s.Value[0], new StyleProperties());
                    FormatText(core, paragraph, s.Value[1], newFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "`"), new StyleProperties());
                    break;

                case StylePattern.Strikethrough:
                    newFontProperties.Strikeout = true;
                    FormatText(core, paragraph, s.Value[0], new StyleProperties());
                    FormatText(core, paragraph, s.Value[1], newFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "~~"), new StyleProperties());
                    break;

                case StylePattern.Image:
                    var regex = PatternMatcher.GetStyleMatch(s.Value[1]);
                    FormatText(core, paragraph, s.Value[0], new StyleProperties());
                    core.AddImage(ConvertRelativeToAbsolutePath(regex.Value.Groups[2].Value, core.GetFileDirectory()), paragraph);
                    FormatText(core, paragraph, FramePendingString(s.Value, ""), new StyleProperties());
                    break;

                case StylePattern.Underline:
                    newFontProperties.Underline = UnderlineValues.Single;
                    FormatText(core, paragraph, s.Value[0], new StyleProperties());
                    FormatText(core, paragraph, s.Value[1], newFontProperties);
                    FormatText(core, paragraph, FramePendingString(s.Value, "__"), new StyleProperties());
                    break;
                }
                return;
            }
            core.WriteText(paragraph, markdown, fontProperties);
        }