Beispiel #1
0
        public bool ProcessParagraph(IElementProcessingState state, Paragraph paragraph)
        {
            var pData = _numberingMapper.GetNumbering(paragraph);

            if (pData == null)
            {
                return(false);
            }

            var numberingContainer = BuildNumberingContainer();
            var numberingNumber    = BuildNumberingNumber(pData);

            numberingContainer.VLayers[0].Content.Add(numberingNumber);

            if (!state.GetContext(out ParagraphContext paragraphContext))
            {
                return(false);
            }
            paragraphContext.Parent.VLayers[0].Prefix.Add(numberingContainer);

            state.SetContext(new NumberingContext
            {
                NumberingId     = pData.NumberingId,
                Level           = pData.LevelIndex,
                NumberingNumber = numberingNumber
            });
            return(false);
        }
Beispiel #2
0
 public void Initialize()
 {
     _cssRegistrator = Substitute.For <ICssRegistrator>();
     _styleConveter  = Substitute.For <IStyleConverter>();
     _styleConveter.BuildRegistrator().Returns(_cssRegistrator);
     _config   = new StyleConfig();
     _instance = new StylePlugin(_styleConveter, _config);
     _css      = new StringBuilder();
     _state    = Substitute.For <IProcessingState>();
     _state.Css.Returns(_css);
     _state.Elements.Returns(_ => new ElementProcessingState[0]);
     _state.GetContext(out ICssRegistrator _)
     .Returns(x =>
     {
         x[0] = _cssRegistrator;
         return(true);
     });
     _parentVNode  = new VNode();
     _currentVNode = new VNode();
     _elemState    = Substitute.For <IElementProcessingState>();
     _elemState.ProcessingState.Returns(_state);
     _elemState
     .GetContext(out ParagraphContext _)
     .Returns(x =>
     {
         x[0] = new ParagraphContext {
             Parent = _parentVNode
         };
         return(true);
     });
     _elemState.CurrentVNode.Returns(_ => _currentVNode);
 }
Beispiel #3
0
        public void PostProcess_Test()
        {
            _fullCssData = new CssData();
            var state = Substitute.For <IProcessingState>();
            var css   = new StringBuilder();

            state.Css.Returns(css);
            var elemStates = new IElementProcessingState[]
            {
                MockElemProcessingStateWithTableContext(),
                Substitute.For <IElementProcessingState>(),
                MockElemProcessingStateWithTableContext(),
                Substitute.For <IElementProcessingState>(),
                MockElemProcessingStateWithTableContext(),
                Substitute.For <IElementProcessingState>()
            };

            state.Elements.Returns(elemStates);

            _instance.PostProcessing(state);

            var expected = new StringBuilder();

            _fullCssData.RenderInto(expected);
            Assert.AreEqual(TablePlugin.CSS + expected.ToString(), css.ToString());
        }
Beispiel #4
0
        private bool ProcessModifiedRun(IElementProcessingState state, OpenXmlElement modifedRun, string cssClass)
        {
            var vNode = new VNode("span", "", state.Index, modifedRun.InnerText.Length);

            vNode.AddClasses(cssClass);
            state.AddContent(vNode);
            state.CurrentVNode = vNode;
            return(true);
        }
Beispiel #5
0
 private VNode GenSymbolVNode(IElementProcessingState state)
 {
     return(new VNode()
     {
         Tag = _config.SpecialCharacterTagPrefix + new NameGenerator().Generate(),
         Index = state.Index,
         Length = 0
     });
 }
