示例#1
0
        public void Seed_Options_Ok()
        {
            TiledTextLayerPartSeeder seeder = new TiledTextLayerPartSeeder();

            seeder.SetSeedOptions(_seedOptions);
            seeder.Configure(new TiledTextLayerPartSeederOptions
            {
                MaxFragmentCount = 3
            });

            // item with text
            IItem item = _factory.GetItemSeeder().GetItem(1, "facet");
            TiledTextPartSeeder textSeeder = new TiledTextPartSeeder();

            textSeeder.SetSeedOptions(_seedOptions);
            item.Parts.Add(textSeeder.GetPart(_item, null, _factory));

            IPart part = seeder.GetPart(item, "fr.net.fusisoft.comment", _factory);

            Assert.NotNull(part);

            TiledTextLayerPart <CommentLayerFragment> lp =
                part as TiledTextLayerPart <CommentLayerFragment>;

            Assert.NotNull(lp);
            Assert.NotEmpty(lp.Fragments);
        }
        private static TiledTextLayerPart <CommentLayerFragment> GetPart(
            bool fragments = true)
        {
            var part = new TiledTextLayerPart <CommentLayerFragment>
            {
                ItemId    = Guid.NewGuid().ToString(),
                RoleId    = "some-role",
                CreatorId = "zeus",
                UserId    = "another"
            };

            if (fragments)
            {
                for (int i = 1; i <= 3; i++)
                {
                    part.AddFragment(new CommentLayerFragment
                    {
                        Location = $"1.{i}",
                        Tag      = i > 1 ? "scholarly" : null,
                        Text     = "Text {i}.\nEnd."
                    });
                }
            }
            return(part);
        }
