コード例 #1
0
        public static bool IsImportant(OpenXmlElement c, INodeProvider nodeProvider)
        {
            if (nodeProvider.IsParagraph(c))
            {
                return(true);
            }

            if (nodeProvider.IsRun(c))
            {
                var text = c.ChildElements.Where(a => !nodeProvider.IsRunProperties(a)).Only();

                if (nodeProvider.IsText(text) && string.IsNullOrWhiteSpace(nodeProvider.GetText(text)))
                {
                    return(false);
                }

                return(true);
            }

            if (c is BaseNode)
            {
                return(true);
            }

            return(false);
        }
コード例 #2
0
    private static List <ElementInfo> GetElementInfos(IEnumerable <OpenXmlElement> childrens, INodeProvider nodeProvider)
    {
        var infos = childrens.Select(c => new ElementInfo(c, nodeProvider.IsRun(c) ? nodeProvider.GetText(c) : null)).ToList();

        int currentPosition = 0;

        foreach (ElementInfo ri in infos)
        {
            ri.Interval     = new Interval <int>(currentPosition, currentPosition + (ri.Text == null ? 0 : ri.Text.Length));
            currentPosition = ri.Interval.Max;
        }

        return(infos);
    }
コード例 #3
0
        private void FixNakedText(OpenXmlCompositeElement par, INodeProvider nodeProvider) //Simple Spreadsheets cells
        {
            if (par.ChildElements.Count != 1)
            {
                return;
            }

            var only = par.ChildElements.Only();

            if (!nodeProvider.IsText(only))
            {
                return;
            }

            var text = nodeProvider.GetText(only);

            if (!TemplateUtils.KeywordsRegex.IsMatch(text))
            {
                return;
            }

            par.RemoveChild(only);
            par.AppendChild(nodeProvider.WrapInRun(only));
        }
コード例 #4
0
        private void ReplaceRuns(OpenXmlCompositeElement par, INodeProvider nodeProvider)
        {
            FixNakedText(par, nodeProvider);

            string text = par.ChildElements.Where(a => nodeProvider.IsRun(a)).ToString(r => nodeProvider.GetText(r), "");

            var matches = TemplateUtils.KeywordsRegex.Matches(text).Cast <Match>().ToList();

            if (matches.Any())
            {
                List <ElementInfo> infos = GetElementInfos(par.ChildElements, nodeProvider);

                par.RemoveAllChildren();

                var stack = new Stack <ElementInfo>(infos.AsEnumerable().Reverse());

                foreach (var m in matches)
                {
                    var interval = new Interval <int>(m.Index, m.Index + m.Length);

                    //  [Before][Start][Ignore][Ignore][End]...[Remaining]
                    //              [        Match       ]

                    ElementInfo start = stack.Pop();           //Start
                    while (start.Interval.Max <= interval.Min) //Before
                    {
                        par.Append(start.Element);
                        start = stack.Pop();
                    }

                    var startRun = (OpenXmlCompositeElement)nodeProvider.CastRun(start.Element);

                    if (start.Interval.Min < interval.Min)
                    {
                        var firstRunPart = nodeProvider.NewRun(
                            (OpenXmlCompositeElement?)nodeProvider.GetRunProperties(startRun)?.CloneNode(true),
                            start.Text !.Substring(0, m.Index - start.Interval.Min),
                            SpaceProcessingModeValues.Preserve
                            );
                        par.Append(firstRunPart);
                    }

                    par.Append(new MatchNode(nodeProvider, m)
                    {
                        RunProperties = (OpenXmlCompositeElement?)nodeProvider.GetRunProperties(startRun)?.CloneNode(true)
                    });

                    ElementInfo end = start;
                    while (end.Interval.Max < interval.Max) //Ignore
                    {
                        end = stack.Pop();
                    }

                    if (interval.Max < end.Interval.Max) //End
                    {
                        var endRun = (OpenXmlCompositeElement)end.Element;

                        var textPart   = end.Text !.Substring(interval.Max - end.Interval.Min);
                        var endRunPart = nodeProvider.NewRun(
                            nodeProvider.GetRunProperties(startRun)?.Let(r => (OpenXmlCompositeElement)r.CloneNode(true)),
                            textPart,
                            SpaceProcessingModeValues.Preserve
                            );

                        stack.Push(new ElementInfo(endRunPart, textPart)
                        {
                            Interval = new Interval <int>(interval.Max, end.Interval.Max)
                        });
                    }
                }

                while (!stack.IsEmpty()) //Remaining
                {
                    var pop = stack.Pop();
                    par.Append(pop.Element);
                }
            }
        }