Beispiel #6
0
        [ElementProcessing] // require refactoring (again)
        public bool ProcessParagraph(IElementProcessingState state, Paragraph p)
        {
            var props = p.ParagraphProperties;

            state.SetContext(props);
            state.ProcessingState.GetContext(out ICssRegistrator registrator);
            state.GetContext(out ParagraphContext pContext);
            var parentVNode = pContext.Parent;

            // Build and register class
            var param = new ParagraphClassParam(props);

            // Add table properties
            if (state.GetContext(out TableContext tableContext))
            {
                var tableCell = p.Ancestors <TableCell>().FirstOrDefault();
                if (tableCell != null)
                {
                    var cellState = tableContext.GetCell(tableCell);
                    param.TableProperties = cellState.ParagraphProperties;
                }
            }

            // Add numbering properties
            if (state.GetContext(out NumberingContext numberingContext) &&
                props != null)
            {
                // Add the numbering to the cls param
                param.NumberingId    = numberingContext.NumberingId;
                param.NumberingLevel = numberingContext.Level;

                // Register the run mark and add the class to the numbering numbering
                var numberingCls = registrator.RegisterRun(new RunClassParam
                {
                    InlineProperties = props.ParagraphMarkRunProperties,
                    ParagraphStyleId = param.StyleId,
                    NumberingId      = numberingContext.NumberingId,
                    NumberingLevel   = numberingContext.Level
                });
                parentVNode.AddClasses(_config.ContainerWithNumberingCssClassSuffix.Substring(1));
                numberingContext.NumberingNumber.AddClasses(numberingCls.Name);
            }

            // Add the class to the VNode containing the paragraph
            var pCls = registrator.RegisterParagraph(param);

            parentVNode.AddClasses(pCls.Name);

            // Set the paragraph css context
            state.SetContext(new ParagraphCssContext
            {
                CssClass = pCls
            });

            return(false);
        }
Beispiel #7
0
 public bool ProcessElement(IElementProcessingState state, NoBreakHyphen noBreakHyphen)
 {
     state.CurrentVNode.Text += "‑"; // non breaking hyphen
     state.CurrentVNode.VOffsets.Add(new VTextOffset
     {
         Index = state.Index,
         Delta = 1
     });
     return(false);
 }
Beispiel #8
0
 public bool ProcessElement(IElementProcessingState state, SoftHyphen softHyphen)
 {
     state.CurrentVNode.Text += "-"; // soft hyphen
     state.CurrentVNode.VOffsets.Add(new VTextOffset
     {
         Index = state.Index,
         Delta = 1
     });
     return(false);
 }
Beispiel #9
0
 public bool ProcessElement(IElementProcessingState state, TabChar tabChar)
 {
     state.CurrentVNode.SetStyle("white-space", "pre-wrap");
     state.CurrentVNode.Text += _config.TabCharString;
     state.CurrentVNode.VOffsets.Add(new VTextOffset
     {
         Index = state.Index,
         Delta = _config.TabCharString.Length
     });
     return(false);
 }
Beispiel #10
0
        public bool ProcessElement(IElementProcessingState state, FieldCode fc)
        {
            var offset = new VTextOffset
            {
                Delta = fc.InnerText.Length * -1,
                Index = state.Index
            };

            state.CurrentVNode.VOffsets.Add(offset);
            state.Index += offset.Delta * -1;
            return(false);
        }
Beispiel #11
0
        private static void SetErrorNode(IElementProcessingState state)
        {
            var messageNode = new VNode("div", "Error processing table", state.Index);

            messageNode.VOffsets.Add(new VTextOffset
            {
                Index = state.Index,
                Delta = messageNode.Text.Length
            });
            messageNode.SetStyle("color", "red");
            state.AddContent(messageNode);
        }
Beispiel #12
0
 public bool ProcessElement(IElementProcessingState state, Table table)
 {
     try
     {
         TryAddTable(state, table);
         return(true);
     }
     catch
     {
         SetErrorNode(state);
         return(false);
     }
 }
Beispiel #13
0
 private SpacingCssProperty GetSpacingProp(IElementProcessingState state)
 {
     if (state.GetContext(out ParagraphCssContext context))
     {
         var cssClass = context.CssClass;
         if (cssClass == null)
         {
             return(null);
         }
         return(cssClass.Props.Get <SpacingCssProperty>());
     }
     return(null);
 }
