Esempio n. 1
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);
        }
Esempio n. 2
0
        /// <summary>
        /// Write a text in a paragraph, with some styles
        /// </summary>
        /// <param name="paragraph"></param>
        /// <param name="text"></param>
        /// <param name="fontProperties"></param>
        public void WriteText(Paragraph paragraph, string text, StyleProperties fontProperties)
        {
            Run           run = new Run();
            RunProperties rp  = new RunProperties();

            if (fontProperties.StyleName != null)
            {
                rp.Append(new RunStyle()
                {
                    Val = fontProperties.StyleName
                });
            }
            if (fontProperties.FontName != null)
            {
                rp.Append(new RunFonts()
                {
                    ComplexScript = fontProperties.FontName, Ascii = fontProperties.FontName, HighAnsi = fontProperties.FontName
                });
            }
            else if (fontProperties.UseTemplateHeadingFont)
            {
                rp.Append(new RunFonts()
                {
                    AsciiTheme = ThemeFontValues.MajorHighAnsi, HighAnsiTheme = ThemeFontValues.MajorHighAnsi, ComplexScriptTheme = ThemeFontValues.MajorHighAnsi
                });
            }
            if (fontProperties.FontSize != null)
            {
                rp.Append(new FontSize()
                {
                    Val = fontProperties.FontSize
                });
            }
            if (fontProperties.Bold)
            {
                rp.Append(new Bold());
            }
            if (fontProperties.Italic)
            {
                rp.Append(new Italic());
            }
            if (fontProperties.Underline != UnderlineValues.None)
            {
                rp.Append(new Underline()
                {
                    Val = fontProperties.Underline
                });
            }
            if (fontProperties.Strikeout)
            {
                rp.Append(new Strike());
            }
            if (fontProperties.WriteAs != VerticalPositionValues.Baseline)
            {
                rp.Append(new VerticalTextAlignment()
                {
                    Val = fontProperties.WriteAs
                });
            }
            if (fontProperties.UseThemeColor)
            {
                rp.Append(new DocumentFormat.OpenXml.Wordprocessing.Color()
                {
                    ThemeColor = fontProperties.ThemeColor
                });
            }
            else if (fontProperties.Color != null)
            {
                rp.Append(new DocumentFormat.OpenXml.Wordprocessing.Color()
                {
                    Val = string.Format("#{0:X2}{1:X2}{2:X2}", fontProperties.Color.Value.R, fontProperties.Color.Value.G, fontProperties.Color.Value.B)
                });
            }
            run.Append(rp);
            run.Append(new Text(text)
            {
                Space = SpaceProcessingModeValues.Preserve
            });
            paragraph.Append(run);
        }