Example #1
0
        /// <summary>
        /// List files to process
        /// </summary>
        /// <param name="summaryFile">SUMMARY.md content</param>
        /// <returns>List of files</returns>
        public static ToC ParseToc(string content)
        {
            ToC parsed   = new ToC();
            var pipeline = new MarkdownPipelineBuilder().UseAutoIdentifiers(AutoIdentifierOptions.GitHub).Build();
            var doc      = Markdown.Parse(content, pipeline);

            string?     chapterTitle = string.Empty;
            List <Link>?chapterLinks = new List <Link>();

            foreach (MarkdownObject item in doc.Descendants())
            {
                if (item is HeadingBlock heading)
                {
                    if (chapterLinks.Count > 0)
                    {
                        InsertChapter(parsed, ref chapterTitle, ref chapterLinks);
                    }
                    chapterTitle = heading.Inline.FirstChild.ToString();
                    chapterLinks = new List <Link>(50);
                }
                else if (item is LinkInline link && !link.IsImage)
                {
                    if (link.FirstChild != null)
                    {
                        chapterLinks.Add(new Link(link.FirstChild.ToString() !, link.Url));
                    }
                }
            }
            InsertChapter(parsed, ref chapterTitle, ref chapterLinks);
            return(parsed);
        }
Example #2
0
 private static void InsertChapter(ToC toc, ref string?currentchapter, ref List <Link>?chapter)
 {
     if (currentchapter != null && chapter != null)
     {
         toc.AddChapter(currentchapter, chapter);
         currentchapter = null;
         chapter        = null;
     }
 }
Example #3
0
        public void TestFindTocEntry_DoesNotExist()
        {
            XDocument doc         = Utils.ReadXmlResource("UnitTestMergeWebToEpub.TestData.tocGifting.ncx");
            var       mockNcxItem = new EpubItem()
            {
                AbsolutePath = "OEPBS/toc.ncx"
            };
            var api    = Utils.FakeAbsolutePathIndex(doc, "OEPBS/toc.ncx");
            var toc    = new ToC(doc, mockNcxItem, api);
            var actual = toc.FindTocEntry("OEPBS/Text/0002_Chapter_A.xhtml");

            Assert.IsNull(actual.entries);
        }
Example #4
0
        public void TestRoundTripParseToCWithNesting()
        {
            XDocument doc         = Utils.ReadXmlResource("UnitTestMergeWebToEpub.TestData.tocWithNesting.ncx");
            var       mockNcxItem = new EpubItem()
            {
                AbsolutePath = "toc.ncx"
            };
            var api = Utils.FakeAbsolutePathIndex(doc, "toc.ncx");

            var toc   = new ToC(doc, mockNcxItem, api);
            var doc2  = toc.ToXDocument();
            var delta = XmlCompare.ElementSame(doc.Root, doc2.Root);

            Assert.IsTrue(delta.AreSame);
        }
Example #5
0
        public void TestBuildScrToTitleMap()
        {
            XDocument doc         = Utils.ReadXmlResource("UnitTestMergeWebToEpub.TestData.tocGifting.ncx");
            var       mockNcxItem = new EpubItem()
            {
                AbsolutePath = "OEPBS/toc.ncx"
            };
            var api    = Utils.FakeAbsolutePathIndex(doc, "OEPBS/toc.ncx");
            var toc    = new ToC(doc, mockNcxItem, api);
            var actual = toc.BuildScrToTitleMap();

            Assert.AreEqual(6, actual.Count);
            Assert.AreEqual("Splash pages", actual["OEPBS/Text/0000_Splash_pages.xhtml"]);
            Assert.AreEqual("Main Story: Chapter 1", actual["OEPBS/Text/0001_Chapter_1.xhtml"]);
            Assert.AreEqual("Chapter 3", actual["OEPBS/Text/0003_Chapter_3.xhtml"]);
        }
Example #6
0
        public void TestCalcNavMapDepth_Expect2_ThenExpect3()
        {
            XDocument doc         = Utils.ReadXmlResource("UnitTestMergeWebToEpub.TestData.toc.ncx");
            var       mockNcxItem = new EpubItem()
            {
                AbsolutePath = "toc.ncx"
            };
            var api = Utils.FakeAbsolutePathIndex(doc, "toc.ncx");

            var toc = new ToC(doc, mockNcxItem, api);

            Assert.AreEqual(2, toc.CalcNavMapDepth());

            toc.Entries[2].Children[0].Children.Add(new TocEntry());
            Assert.AreEqual(3, toc.CalcNavMapDepth());
        }
Example #7
0
        public RuntimeSettings CreateRuntimeSettings(Config config, ToC toc, BuildConfig current)
        {
            var settings = new RuntimeSettings
            {
                SourceDirectory    = new FsPath(_workdir),
                Configuration      = config,
                TocContents        = toc,
                MetataCache        = new Dictionary <string, string>(100),
                InlineImgCache     = new ConcurrentDictionary <string, string>(),
                CurrentBuildConfig = current,
            };

            if (string.IsNullOrEmpty(config.ImageDir))
            {
                settings.ImageDirectory = FsPath.Empty;
            }
            else
            {
                settings.ImageDirectory = settings.SourceDirectory.Combine(config.ImageDir);
            }

            return(settings);
        }