Beispiel #1
0
        /// <summary>
        /// Initializes a new instance with the given style info.
        /// </summary>
        /// <param name="style">Style information</param>
        public Stylizer(Style style)
        {
            this.style = style;

            // generate css string here to optimize usage
            css = style.ToCss();
        }
Beispiel #2
0
        private bool StylizeParagraphs()
        {
            // find all paragraphs - OE elements - that have selections
            var elements = page.Descendants()
                           .Where(p => p.NodeType == XmlNodeType.Element &&
                                  p.Name.LocalName == "T" &&
                                  p.Attributes("selected").Any(a => a.Value.Equals("all")))
                           .Select(p => p.Parent);

            if (elements?.Any() == true)
            {
                var css = style.ToCss();

                var applied = new Style(style)
                {
                    ApplyColors = true
                };

                foreach (var element in elements)
                {
                    // clear any existing style on or within the paragraph
                    stylizer.Clear(element, style.ApplyColors ? Stylizer.Clearing.All : Stylizer.Clearing.None);

                    // style may still exist if apply colors if false and there are colors
                    var attr = element.Attribute("style");
                    if (attr == null)
                    {
                        // blast style onto paragraph, let OneNote normalize across
                        // children if it wants
                        attr = new XAttribute("style", css);
                        element.Add(attr);
                    }
                    else
                    {
                        applied.MergeColors(new Style(attr.Value));
                        attr.Value = applied.ToCss();
                    }

                    ApplySpacing(element, "spaceBefore", style.SpaceBefore);
                    ApplySpacing(element, "spaceAfter", style.SpaceAfter);

                    ApplyToList(element, style);
                }

                return(true);
            }

            return(false);
        }
Beispiel #3
0
        private void ColorizeElement(XElement element, string color)
        {
            var attr = element.Attribute("style");

            if (attr != null)
            {
                var style = new Style(attr.Value)
                {
                    Color = color
                };

                attr.Value = style.ToCss();
            }
            else
            {
                element.Add(new XAttribute("style", "color:#5B9BD5"));
            }
        }
Beispiel #4
0
        /// <summary>
        /// Apply info styles to given element, merging with existing properties
        /// </summary>
        /// <param name="info">The css styles to apply</param>
        /// <param name="element">The element to which the styles are applied</param>
        private void Apply(XElement element)
        {
            var span = element.Attribute("style");

            if (span == null)
            {
                // give element new style
                element.Add(new XAttribute("style", css));
            }
            else
            {
                var given = element.Attribute("style")?.Value;
                if (given != null)
                {
                    // merge style into element's style
                    var estyle = new Style(given);
                    estyle.Merge(style);
                    span.Value = estyle.ToCss();
                }
            }
        }
Beispiel #5
0
        private static bool RestyleText(XCData cdata, bool completed)
        {
            var modified = false;
            var wrapper  = cdata.GetWrapper();
            var span     = wrapper.Elements("span").FirstOrDefault(e => e.Attribute("style") != null);

            if (completed)
            {
                if (span == null)
                {
                    wrapper.FirstNode.ReplaceWith(
                        new XElement("span",
                                     new XAttribute("style", "text-decoration:line-through"),
                                     cdata.Value
                                     ));

                    modified = true;
                }
                else
                {
                    var style = new Style(span.Attribute("style").Value);
                    if (!style.IsStrikethrough)
                    {
                        style.IsStrikethrough = true;
                        var css = style.ToCss(false);
                        if (string.IsNullOrEmpty(css))
                        {
                            wrapper.Value = span.Value;
                        }
                        else
                        {
                            span.SetAttributeValue("style", style.ToCss(false));
                        }
                        modified = true;
                    }
                }
            }
            else
            {
                if (span != null)
                {
                    var style = new Style(span.Attribute("style").Value);
                    if (style.IsStrikethrough)
                    {
                        style.IsStrikethrough = false;
                        var css = style.ToCss(false);
                        if (string.IsNullOrEmpty(css))
                        {
                            wrapper.Value = span.Value;
                        }
                        else
                        {
                            span.SetAttributeValue("style", style.ToCss(false));
                        }
                        modified = true;
                    }
                }
            }

            cdata.Value = wrapper.GetInnerXml();

            return(modified);
        }