public EpubWriter() { var opf = new OpfDocument { EpubVersion = EpubVersion.Epub3 }; opf.Metadata.Dates.Add(new OpfMetadataDate { Text = DateTimeOffset.UtcNow.ToString("o") }); opf.Manifest.Items.Add(new OpfManifestItem { Id = "ncx", Href = "toc.ncx", MediaType = ContentType.ContentTypeToMimeType[EpubContentType.DtbookNcx] }); opf.Spine.Toc = "ncx"; format = new EpubFormat { Opf = opf, Nav = new NavDocument(), Ncx = new NcxDocument() }; format.Nav.Head.Dom = new XElement(NavElements.Head); format.Nav.Body.Dom = new XElement( NavElements.Body, new XElement(NavElements.Nav, new XAttribute(NavNav.Attributes.Type, NavNav.Attributes.TypeValues.Toc), new XElement(NavElements.Ol))); resources = new EpubResources(); }
public static string Format(OpfDocument opf) { var root = new XElement(OpfElements.Package); root.Add(new XAttribute("xmlns", Constants.OpfNamespace)); root.Add(new XAttribute(XNamespace.Xmlns + "dc", Constants.OpfMetadataNamespace)); // This attribute is required, but some books don't have it. So we leave it as it is. if (!string.IsNullOrWhiteSpace(opf.UniqueIdentifier)) { root.Add(new XAttribute(OpfDocument.Attributes.UniqueIdentifier, opf.UniqueIdentifier)); } string versionString; switch (opf.EpubVersion) { case EpubVersion.Epub2: versionString = "2.0"; break; case EpubVersion.Epub3: versionString = "3.0"; break; default: throw new EpubWriteException($"Unknown version: {opf.EpubVersion}"); } root.Add(new XAttribute(OpfDocument.Attributes.Version, versionString)); root.Add(WriteMetadata(opf.Metadata, opf.EpubVersion)); root.Add(WriteManifest(opf.Manifest)); root.Add(WriteSpine(opf.Spine)); if (opf.Guide != null) { root.Add(WriteGuide(opf.Guide)); } var xml = Constants.XmlDeclaration + "\n" + root; return(xml); }
private XElement LoadFiles() { //Get Files LIst OpfDocument OpfDoc = new OpfDocument(); List <string> htmlFileLIst = OpfDoc.GetFilesList("html"); List <string> t = htmlFileLIst; //Load text from Chapter MyHtmlDocument htmlDoc = new MyHtmlDocument(); Dictionary <string, DetectedHeaders> DetectText = htmlDoc.FindHeaderTextInFile(t); //Create NavDetails ENtry for Each FIles List <NavDetails> FilesToAdd = new List <NavDetails>(); foreach (var item in DetectText) { string Text = String.Empty; Text = Variables.TextInChapters == true ? item.Value.Result.Count == 0 ? item.Key : item.Value.Result[0] : item.Key; FilesToAdd.Add(new NavDetails(Utils.GetId(item.Key), item.Key, Text)); } List <XElement> list = new List <XElement>(); //Converts the navdetails to xml foreach (var item in FilesToAdd) { XElement navPoint = ConvertToXElement(item); if (navPoint != null) { list.Add(navPoint); } } XElement navMap = new XElement(ns + "navMap", list); //return the new xml file return(navMap); }
public static string Format(OpfDocument opf) { var root = new XElement(OpfElements.Package); root.Add(new XAttribute("xmlns", Constants.OpfNamespace)); root.Add(new XAttribute(XNamespace.Xmlns + "dc", Constants.OpfMetadataNamespace)); string versionString; switch (opf.EpubVersion) { case EpubVersion.Epub2: versionString = "2.0"; break; case EpubVersion.Epub3: versionString = "3.0"; break; default: throw new EpubWriteException($"Unknown version: {opf.EpubVersion}"); } root.Add(new XAttribute(OpfDocument.Attributes.Version, versionString)); root.Add(WriteMetadata(opf.Metadata, opf.EpubVersion)); root.Add(WriteManifest(opf.Manifest)); root.Add(WriteSpine(opf.Spine)); if (opf.Guide != null) { root.Add(WriteGuide(opf.Guide)); } var xml = Constants.XmlDeclaration + "\n" + root; return(xml); }
private void AssertOpf(OpfDocument expected, OpfDocument actual) { Assert.Equal(expected == null, actual == null); if (expected != null && actual != null) { Assert.Equal(expected.EpubVersion, actual.EpubVersion); Assert.Equal(expected.Metadata == null, actual.Metadata == null); if (expected.Metadata != null && actual.Metadata != null) { AssertCreators(expected.Metadata.Creators, actual.Metadata.Creators); AssertCreators(expected.Metadata.Contributors, actual.Metadata.Contributors); AssertCollection(expected.Metadata.Dates, actual.Metadata.Dates, (a, b) => { Assert.Equal(a.Text, b.Text); Assert.Equal(a.Event, b.Event); }); AssertCollection(expected.Metadata.Identifiers, actual.Metadata.Identifiers, (a, b) => { Assert.Equal(a.Id, b.Id); Assert.Equal(a.Scheme, b.Scheme); Assert.Equal(a.Text, b.Text); }); AssertCollection(expected.Metadata.Metas, actual.Metadata.Metas, (a, b) => { Assert.Equal(a.Id, b.Id); Assert.Equal(a.Name, b.Name); Assert.Equal(a.Property, b.Property); Assert.Equal(a.Refines, b.Refines); Assert.Equal(a.Scheme, b.Scheme); Assert.Equal(a.Text, b.Text); }); AssertPrimitiveCollection(expected.Metadata.Coverages, actual.Metadata.Coverages, "Coverage"); AssertPrimitiveCollection(expected.Metadata.Descriptions, actual.Metadata.Descriptions, "Description"); AssertPrimitiveCollection(expected.Metadata.Languages, actual.Metadata.Languages, "Language"); AssertPrimitiveCollection(expected.Metadata.Publishers, actual.Metadata.Publishers, "Publisher"); AssertPrimitiveCollection(expected.Metadata.Relations, actual.Metadata.Relations, "Relation"); AssertPrimitiveCollection(expected.Metadata.Rights, actual.Metadata.Rights, "Right"); AssertPrimitiveCollection(expected.Metadata.Sources, actual.Metadata.Sources, "Source"); AssertPrimitiveCollection(expected.Metadata.Subjects, actual.Metadata.Subjects, "Subject"); AssertPrimitiveCollection(expected.Metadata.Titles, actual.Metadata.Titles, "Title"); AssertPrimitiveCollection(expected.Metadata.Types, actual.Metadata.Types, "Type"); } Assert.Equal(expected.Guide == null, actual.Guide == null); if (expected.Guide != null && actual.Guide != null) { AssertCollection(expected.Guide.References, actual.Guide.References, (a, b) => { Assert.Equal(a.Title, b.Title); Assert.Equal(a.Type, b.Type); Assert.Equal(a.Href, b.Href); }); } Assert.Equal(expected.Manifest == null, actual.Manifest == null); if (expected.Manifest != null && actual.Manifest != null) { AssertCollection(expected.Manifest.Items, actual.Manifest.Items, (a, b) => { Assert.Equal(a.Fallback, b.Fallback); Assert.Equal(a.FallbackStyle, b.FallbackStyle); Assert.Equal(a.Href, b.Href); Assert.Equal(a.Id, b.Id); Assert.Equal(a.MediaType, b.MediaType); Assert.Equal(a.RequiredModules, b.RequiredModules); Assert.Equal(a.RequiredNamespace, b.RequiredNamespace); AssertPrimitiveCollection(a.Properties, b.Properties, "Item.Property"); }); } Assert.Equal(expected.Spine == null, actual.Spine == null); if (expected.Spine != null && actual.Spine != null) { Assert.Equal(expected.Spine.Toc, actual.Spine.Toc); AssertCollection(expected.Spine.ItemRefs, actual.Spine.ItemRefs, (a, b) => { Assert.Equal(a.Id, b.Id); Assert.Equal(a.IdRef, b.IdRef); Assert.Equal(a.Linear, b.Linear); AssertPrimitiveCollection(a.Properties, b.Properties, "ItemRef.Property"); }); } Assert.Equal(expected.FindCoverPath(), actual.FindCoverPath()); Assert.Equal(expected.FindNavPath(), actual.FindNavPath()); Assert.Equal(expected.FindNcxPath(), actual.FindNcxPath()); } }
public static OpfDocument Read(XDocument xml) { if (xml == null) { throw new ArgumentNullException(nameof(xml)); } if (xml.Root == null) { throw new ArgumentException("XML document has no root element.", nameof(xml)); } Func <XElement, OpfMetadataCreator> readCreator = elem => new OpfMetadataCreator { Role = (string)elem.Attribute(OpfMetadataCreator.Attributes.Role), FileAs = (string)elem.Attribute(OpfMetadataCreator.Attributes.FileAs), AlternateScript = (string)elem.Attribute(OpfMetadataCreator.Attributes.AlternateScript), Text = elem.Value }; var epubVersion = GetAndValidateVersion((string)xml.Root.Attribute(OpfDocument.Attributes.Version)); var metadata = xml.Root.Element(OpfElements.Metadata); var guide = xml.Root.Element(OpfElements.Guide); var spine = xml.Root.Element(OpfElements.Spine); var package = new OpfDocument { UniqueIdentifier = (string)xml.Root.Attribute(OpfDocument.Attributes.UniqueIdentifier), EpubVersion = epubVersion, Metadata = new OpfMetadata { Creators = metadata?.Elements(OpfElements.Creator).AsObjectList(readCreator), Contributors = metadata?.Elements(OpfElements.Contributor).AsObjectList(readCreator), Coverages = metadata?.Elements(OpfElements.Coverages).AsStringList(), Dates = metadata?.Elements(OpfElements.Date).AsObjectList(elem => new OpfMetadataDate { Text = elem.Value, Event = (string)elem.Attribute(OpfMetadataDate.Attributes.Event) }), Descriptions = metadata?.Elements(OpfElements.Description).AsStringList(), Formats = metadata?.Elements(OpfElements.Format).AsStringList(), Identifiers = metadata?.Elements(OpfElements.Identifier).AsObjectList(elem => new OpfMetadataIdentifier { Id = (string)elem.Attribute(OpfMetadataIdentifier.Attributes.Id), Scheme = (string)elem.Attribute(OpfMetadataIdentifier.Attributes.Scheme), Text = elem.Value }), Languages = metadata?.Elements(OpfElements.Language).AsStringList(), Metas = metadata?.Elements(OpfElements.Meta).AsObjectList(elem => new OpfMetadataMeta { Id = (string)elem.Attribute(OpfMetadataMeta.Attributes.Id), Name = (string)elem.Attribute(OpfMetadataMeta.Attributes.Name), Refines = (string)elem.Attribute(OpfMetadataMeta.Attributes.Refines), Scheme = (string)elem.Attribute(OpfMetadataMeta.Attributes.Scheme), Property = (string)elem.Attribute(OpfMetadataMeta.Attributes.Property), Text = epubVersion == EpubVersion.Epub2 ? (string)elem.Attribute(OpfMetadataMeta.Attributes.Content) : elem.Value }), Publishers = metadata?.Elements(OpfElements.Publisher).AsStringList(), Relations = metadata?.Elements(OpfElements.Relation).AsStringList(), Rights = metadata?.Elements(OpfElements.Rights).AsStringList(), Sources = metadata?.Elements(OpfElements.Source).AsStringList(), Subjects = metadata?.Elements(OpfElements.Subject).AsStringList(), Titles = metadata?.Elements(OpfElements.Title).AsStringList(), Types = metadata?.Elements(OpfElements.Type).AsStringList() }, Guide = guide == null ? null : new OpfGuide { References = guide.Elements(OpfElements.Reference).AsObjectList(elem => new OpfGuideReference { Title = (string)elem.Attribute(OpfGuideReference.Attributes.Title), Type = (string)elem.Attribute(OpfGuideReference.Attributes.Type), Href = (string)elem.Attribute(OpfGuideReference.Attributes.Href) }) }, Manifest = new OpfManifest { Items = xml.Root.Element(OpfElements.Manifest)?.Elements(OpfElements.Item).AsObjectList(elem => new OpfManifestItem { Fallback = (string)elem.Attribute(OpfManifestItem.Attributes.Fallback), FallbackStyle = (string)elem.Attribute(OpfManifestItem.Attributes.FallbackStyle), Href = (string)elem.Attribute(OpfManifestItem.Attributes.Href), Id = (string)elem.Attribute(OpfManifestItem.Attributes.Id), MediaType = (string)elem.Attribute(OpfManifestItem.Attributes.MediaType), Properties = ((string)elem.Attribute(OpfManifestItem.Attributes.Properties))?.Split(' ') ?? new string[0], RequiredModules = (string)elem.Attribute(OpfManifestItem.Attributes.RequiredModules), RequiredNamespace = (string)elem.Attribute(OpfManifestItem.Attributes.RequiredNamespace) }) }, Spine = new OpfSpine { ItemRefs = spine?.Elements(OpfElements.ItemRef).AsObjectList(elem => new OpfSpineItemRef { IdRef = (string)elem.Attribute(OpfSpineItemRef.Attributes.IdRef), Linear = (string)elem.Attribute(OpfSpineItemRef.Attributes.Linear) != "no", Id = (string)elem.Attribute(OpfSpineItemRef.Attributes.Id), Properties = ((string)elem.Attribute(OpfSpineItemRef.Attributes.Properties))?.Split(' ').ToList() ?? new List <string>() }), Toc = spine?.Attribute(OpfSpine.Attributes.Toc)?.Value } }; return(package); }
private void AssertOpf(OpfDocument expected, OpfDocument actual) { Assert.AreEqual(expected == null, actual == null, nameof(actual)); if (expected != null && actual != null) { Assert.AreEqual(expected.EpubVersion, actual.EpubVersion, nameof(actual.EpubVersion)); Assert.AreEqual(expected.Metadata == null, actual.Metadata == null, nameof(actual.Metadata)); if (expected.Metadata != null && actual.Metadata != null) { AssertCreators(expected.Metadata.Creators, actual.Metadata.Creators, nameof(actual.Metadata.Creators)); AssertCreators(expected.Metadata.Contributors, actual.Metadata.Contributors, nameof(actual.Metadata.Contributors)); AssertCollection(expected.Metadata.Dates, actual.Metadata.Dates, nameof(actual.Metadata.Dates), (a, b) => { Assert.AreEqual(a.Text, b.Text, "Date.Text"); Assert.AreEqual(a.Event, b.Event, "Date.Event"); }); AssertCollection(expected.Metadata.Identifiers, actual.Metadata.Identifiers, nameof(actual.Metadata.Identifiers), (a, b) => { Assert.AreEqual(a.Id, b.Id, "Identifier.Id"); Assert.AreEqual(a.Scheme, b.Scheme, "Identifier.Scheme"); Assert.AreEqual(a.Text, b.Text, "Identifier.Text"); }); AssertCollection(expected.Metadata.Metas, actual.Metadata.Metas, nameof(actual.Metadata.Metas), (a, b) => { Assert.AreEqual(a.Id, b.Id, "Meta.Id"); Assert.AreEqual(a.Name, b.Name, "Meta.Name"); Assert.AreEqual(a.Property, b.Property, "Meta.Property"); Assert.AreEqual(a.Refines, b.Refines, "Meta.Refines"); Assert.AreEqual(a.Scheme, b.Scheme, "Meta.Scheme"); Assert.AreEqual(a.Text, b.Text, "Meta.Text"); }); AssertPrimitiveCollection(expected.Metadata.Coverages, actual.Metadata.Coverages, "Coverages", "Coverage"); AssertPrimitiveCollection(expected.Metadata.Descriptions, actual.Metadata.Descriptions, "Descriptions", "Description"); AssertPrimitiveCollection(expected.Metadata.Languages, actual.Metadata.Languages, "Languages", "Language"); AssertPrimitiveCollection(expected.Metadata.Publishers, actual.Metadata.Publishers, "Publishers", "Publisher"); AssertPrimitiveCollection(expected.Metadata.Relations, actual.Metadata.Relations, "Relations", "Relation"); AssertPrimitiveCollection(expected.Metadata.Rights, actual.Metadata.Rights, "Rights", "Right"); AssertPrimitiveCollection(expected.Metadata.Sources, actual.Metadata.Sources, "Sources", "Source"); AssertPrimitiveCollection(expected.Metadata.Subjects, actual.Metadata.Subjects, "Subjects", "Subject"); AssertPrimitiveCollection(expected.Metadata.Titles, actual.Metadata.Titles, "Titles", "Title"); AssertPrimitiveCollection(expected.Metadata.Types, actual.Metadata.Types, "Types", "Type"); } Assert.AreEqual(expected.Guide == null, actual.Guide == null, nameof(actual.Guide)); if (expected.Guide != null && actual.Guide != null) { AssertCollection(expected.Guide.References, actual.Guide.References, nameof(actual.Guide.References), (a, b) => { Assert.AreEqual(a.Title, b.Title, "Reference.Title"); Assert.AreEqual(a.Type, b.Type, "Reference.Type"); Assert.AreEqual(a.Href, b.Href, "Reference.Href"); }); } Assert.AreEqual(expected.Manifest == null, actual.Manifest == null, nameof(actual.Manifest)); if (expected.Manifest != null && actual.Manifest != null) { AssertCollection(expected.Manifest.Items, actual.Manifest.Items, nameof(actual.Manifest.Items), (a, b) => { Assert.AreEqual(a.Fallback, b.Fallback, "Item.Fallback"); Assert.AreEqual(a.FallbackStyle, b.FallbackStyle, "Item.FallbackStyle"); Assert.AreEqual(a.Href, b.Href, "Item.Href"); Assert.AreEqual(a.Id, b.Id, "Item.Id"); Assert.AreEqual(a.MediaType, b.MediaType, "Item.MediaType"); Assert.AreEqual(a.RequiredModules, b.RequiredModules, "Item.RequiredModules"); Assert.AreEqual(a.RequiredNamespace, b.RequiredNamespace, "Item.RequiredNamespace"); AssertPrimitiveCollection(a.Properties, b.Properties, "Item.Properties", "Item.Property"); }); } Assert.AreEqual(expected.Spine == null, actual.Spine == null, nameof(actual.Spine)); if (expected.Spine != null && actual.Spine != null) { Assert.AreEqual(expected.Spine.Toc, actual.Spine.Toc, nameof(actual.Spine.Toc)); AssertCollection(expected.Spine.ItemRefs, actual.Spine.ItemRefs, nameof(actual.Spine.ItemRefs), (a, b) => { Assert.AreEqual(a.Id, b.Id, "ItemRef.Id"); Assert.AreEqual(a.IdRef, b.IdRef, "ItemRef.IdRef"); Assert.AreEqual(a.Linear, b.Linear, "ItemRef.Linear"); AssertPrimitiveCollection(a.Properties, b.Properties, "ItemRef.Properties", "ItemRef.Property"); }); } } }
/* * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ * @@@@ Method @@@@ * @@@@****************************Read Epub ******************************************************@@@@ * @@@@ @@@@ * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */ //------------method geeting smil fil ----------- public void ProcessEpub(string currentFile) { try { string EpubNummerOnly = Path.GetFileNameWithoutExtension(currentFile); // Read an epub file EpubBook book = EpubReader.Read(currentFile); // Read metadata string title = book.Title; FinalReportlist.Add("Title of book = " + title); //******Author****** var authors = book.Authors; int Authors_count = 0; string Authors_Metadata = ""; foreach (var eachData in authors) { Authors_count++; Authors_Metadata = Authors_Metadata + " (" + eachData + "), "; } FinalReportlist.Add("Author of book= (" + Authors_count + ")" + Authors_Metadata); //******images*********** ICollection <EpubByteFile> images = book.Resources.Images; FinalReportlist.Add("Images in book= (" + images.Count + ")"); //******other metadata*********** // Access internal EPUB format specific data structures. EpubFormat format = book.Format; OcfDocument ocf = format.Ocf; OpfDocument opf = format.Opf; NcxDocument ncx = format.Ncx; NavDocument nav = format.Nav; var bookVersion = format.Opf.EpubVersion; var bookIsbnID = format.Opf.Metadata.Identifiers; var allMetadata = opf.Metadata.Languages; string language_Metadata = ""; foreach (var eachData in allMetadata) { language_Metadata = language_Metadata + eachData + ", "; } FinalReportlist.Add("Language of of book= (" + allMetadata.Count + ")" + language_Metadata); FinalReportlist.Add("version of book= " + bookVersion); // Get table of contents FinalReportlist.Add("\n*****Table of Content*****"); ICollection <EpubChapter> chapters = book.TableOfContents; foreach (var eachH1chapter in chapters) { FinalReportlist.Add(eachH1chapter.Title); //*******writing h2 Chapter**** if (eachH1chapter.SubChapters.Count >= 1) { var h2chapter = eachH1chapter.SubChapters; foreach (var eachH2chapter in h2chapter) { FinalReportlist.Add("\t" + eachH2chapter.Title); //*******writing h3 Chapter**** if (eachH2chapter.SubChapters.Count >= 1) { var h3chapter = eachH2chapter.SubChapters; foreach (var eachH3chapter in h3chapter) { FinalReportlist.Add("\t\t" + eachH3chapter.Title); } } } } } }// try end catch (Exception e) { string error = "\n Error in syncStart " + Program.Current_bookPath; Console.WriteLine(error + e); Program.Global_error = Program.Global_error + error; } // catch end } //syncStart end