Exemplo n.º 1
0
        public void GetDirectoryName_WithDirectory_ReturnsDirectory()
        {
            const string fullPath = "test/one.html";

            var result = EpubPathHelper.GetDirectoryName(fullPath);

            Assert.AreEqual("test", result);
        }
Exemplo n.º 2
0
        private Task <File> CreateFileFromManifestItem(ManifestItem item, string opfDirectory, IZip zip)
        {
            var path     = EpubPathHelper.ExpandPath(opfDirectory, item.Href !);
            var fileName = EpubPathHelper.GetFileName(item.Href !);

            return(Task.FromResult(
                       new File(fileName, path, item.ContentType, zip.GetFileContent(path))));
        }
Exemplo n.º 3
0
        public void GetDirectoryName_PathIsRootWithoutPrecedingSeparator_ReturnsEmpty()
        {
            const string fullPath = "one.html";

            var result = EpubPathHelper.GetDirectoryName(fullPath);

            Assert.AreEqual(string.Empty, result);
        }
Exemplo n.º 4
0
        public void GetDirectoryName_PathIsRootWithPrecedingSeparator_ReturnsSeparator()
        {
            const string fullPath = "/one.html";

            var result = EpubPathHelper.GetDirectoryName(fullPath);

            Assert.AreEqual("/", result);
        }
Exemplo n.º 5
0
        public void GetFileName_WithPath_ReturnsFileName()
        {
            const string fullPath = "test/one.html";

            var result = EpubPathHelper.GetFileName(fullPath);

            Assert.AreEqual("one.html", result);
        }
Exemplo n.º 6
0
        public void ExpandPath_WithRelativePath_ExpandsPath()
        {
            const string extension = "test/ch1/../ch2/ch2.html";

            var result = EpubPathHelper.ExpandPath(extension);

            Assert.AreEqual("test/ch2/ch2.html", result);
        }
Exemplo n.º 7
0
        public IEnumerable <NavigationPoint> ExtractNavigationPoints(
            XmlStructureFile xHtmlToc, IEnumerable <File> files)
        {
            var navigationOrderProcessor = NavigationOrderProcessor.Create();
            var navElement = GetRootOrderedList(xHtmlToc.Doc);

            return(ProcessOrderedListRecursive(
                       navElement, navigationOrderProcessor, EpubPathHelper.GetDirectoryName(xHtmlToc.Path), files).ToArray());
        }
Exemplo n.º 8
0
        public void Append_WithFolderWithoutTrailingDirectorySeparatorAndFile_ExpandsPath()
        {
            const string folderPath = "test/proj1";
            const string fileName   = "one.html";

            var result = EpubPathHelper.Append(folderPath, fileName);

            Assert.AreEqual(folderPath + '/' + fileName, result);
        }
Exemplo n.º 9
0
        public void Append_WithFolderAndFile_ExpandsPath()
        {
            const string folderPath = "test/proj1/";
            const string fileName   = "one.html";

            var result = EpubPathHelper.Append(folderPath, fileName);

            Assert.AreEqual(folderPath + fileName, result);
        }
Exemplo n.º 10
0
        public void ExpandPath_WithLevelUpInRelativeAndBasePath_ExpandsPath()
        {
            const string baseDirectory = "test/ch1/ch2/..";
            const string extension     = "../../ch2/ch3/../ch2.html";

            var result = EpubPathHelper.ExpandPath(baseDirectory, extension);

            Assert.AreEqual("ch2/ch2.html", result);
        }
Exemplo n.º 11
0
        public void ExpandPath_BasePathHasDirectorySeparatorAtEnd_ExpandsPath()
        {
            const string baseDirectory = "test/ch1/";
            const string extension     = "../../ch2/ch2.html";

            var result = EpubPathHelper.ExpandPath(baseDirectory, extension);

            Assert.AreEqual("ch2/ch2.html", result);
        }
Exemplo n.º 12
0
        public IEnumerable <NavigationPoint> ExtractNavigationPoints(XmlStructureFile ncx, IEnumerable <File> files)
        {
            var root     = ncx.Doc.Root;
            var basePath = EpubPathHelper.GetDirectoryName(ncx.Path);

            var navMap = root.Elements()
                         .Single(element => element.Name.Equals(XmlNamespaces.Ncx + NcxXmlNames.NavMapElementName));
            var orderProcessor = NavigationOrderProcessor.Create(navMap);

            return(FilterAndSelectChildNavPoints(navMap, orderProcessor, basePath, files));
        }