Beispiel #14
0
        private void TryAddTable(IElementProcessingState state, Table table)
        {
            if (!state.GetContext(out TableContext tc))
            {
                tc = new TableContext();
                tc.GridStateFactory = GridStateFactory;
                state.SetContext(tc);
            }
            var grid = tc.AddTable(table, state.Index);

            state.AddContent(grid.VNode);
            state.CurrentVNode = grid.VNode;
        }
Beispiel #15
0
        public bool ProcessRun(IElementProcessingState state, Run run)
        {
            if (!state.ProcessingState.GetContext(out ICssRegistrator registrator))
            {
                return(false);
            }
            RunClassParam param    = BuildRunCssParam(state, run);
            CssClass      cssClass = registrator.RegisterRun(param);

            state.CurrentVNode.AddClasses(cssClass.Name);

            return(false);
        }
Beispiel #16
0
        public bool ProcessElement(IElementProcessingState state, Hyperlink anchor)
        {
            var vAnchor = new VNode
            {
                Tag    = _config.HyperLinkTag,
                Index  = state.Index,
                Length = anchor.InnerText.Length
            };

            vAnchor.AddClasses(_config.HyperLinkCls);
            state.AddContent(vAnchor);
            state.CurrentVNode = vAnchor;
            return(true);
        }
Beispiel #17
0
        public bool ProcessElement(IElementProcessingState state, SymbolChar symbol)
        {
            VNode vNode = GenSymbolVNode(state);

            state.AddLayer(vNode);
            state.GetContext(out ParagraphContext context);

            string selector = vNode.Tag + "::before";

            SetSymbolContent(symbol, context, selector);
            SetSymbolFont(symbol, context, selector);

            return(false);
        }
Beispiel #18
0
        public bool ProcessElement(IElementProcessingState state, Run run)
        {
            var vSpan = new VNode
            {
                Tag    = _config.RunTag,
                Index  = state.Index,
                Length = run.InnerText.Length
            };

            vSpan.AddClasses(_config.RunCls);
            state.AddContent(vSpan);
            state.CurrentVNode = vSpan;
            return(true);
        }
Beispiel #19
0
 public void Initialize()
 {
     _config       = new TrackChangesConfig();
     _instance     = new TrackChangesPlugin(_config);
     _rootVNode    = new VNode();
     _currentVNode = new VNode();
     _elemState    = Substitute.For <IElementProcessingState>();
     _elemState.Index.Returns(10);
     _elemState.RootVNode.Returns(_rootVNode);
     _elemState.CurrentVNode.Returns(_currentVNode);
     _elemState
     .When(x => { x.CurrentVNode = Arg.Any <VNode>(); })
     .Do(x => _currentVNode = x.Arg <VNode>());
 }
Beispiel #20
0
 public void Initialize()
 {
     _config = new TextProcessorConfig()
     {
         ParagraphCls = "pcls",
         HyperLinkCls = "hcls",
         RunCls       = "rcls"
     };
     _instance     = new TextProcessorPlugin(_config);
     _state        = Substitute.For <IProcessingState>();
     _elementState = Substitute.For <IElementProcessingState>();
     _elementState.CurrentVNode.Returns(new VNode("div"));
     _elementState
     .When(x => x.CurrentVNode = Arg.Any <VNode>())
     .Do(x => _currentNode     = x.Arg <VNode>());
 }
Beispiel #21
0
 private void InsertBreak(IElementProcessingState state)
 {
     if (state.Index == 0 &&
         state.GetContext(out ParagraphContext c))
     {
         c.Parent.AddClasses(_config.BreakAtStartCssCls);
     }
     else
     {
         state.AddContent(new VNode
         {
             Index  = state.Index,
             Length = 0,
             Tag    = "br"
         });
     }
 }
Beispiel #22
0
 public bool ProcessElement(IElementProcessingState state, TableCell tableCell)
 {
     if (!state.GetContext(out TableContext tc))
     {
         return(false);
     }
     try
     {
         var cell = tc.GetCell(tableCell);
         state.CurrentVNode = cell.VNode;
         return(true);
     }
     catch (KeyNotFoundException)
     {
         return(false);
     }
 }
