private void AddNoteToWitOrSource(IList <XmlApparatusNote> notes,
                                          ApparatusAnnotatedValue target)
        {
            int distCount = notes.Select(n => n.SectionId).Distinct().Count();

            if (distCount != notes.Count)
            {
                Logger?.LogError(
                    $"Duplicate section(s) in notes targeting {target.Value}");
            }

            StringBuilder sb        = new StringBuilder();
            int           sectionId = 1;

            foreach (XmlApparatusNote note in notes.OrderBy(n => n.SectionId))
            {
                while (sectionId < note.SectionId)
                {
                    sb.Append(NOTE_SECT_SEP);
                    sectionId++;
                }
                sb.Append(note.Value);
            }
            target.Note = ApplyMarkdown(sb.ToString());
        }
        private void AddContentToEntry(XmlApparatusVarContent content,
                                       ApparatusEntry entry)
        {
            // value
            if (!string.IsNullOrWhiteSpace(content.Value))
            {
                entry.Value = content.Value.Trim();
            }

            // ident's
            if (content.Idents.Count > 0)
            {
                entry.NormValue = string.Join(" ", content.Idents);
            }

            // notes
            if (content.Notes.Count > 0)
            {
                // corner case: only note section 1
                if (content.Notes.Count == 1 &&
                    content.Notes[0].SectionId == 1 &&
                    content.Notes[0].Target == null)
                {
                    entry.Note = ApplyMarkdown(content.Notes[0].Value);
                    return;
                }

                // first process wit/source notes, grouped by target
                foreach (var group in from n in content.Notes
                         where n.Target != null
                         group n by n.Target into g
                         select g)
                {
                    ApparatusAnnotatedValue target =
                        entry.Witnesses.Find(w => w.Value == group.Key);
                    if (target != null)
                    {
                        AddNoteToWitOrSource(group.ToList(), target);
                        continue;
                    }
                    target = entry.Authors.Find(a => a.Value == group.Key);
                    if (target != null)
                    {
                        AddNoteToWitOrSource(group.ToList(), target);
                    }
                    else
                    {
                        Logger?.LogError($"Target \"{group.Key}\" not found");
                    }
                }

                // then process untargeted notes
                StringBuilder sb       = new StringBuilder();
                int           curSect  = 1;
                HashSet <int> sections = new HashSet <int>();

                foreach (XmlApparatusNote note in content.Notes
                         .Where(n => n.Target == null)
                         .OrderBy(n => n.SectionId))
                {
                    if (sections.Contains(note.SectionId))
                    {
                        Logger?.LogError(
                            $"Note section {note.SectionId} overwritten by \"{note.Value}\"");
                    }
                    while (curSect < note.SectionId)
                    {
                        sb.Append(NOTE_SECT_SEP);
                        curSect++;
                    }
                    sb.Append(note.Value);
                    sections.Add(note.SectionId);
                }

                if (sb.Length > 0)
                {
                    entry.Note = ApplyMarkdown(sb.ToString());
                }
            }
        }