Esempio n. 1
0
        public async Task <string> ExportDocx(string docId)
        {
            XieChar[] text = null;
            lock (lockObject)
            {
                ensureDocLoaded(docId);
                var doc = docs.Find(x => x.DocId == docId);
                if (doc == null)
                {
                    return(null);
                }
                text = new XieChar[doc.HeadText.Length];
                for (int i = 0; i < text.Length; ++i)
                {
                    text[i] = doc.HeadText[i];
                }
            }
            var exportFileName = docId + "-" + ShortIdGenerator.Next() + ".docx";
            var exportFilePath = Path.Combine(options.ExportsFolder, exportFileName);
            var exporter       = new DocxExporter(composer, text, exportFilePath);
            await exporter.Export();

            return(exportFileName);
        }
Esempio n. 2
0
        public static ChangeSet Follow(ChangeSet a, ChangeSet b)
        {
            if (a.LengthBefore != b.LengthBefore)
            {
                throw new ArgumentException("Two change sets must have same LengthBefore.");
            }
            ChangeSet res = new ChangeSet {
                LengthBefore = a.LengthAfter
            };
            int ixa = 0, ixb = 0;

            //int ofs = 0;
            while (ixa < a.Items.Count || ixb < b.Items.Count)
            {
                if (ixa == a.Items.Count)
                {
                    // Insertions in B become insertions
                    if (b.Items[ixb] is XieChar)
                    {
                        res.Items.Add(b.Items[ixb]);
                    }
                    ++ixb;
                    continue;
                }
                if (ixb == b.Items.Count)
                {
                    // Insertions in A become retained characters
                    //if (a.Items[ixa] is XieChar) res.Items.Add(ixa + ofs);
                    if (a.Items[ixa] is XieChar)
                    {
                        res.Items.Add(ixa);
                    }
                    ++ixa;
                    continue;
                }
                // We got stuff in both
                XieChar ca = a.Items[ixa] as XieChar;
                XieChar cb = b.Items[ixb] as XieChar;
                // Both are kept characters: sync up position, and keep what's kept in both
                if (ca == null && cb == null)
                {
                    int vala = (int)a.Items[ixa];
                    int valb = (int)b.Items[ixb];
                    if (vala == valb)
                    {
                        //res.Items.Add(vala + ofs);
                        res.Items.Add(ixa);
                        ++ixa; ++ixb;
                        continue;
                    }
                    else if (vala < valb)
                    {
                        ++ixa;
                    }
                    else
                    {
                        ++ixb;
                    }
                    continue;
                }
                // Insertions in A become retained characters
                // We consume A first
                if (ca != null)
                {
                    res.Items.Add(ixa);
                    ++ixa;
                    continue;
                }
                // Insertions in B become insertions
                else
                {
                    res.Items.Add(b.Items[ixb]);
                    ++ixb;
                    continue;
                }
            }
            res.LengthAfter = res.Items.Count;
            return(res);
        }
Esempio n. 3
0
        public static ChangeSet Merge(ChangeSet a, ChangeSet b)
        {
            if (a.LengthBefore != b.LengthBefore)
            {
                throw new ArgumentException("Two change sets must have same LengthBefore.");
            }
            ChangeSet res = new ChangeSet {
                LengthBefore = a.LengthBefore
            };
            int ixa = 0, ixb = 0;

            while (ixa < a.Items.Count || ixb < b.Items.Count)
            {
                if (ixa == a.Items.Count)
                {
                    if (b.Items[ixb] is XieChar)
                    {
                        res.Items.Add(b.Items[ixb]);
                    }
                    ++ixb;
                    continue;
                }
                if (ixb == b.Items.Count)
                {
                    if (a.Items[ixa] is XieChar)
                    {
                        res.Items.Add(a.Items[ixa]);
                    }
                    ++ixa;
                    continue;
                }
                // We got stuff in both
                XieChar ca = a.Items[ixa] as XieChar;
                XieChar cb = b.Items[ixb] as XieChar;
                // Both are kept characters: sync up position, and keep what's kept in both
                if (ca == null && cb == null)
                {
                    int vala = (int)a.Items[ixa];
                    int valb = (int)b.Items[ixb];
                    if (vala == valb)
                    {
                        res.Items.Add(vala);
                        ++ixa; ++ixb;
                        continue;
                    }
                    else if (vala < valb)
                    {
                        ++ixa;
                    }
                    else
                    {
                        ++ixb;
                    }
                    continue;
                }
                // Both are insertions: insert both, in lexicographical order (so merge is commutative)
                if (ca != null && cb != null)
                {
                    if (ca.CompareTo(cb) < 0)
                    {
                        res.Items.Add(ca);
                        res.Items.Add(cb);
                    }
                    else
                    {
                        res.Items.Add(cb);
                        res.Items.Add(ca);
                    }
                    ++ixa; ++ixb;
                    continue;
                }
                // If only one is an insertion, keep that, and advance in that changeset
                if (ca != null)
                {
                    res.Items.Add(ca);
                    ++ixa;
                }
                else
                {
                    res.Items.Add(cb);
                    ++ixb;
                }
            }
            res.LengthAfter = res.Items.Count;
            return(res);
        }