Beispiel #23
0
 public void Initialize()
 {
     _wpDoc        = EmptyDocuments.GenerateDocument();
     _instance     = new TablePlugin(_wpDoc);
     _tableContext = null;
     _elemState    = Substitute.For <IElementProcessingState>();
     _elemState.Index.Returns(10);
     _elemState
     .When(x => x.SetContext(Arg.Any <TableContext>()))
     .Do(x => _tableContext = x.Args()[0] as TableContext);
     _elemState
     .GetContext(out TableContext _)
     .Returns(x =>
     {
         x[0] = _tableContext;
         return(_tableContext != null);
     });
 }
 public void Initialize()
 {
     _config           = new NumberingConfig();
     _numberingMapper  = Substitute.For <INumberingMapper>();
     _paragraphContext = new ParagraphContext
     {
         CustomCss = new CssData(),
         Parent    = new VNode()
     };
     _state = Substitute.For <IElementProcessingState>();
     _state.GetContext(out ParagraphContext _).Returns(x =>
     {
         x[0] = _paragraphContext;
         return(true);
     });
     _level    = new Level();
     _instance = new NumberingPlugin(_numberingMapper, _config);
 }
Beispiel #25
0
        public bool ProcessElement(IElementProcessingState state)
        {
            var type = state.CurrentElement.GetType();

            if (ElementProcessingActions.TryGetValue(type,
                                                     out Func <IElementProcessingState, OpenXmlElement, bool>[] actions))
            {
                bool r = false;
                for (int i = 0; i < actions.Length; i++)
                {
                    r = actions[i](state, state.CurrentElement) || r;
                }
                return(r);
            }
            else
            {
                return(false);
            }
        }
Beispiel #26
0
        private static RunClassParam BuildRunCssParam(
            IElementProcessingState state,
            Run run)
        {
            var rPr   = run.RunProperties;
            var param = new RunClassParam(rPr);

            state.GetContext(out ParagraphProperties paragraphProperties);
            param.ParagraphStyleId = paragraphProperties?.ParagraphStyleId?.Val?.Value;
            if (state.GetContext(out TableContext tableContext))
            {
                var tableCell = run.Ancestors <TableCell>().FirstOrDefault();
                if (tableCell != null)
                {
                    var cellState = tableContext.GetCell(tableCell);
                    param.TableProperties = cellState?.RunProperties;
                }
            }
            return(param);
        }
Beispiel #27
0
        public bool ProcessElement(IElementProcessingState state, Paragraph p)
        {
            var layer = new VLayer();

            foreach (Match m in regex.Matches(p.InnerText))
            {
                var node = new VNode
                {
                    Index  = state.Index + m.Index,
                    Length = m.Length,
                    Tag    = "span"
                };
                node.AddClasses(CLASS_NAME);
                layer.Content.Add(node);
            }
            if (!layer.IsEmpty)
            {
                state.CurrentVNode.VLayers.Add(layer);
            }
            return(false);
        }
Beispiel #28
0
        public bool ProcessElement(IElementProcessingState state, Paragraph paragraph)
        {
            if (!(paragraph.Parent is Body))
            {
                var container = new VNode("div");
                container.AddClasses("container");
                state.AddContent(container);
                state.CurrentVNode = container;
            }
            var vP = new VNode {
                Tag = _config.ParagraphTag
            };

            vP.Index  = state.Index;
            vP.Length = paragraph.InnerText.Length;
            vP.Text   = "​"; // zero width white psace
            vP.AddClasses(_config.ParagraphCls);
            state.SetContext(new ParagraphContext {
                Parent = state.CurrentVNode
            });
            state.AddContent(vP);
            state.CurrentVNode = vP;
            return(true);
        }
Beispiel #29
0
 public bool ProcessElement(IElementProcessingState state, TableRow tableRow)
 {
     // hehe, that's it :)
     return(true);
 }
 public void Initalize()
 {
     _processingState = Substitute.For <IProcessingState>();
     _elementContext  = Substitute.For <IElementProcessingState>();
 }