示例#1
0
        private List <XElement> GetNccElements(MergeEntry me, List <XElement> smilElements)
        {
            var nccElements = me.NccElements.Select(Utils.CloneWithBaseUri).ToList();

            //Set new id attributes on ncc elements, fixing references from smil <text> elements
            foreach (var idAttr in nccElements
                     .SelectMany(e => e.DescendantsAndSelf())
                     .Select(e => e.Attribute("id"))
                     .Where(attr => attr != null))
            {
                var newId = $"NCCID{nccIdIndex:D5}";
                nccIdIndex++;
                //Find and fix smil <text> elements src attributes, that link to ncc elements
                foreach (var textSrcAttr in smilElements
                         .Select(e => e.Element("text")?.Attribute("src"))
                         .Where(attr => Utils.IsSameFile(Utils.GetUri(attr), new Uri(me.Ncc.BaseUri))))
                {
                    var textSrcUri = Utils.GetUri(textSrcAttr);
                    if (textSrcUri.Fragment == $"#{idAttr.Value}")
                    {
                        textSrcAttr.Value = $"{NccDocumentName}#{newId}";
                    }
                }
                idAttr.Value = newId;
            }
            //Fix <a> element descendants of ncc elements, that point to the smil file
            foreach (var nccAHrefAttr in nccElements
                     .SelectMany(e => e.DescendantsAndSelf(e.Name.Namespace + "a"))
                     .Select(a => a.Attribute("href"))
                     .Where(href => href != null))
            {
                var uri = Utils.GetUri(nccAHrefAttr);
                if (Utils.IsSameFile(uri, new Uri(me.Smil.BaseUri)))
                {
                    nccAHrefAttr.Value = $"{GetSmilFileName(entryIndex)}{uri.Fragment}";
                }
            }
            //Fix heading depth
            foreach (var hd in nccElements.Where(Utils.IsHeading))
            {
                hd.Name = hd.Name.Namespace + $"h{Math.Min(me.Depth, 6)}";
            }

            return(nccElements);
        }
示例#2
0
        private List <XElement> GetContentElements(MergeEntry me, List <XElement> smilElements)
        {
            var contentElements = me.TextElements.Select(Utils.CloneWithBaseUri).ToList();

            if (contentElements.Any())
            {
                //Set new id attributes on content elements, fixing references from smil <text> elements
                foreach (var idAttr in contentElements
                         .SelectMany(e => e.DescendantsAndSelf())
                         .Select(e => e.Attribute("id"))
                         .Where(attr => attr != null))
                {
                    var newId = $"TEXTID{contentIdIndex:D5}";
                    contentIdIndex++;
                    //Find and fix smil <text> elements src attributes, that link to content elements
                    foreach (var textSrcAttr in smilElements
                             .Select(e => e.Element("text")?.Attribute("src"))
                             .Where(attr => me.ContentDocuments.Values.Any(cd =>
                                                                           Utils.IsSameFile(Utils.GetUri(attr), new Uri(cd.BaseUri)))))
                    {
                        var textSrcUri = Utils.GetUri(textSrcAttr);
                        if (textSrcUri.Fragment == $"#{idAttr.Value}")
                        {
                            textSrcAttr.Value = $"{ContentDocumentName}#{newId}";
                        }
                    }
                    idAttr.Value = newId;
                }
                //Fix <a> element descendants of ncc elements, that point to the smil file
                foreach (var contentAHrefAttr in contentElements
                         .SelectMany(e => e.DescendantsAndSelf(e.Name.Namespace + "a"))
                         .Select(a => a.Attribute("href"))
                         .Where(href => href != null))
                {
                    var uri = Utils.GetUri(contentAHrefAttr);
                    if (Utils.IsSameFile(uri, new Uri(me.Smil.BaseUri)))
                    {
                        contentAHrefAttr.Value = $"{GetSmilFileName(entryIndex)}{uri.Fragment}";
                    }
                }
            }

            return(contentElements);
        }
示例#3
0
 /// <summary>
 /// Gets macro <see cref="XElement"/>s representing an entire DTB
 /// </summary>
 /// <param name="ncc">The ncc <see cref="XDocument"/> of the DTB - must have <see cref="XObject.BaseUri"/> set</param>
 /// <returns>The macro <see cref="XElement"/>s representing the DTB</returns>
 public static IEnumerable <XElement> GetMacroElementsFromNcc(XDocument ncc)
 {
     return(MergeEntry.LoadMergeEntriesFromNcc(ncc).Select(me => me.MacroElement));
 }