示例#3
0
        private bool AppendItemContent(IItem item, XElement div)
        {
            if (IncludeComments)
            {
                div.Add(new XComment($"item {item.Title} ({item.Id})"));
            }

            int count = 0;

            foreach (string roleId in _apparatusFrIds)
            {
                TiledTextLayerPart <ApparatusLayerFragment>?part =
                    GetApparatusPart(item.Parts, roleId);
                if (part == null)
                {
                    Logger?.LogInformation($"Item {item.Id} has no {roleId} part");
                }
                else
                {
                    AppendApparatusContent(roleId, item, part, div);
                    count++;
                }
            }
            if (count == 0)
            {
                Logger?.LogInformation($"Item {item.Id} has no apparatus part");
            }
            return(count > 0);
        }
        public void GetTextAt_3LinesRange_Ok(string location, string expectedText)
        {
            TiledTextPart textPart = GetTiledTextPart(3);
            TiledTextLayerPart <CommentLayerFragment> layerPart = GetPart();

            string text = layerPart.GetTextAt(textPart, location);

            Assert.Equal(expectedText, text);
        }
        public void GetTextAt_InvalidLocation_Null()
        {
            TiledTextPart textPart = GetTiledTextPart(3);
            TiledTextLayerPart <CommentLayerFragment> layerPart = GetPart();

            string text = layerPart.GetTextAt(textPart, "12.3");

            Assert.Null(text);
        }
        private static bool PartHasOverlaps(
            TiledTextLayerPart <ApparatusLayerFragment> part)
        {
            List <TokenTextLocation> locs = (from fr in part.Fragments
                                             select TokenTextLocation.Parse(fr.Location))
                                            .ToList();

            for (int i = 0; i < locs.Count; i++)
            {
                for (int j = i + 1; j < locs.Count; j++)
                {
                    if (locs[i].Overlaps(locs[j]))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        private void AddFragmentToPart(ApparatusLayerFragment fr,
                                       TiledTextLayerPart <ApparatusLayerFragment> part,
                                       string originalLoc)
        {
            int count = part.Fragments.Count;

            // bypass part's add so that even overlapping fragments are added;
            // we will then move them all at once later, when splitting part
            TokenTextLocation loc = TokenTextLocation.Parse(fr.Location);

            if (part.Fragments.Any(f => TokenTextLocation.Parse(f.Location)
                                   .Overlaps(loc)))
            {
                Logger?.LogInformation(
                    $"Overlap for new fragment at {fr.Location}"
                    + $" (original {originalLoc}): "
                    + string.Join("; ", fr.Entries));
            }
            part.Fragments.Add(fr);
        }
        /// <summary>
        /// Splits the received apparatus layer part into 1 to 3 parts,
        /// distributing them as follows: 1) all the original entries except
        /// those under 2 and 3; 2) all the entries marked as ancient notes;
        /// 3) all the fragments marked as margin notes.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <returns>The parts: just 1 if no splitting required.</returns>
        private IList <TiledTextLayerPart <ApparatusLayerFragment> > SplitPart(
            TiledTextLayerPart <ApparatusLayerFragment> part)
        {
            TiledTextLayerPart <ApparatusLayerFragment> ancPart =
                new TiledTextLayerPart <ApparatusLayerFragment>
            {
                ItemId         = part.ItemId,
                ThesaurusScope = part.ThesaurusScope,
                CreatorId      = part.CreatorId,
                UserId         = part.UserId,
            };

            ancPart.RoleId += ":ancient";
            TiledTextLayerPart <ApparatusLayerFragment> margPart =
                new TiledTextLayerPart <ApparatusLayerFragment>
            {
                ItemId         = part.ItemId,
                ThesaurusScope = part.ThesaurusScope,
                CreatorId      = part.CreatorId,
                UserId         = part.UserId,
            };

            margPart.RoleId += ":margin";

            int ancEntryCount = 0, margEntryCount = 0;

            foreach (var fr in part.Fragments
                     .Where(f => f.Tag.Contains(TYPE_MARGIN_NOTE) ||
                            f.Entries.Any(e => e.Tag == TYPE_ANCIENT_NOTE))
                     .ToList())
            {
                ApparatusLayerFragment targetFr = null;

                // app@type=margin -> tag with margin: move whole fragment
                if (fr.Tag.Contains(TYPE_MARGIN_NOTE))
                {
                    margPart.AddFragment(fr);
                    margEntryCount += fr.Entries.Count;
                    part.Fragments.Remove(fr);
                    continue;
                }

                // else examine parts, some might belong to ancient
                var ancEntries = fr.Entries
                                 .Where(e => e.Tag == TYPE_ANCIENT_NOTE)
                                 .ToList();
                ancEntryCount += ancEntries.Count;

                // if all the entries are to be moved, move the whole fragment
                if (ancEntries.Count == fr.Entries.Count)
                {
                    ancPart.AddFragment(fr);
                    part.Fragments.Remove(fr);
                    continue;
                }

                // else move only the ancient entries to a cloned fragment
                if (targetFr == null)
                {
                    targetFr = new ApparatusLayerFragment
                    {
                        Location = fr.Location,
                        Tag      = fr.Tag
                    };
                }
                foreach (var entry in ancEntries)
                {
                    targetFr.Entries.Add(entry);
                    fr.Entries.Remove(entry);
                }
            }

            var parts = new List <TiledTextLayerPart <ApparatusLayerFragment> >
            {
                part
            };

            // check for overlaps after splitting
            if (PartHasOverlaps(part))
            {
                Logger?.LogError("Part has overlaps: "
                                 + GetFragmentLocsDump(part.Fragments));
            }

            if (ancPart.Fragments.Count > 0)
            {
                AdjustNoteFragments(ancPart.Fragments);
                parts.Add(ancPart);
                if (PartHasOverlaps(ancPart))
                {
                    Logger?.LogError("Ancient part has overlaps: "
                                     + GetFragmentLocsDump(ancPart.Fragments));
                }
            }
            if (margPart.Fragments.Count > 0)
            {
                AdjustNoteFragments(margPart.Fragments);
                parts.Add(margPart);
                if (PartHasOverlaps(margPart))
                {
                    Logger?.LogError("Margin part has overlaps: "
                                     + GetFragmentLocsDump(margPart.Fragments));
                }
            }

            return(parts);
        }
示例#9
0
        private void AppendApparatusContent(
            string roleId,
            IItem item,
            TiledTextLayerPart <ApparatusLayerFragment> part,
            XElement div)
        {
            if (IncludeComments)
            {
                div.Add(new XComment(
                            $"apparatus {roleId} ({part.Fragments.Count} in {part.Id})"));
            }

            // each fragment is an app element
            int frIndex = 0;

            foreach (ApparatusLayerFragment fr in part.Fragments)
            {
                // nope if no entries
                if (fr.Entries.Count == 0)
                {
                    continue;
                }

                // app
                if (IncludeComments)
                {
                    div.Add(new XComment($"fr {fr.Location} [{frIndex++}]"));
                }

                XElement app = new XElement(XmlHelper.TEI + "app");
                div.Add(app);

                // @type
                if (roleId == _apparatusFrIds[1])
                {
                    app.SetAttributeValue("type", "margin-note");
                }

                // map location into @from, @to
                var ft = _locMapper.Map(fr.Location, item);
                if (ft != null)
                {
                    if (ft.Item2 != null)
                    {
                        app.SetAttributeValue("from", "#" + ft.Item1);
                        app.SetAttributeValue("to", "#" + ft.Item2);
                    }
                    else
                    {
                        string target = "#" + ft.Item1;
                        app.SetAttributeValue("from", target);
                        app.SetAttributeValue("to", target);
                    }
                }

                // 2nd token of tag, if any = @type
                if (!string.IsNullOrEmpty(fr.Tag))
                {
                    int i = fr.Tag.IndexOf(' ');
                    if (i > -1)
                    {
                        app.SetAttributeValue("type", fr.Tag.Substring(i + 1));
                    }
                }

                // add fragment's entries to app
                foreach (ApparatusEntry entry in fr.Entries)
                {
                    AppendEntryContent(entry, app, part.Id);
                }
            }
        }