Exemplo n.º 13
0
        public Task <XmlStructureFile> LoadXHtmlToc(
            IEnumerable <ManifestItem> manifestItems, string relativeFilePathsAnchor, IEnumerable <File> files)
        {
            var xHtmlTocRelativePath = manifestItems.Single(
                i => OpfXmlNames.NavPropertiesAttributeValue.EqualsIgnoreCaseWithNull(i.Properties)).Href !;
            var xHtmlTocPath = EpubPathHelper.ExpandPath(
                EpubPathHelper.GetDirectoryName(relativeFilePathsAnchor), xHtmlTocRelativePath);

            return(XmlStructureFile.LoadFromBytesAsync(
                       xHtmlTocPath, files.Single(f => f.Path.EqualsIgnoreCaseWithNull(xHtmlTocPath)).Content));
        }
Exemplo n.º 14
0
        public Task <XmlStructureFile> LoadNcx(
            IEnumerable <ManifestItem> manifestItems, string relativeFilePathsAnchor, IEnumerable <File> files)
        {
            var ncxRelativePath = manifestItems
                                  .Single(item => item.ContentType == ContentType.Ncx).Href !;
            var ncxPath = EpubPathHelper.ExpandPath(
                EpubPathHelper.GetDirectoryName(relativeFilePathsAnchor), ncxRelativePath);

            return(XmlStructureFile.LoadFromBytesAsync(
                       ncxPath, files.Single(f => f.Path.EqualsIgnoreCaseWithNull(ncxPath)).Content));
        }
Exemplo n.º 15
0
        private NavigationPoint ProcessNavPoint(
            XElement element, NavigationOrderProcessor orderProcessor, string basePath, IEnumerable <File> files)
        {
            var title = element.Elements()
                        .Single(e => e.Name.Equals(XmlNamespaces.Ncx + NcxXmlNames.NavLabelElementName))
                        .Elements().Single(e => e.Name.Equals(XmlNamespaces.Ncx + NcxXmlNames.TextElementName))
                        .Value;

            var contentText = element.Elements()
                              .Single(e => e.Name.Equals(XmlNamespaces.Ncx + NcxXmlNames.ContentElementName))
                              .GetAttributeValueOrNull(NcxXmlNames.SrcAttributeName) !;
            var filePath  = SrcTextSplitter.GetFileName(contentText);
            var elementId = SrcTextSplitter.GetElementId(contentText);

            var fileFullPath = EpubPathHelper.ExpandPath(basePath, filePath);
            var file         = files.Single(f => f.Path.EqualsIgnoreCaseWithNull(fileFullPath));

            var order = orderProcessor.GetOrder(element);

            var children = FilterAndSelectChildNavPoints(element, orderProcessor, basePath, files);

            return(new NavigationPoint(title, file, elementId, order, children));
        }
Exemplo n.º 16
0
        private NavigationPoint ProcessListItemRecursive(
            XElement listItem, NavigationOrderProcessor navigationOrderProcessor, string basePath, IEnumerable <File> files)
        {
            var linkElement = listItem.Elements()
                              .Single(element => element.Name.Equals(XmlNamespaces.XHtml + XHtmlTocXmlNames.LinkElementName));

            var title     = linkElement.Value;
            var srcPath   = linkElement.GetAttributeValueOrNull(XHtmlTocXmlNames.HrefAttributeName) !;
            var filePath  = SrcTextSplitter.GetFileName(srcPath);
            var elementId = SrcTextSplitter.GetElementId(srcPath);
            var order     = navigationOrderProcessor.GetOrder(listItem);

            var fileFullPath = EpubPathHelper.ExpandPath(basePath, filePath);
            var file         = files.Single(f => f.Path.EqualsIgnoreCaseWithNull(fileFullPath));

            var childOrderedList = listItem.Elements()
                                   .Cast <XElement?>().SingleOrDefault(
                e => e !.Name.Equals(XmlNamespaces.XHtml + XHtmlTocXmlNames.OrderedListElementName));
            var children = childOrderedList != null
                ? ProcessOrderedListRecursive(childOrderedList, navigationOrderProcessor, basePath, files).ToArray()
                : Array.Empty <NavigationPoint>();

            return(new NavigationPoint(title, file, elementId, order, children));
        }
Exemplo n.º 17
0
 public Task <File[]> ExtractFiles(
     XmlStructureFile opf, IEnumerable <ManifestItem> manifestItems, IZip zip)
 {
     return(Task.WhenAll(manifestItems.Select(
                             item => CreateFileFromManifestItem(item, EpubPathHelper.GetDirectoryName(opf.Path), zip))));
 }