Пример #1
0
        private async Task RemoveSpacing()
        {
            using (var one = new OneNote(out var page, out var ns))
            {
                logger.StartClock();

                var elements =
                    (from e in page.Root.Descendants(page.Namespace + "OE")
                     where e.Elements().Count() == 1
                     let t = e.Elements().First()
                             where (t != null) && (t.Name.LocalName == "T") &&
                             ((e.Attribute("spaceBefore") != null) ||
                              (e.Attribute("spaceAfter") != null) ||
                              (e.Attribute("spaceBetween") != null))
                             select e)
                    .ToList();

                if (elements != null)
                {
                    var quickStyles = page.GetQuickStyles()
                                      .Where(s => s.StyleType == StyleType.Heading);

                    var customStyles = new StyleProvider().GetStyles()
                                       .Where(e => e.StyleType == StyleType.Heading)
                                       .ToList();

                    var modified = false;

                    foreach (var element in elements)
                    {
                        // is this a known Heading style?
                        var attr = element.Attribute("quickStyleIndex");
                        if (attr != null)
                        {
                            var index = int.Parse(attr.Value, CultureInfo.InvariantCulture);
                            if (quickStyles.Any(s => s.Index == index))
                            {
                                if (includeHeadings)
                                {
                                    modified |= CleanElement(element);
                                }

                                continue;
                            }
                        }

                        // is this a custom Heading style?
                        var style = new Style(element.CollectStyleProperties(true));
                        if (customStyles.Any(s => s.Equals(style)))
                        {
                            if (includeHeadings)
                            {
                                modified |= CleanElement(element);
                            }

                            continue;
                        }

                        // normal paragraph
                        modified |= CleanElement(element);
                    }

                    logger.WriteTime("removed spacing, now saving...");

                    if (modified)
                    {
                        await one.Update(page);
                    }
                }
            }
        }
Пример #2
0
        /*
         * Find consecutive empty lines that need to be collapsed...
         */

        public bool CollapseEmptyLines(Page page, XNamespace ns)
        {
            var elements =
                (from e in page.Root.Descendants(ns + "OE")
                 let t = e.Elements().FirstOrDefault()
                         where (t?.Name.LocalName == "T") && (t.TextValue().Trim().Length == 0)
                         select e)
                .ToList();

            if (elements?.Any() != true)
            {
                return(false);
            }

            var modified = false;

            var quickStyles = page.GetQuickStyles()
                              .Where(s => s.StyleType == StyleType.Heading);

            var customStyles = new StyleProvider().GetStyles()
                               .Where(e => e.StyleType == StyleType.Heading)
                               .ToList();

            foreach (var element in elements)
            {
                // is this a known Heading style?
                var attr = element.Attribute("quickStyleIndex");
                if (attr != null)
                {
                    var index = int.Parse(attr.Value, CultureInfo.InvariantCulture);
                    if (quickStyles.Any(s => s.Index == index))
                    {
                        // remove empty standard heading
                        element.Remove();
                        modified = true;
                        continue;
                    }
                }

                // is this a custom Heading style?
                var style = new Style(element.CollectStyleProperties(true));
                if (customStyles.Any(s => s.Equals(style)))
                {
                    // remove empty custom heading
                    element.Remove();
                    modified = true;
                    continue;
                }

                // is this an empty paragraph preceded by an empty paragraph?
                if (element.PreviousNode != null &&
                    element.PreviousNode.NodeType == System.Xml.XmlNodeType.Element)
                {
                    var prev = element.PreviousNode as XElement;

                    if (prev.Name.LocalName == "OE")
                    {
                        var t = prev.Elements().Last();
                        if (t.Name.LocalName == "T" && t.TextValue().Trim().Length == 0)
                        {
                            // remove consecutive empty line
                            prev.Remove();
                            modified = true;
                        }
                    }
                }
            }

            return(modified);
        }