Represents an instance of a document
Exemple #1
0
        public override TemplMatchTable Handler(DocX doc, object model, TemplMatchTable m)
        {
            m.Validate();
            var e        = TemplModelEntry.Get(model, m.Fields.First());
            var maxCells = TemplConst.MaxMatchesPerScope;

            if (m.Fields.Length == 2)
            {
                if (!uint.TryParse(m.Fields[1], out maxCells))
                {
                    throw new ArgumentException($"Templ: Dynamic Cell Module '{m.Placeholder}' has invalid maximum cells: '{m.Fields[1]}'");
                }
            }
            var keys       = e.ToStringKeys().Take((int)maxCells).ToList();
            var columnDiff = keys.Count() - m.Table.ColumnCount + m.CellIndex;

            for (int i = 0; i < columnDiff; i++)
            {
                m.Table.InsertColumn();
            }
            for (int i = 0; i < keys.Count(); i++)
            {
                var s = $"{TemplConst.MatchOpen}{TemplConst.Prefix.Text}{TemplConst.FieldSep}{m.Fields[0]}[{keys[i]}]{TemplConst.MatchClose}";
                var p = m.Row.Cells[i + m.CellIndex].Paragraphs.Last();
                p.InsertParagraphAfterSelf(m.Paragraph).ReplaceText(m.Paragraph.Text, s);
                p.Remove(trackChanges: false);
                TemplDoc.CellCopyProperties(m.Cell, m.Row.Cells[i + m.CellIndex]);
            }
            m.RemovePlaceholder();
            m.Removed = true;
            return(m);
        }
Exemple #2
0
        /// <summary>
        /// Stores a named document state
        /// </summary>
        /// <param name="document"></param>
        /// <param name="name"></param>
        public void AddState(TemplDoc document, string name)
        {
            var state = document.Copy();

            state.Filename = $"{States.Count}-{name}";
            States.Add(state);
        }
Exemple #3
0
        }                   // Empty constructor is private

        /// <summary>
        /// Initialises a document template, based on the supplied document object
        /// </summary>
        public static Templ Load(TemplDoc document, bool useDefaultModules = true)
        {
            return(new Templ()
            {
                Document = document,
                Debugger = new TemplDebugger(),
                ActiveModules = (useDefaultModules ? DefaultModules : new List <TemplModule>())
            });
        }
Exemple #4
0
        public override TemplMatchTable Handler(DocX doc, object model, TemplMatchTable m)
        {
            var width = m.Table.Rows.First().Cells.Count;
            var keys  = TemplModelEntry.Get(model, m.Body).ToStringKeys();
            var nrows = (int)Math.Ceiling(keys.Count() / (float)width);

            m.Validate();
            m.RemovePlaceholder();
            for (int n = 0; n < width; n++)
            {
                if (n != m.CellIndex)
                {
                    TemplDoc.CellCopyContents(m.Cell, m.Row.Cells[n]);
                }
            }
            Row row = m.Row;

            for (int keyIdx = 0; keyIdx < nrows * width; keyIdx++)
            {
                if (keyIdx % width == 0)
                {
                    row = m.Table.InsertRow(m.Row, m.RowIndex + keyIdx / width + 1);
                }
                Cell cell = row.Cells[keyIdx % width];
                if (keyIdx < keys.Count())
                {
                    new TemplCollectionModule().BuildFromScope(doc, model, cell.Paragraphs, $"{m.Body}[{keys[keyIdx]}]");
                }
                else
                {
                    TemplDoc.CellClear(cell);
                }
            }
            m.Row.Remove();
            m.Removed = true;
            return(m);
        }
Exemple #5
0
 public override IEnumerable <TemplMatchText> FindAll(DocX doc, TemplRegex rxp)
 {
     return(TemplMatchText.Find(rxp, TemplDoc.Paragraphs(doc)));
 }
Exemple #6
0
 public override IEnumerable <TemplMatchText> FindAll(DocX doc, TemplRegex rxp)
 {
     // Expecting only 1 match per paragraph
     return(TemplMatchText.Find(rxp, TemplDoc.Paragraphs(doc), 1));
 }
Exemple #7
0
 /// <summary>
 /// Initialises a document template, based on the supplied document object
 /// </summary>
 public static Templ Load(TemplDoc document, bool useDefaultModules = true)
 {
     return new Templ()
     {
         Document = document,
         Debugger = new TemplDebugger(),
         ActiveModules = (useDefaultModules ? DefaultModules : new List<TemplModule>())
     };
 }
Exemple #8
0
 /// <summary>
 /// Stores a named document state
 /// </summary>
 /// <param name="document"></param>
 /// <param name="name"></param>
 public void AddState(TemplDoc document, string name)
 {
     var state = document.Copy();
     state.Filename = $"{States.Count}-{name}";
     States.Add(state);
 }