public EpubTextContentFile CreateIndex(List<EpubChapter> chapters, Dictionary<string, EpubTextContentFile> xhtmlFiles) { //reading content to string var _indexFileContent = _templateContent; string componentsTagString = "getComponents:"; string contentsTagString = "getContents:"; int indexOfComponentsTag = _indexFileContent.IndexOf(componentsTagString); int lengthOfComponentsTag = componentsTagString.Length; string componentInjectonPrefix = " function() {\nreturn ["; string componentInjectonPostfix = "];\n},\n"; string allComponents = BuildAllXHTMLFilesToString(xhtmlFiles); //Injecting after the <head> _indexFileContent = _indexFileContent.Insert(indexOfComponentsTag + lengthOfComponentsTag, componentInjectonPrefix + allComponents + componentInjectonPostfix); int indexOfContentTag = _indexFileContent.IndexOf(contentsTagString); string allContent = BuildChapterListToString(chapters); _indexFileContent = _indexFileContent.Insert(indexOfContentTag + contentsTagString.Length, componentInjectonPrefix + allContent + componentInjectonPostfix); //Adding a new item to the HTML list of book files EpubTextContentFile _indexMemoryItem = new EpubTextContentFile() { Content = _indexFileContent, FileName = "index.html", ContentType = EpubContentType.XHTML_1_1, ContentMimeType = "text/html" }; //currentEpubBook.Content.Html.Add("index.html", _indexMemoryItem); Debug.WriteLine(string.Format("-------- Here comes the final result:------- \n{0}\n ------------------------------", _indexFileContent)); return _indexMemoryItem; }
public static EpubContent ReadContentFiles(ZipArchive epubArchive, EpubBook book) { EpubContent result = new EpubContent { Html = new Dictionary<string, EpubTextContentFile>(), Css = new Dictionary<string, EpubTextContentFile>(), Images = new Dictionary<string, EpubByteContentFile>(), Fonts = new Dictionary<string, EpubByteContentFile>(), AllFiles = new Dictionary<string, EpubContentFile>() }; foreach (EpubManifestItem manifestItem in book.Schema.Package.Manifest) { string contentFilePath = ZipPathUtils.Combine(book.Schema.ContentDirectoryPath, manifestItem.Href); ZipArchiveEntry contentFileEntry = epubArchive.GetEntry(contentFilePath); if (contentFileEntry == null) throw new Exception(String.Format("EPUB parsing error: file {0} not found in archive.", contentFilePath)); if (contentFileEntry.Length > Int32.MaxValue) throw new Exception(String.Format("EPUB parsing error: file {0} is bigger than 2 Gb.", contentFilePath)); string fileName = manifestItem.Href; string contentMimeType = manifestItem.MediaType; EpubContentType contentType = GetContentTypeByContentMimeType(contentMimeType); switch (contentType) { case EpubContentType.XHTML_1_1: case EpubContentType.CSS: case EpubContentType.OEB1_DOCUMENT: case EpubContentType.OEB1_CSS: case EpubContentType.XML: case EpubContentType.DTBOOK: case EpubContentType.DTBOOK_NCX: EpubTextContentFile epubTextContentFile = new EpubTextContentFile { FileName = fileName, ContentMimeType = contentMimeType, ContentType = contentType }; using (Stream contentStream = contentFileEntry.Open()) { if (contentStream == null) throw new Exception(String.Format("Incorrect EPUB file: content file \"{0}\" specified in manifest is not found", fileName)); using (StreamReader streamReader = new StreamReader(contentStream)) epubTextContentFile.Content = streamReader.ReadToEnd(); } switch (contentType) { case EpubContentType.XHTML_1_1: result.Html.Add(fileName, epubTextContentFile); break; case EpubContentType.CSS: result.Css.Add(fileName, epubTextContentFile); break; } result.AllFiles.Add(fileName, epubTextContentFile); break; default: EpubByteContentFile epubByteContentFile = new EpubByteContentFile { FileName = fileName, ContentMimeType = contentMimeType, ContentType = contentType }; using (Stream contentStream = contentFileEntry.Open()) { if (contentStream == null) throw new Exception(String.Format("Incorrect EPUB file: content file \"{0}\" specified in manifest is not found", fileName)); using (MemoryStream memoryStream = new MemoryStream((int)contentFileEntry.Length)) { contentStream.CopyTo(memoryStream); epubByteContentFile.Content = memoryStream.ToArray(); } } switch (contentType) { case EpubContentType.IMAGE_GIF: case EpubContentType.IMAGE_JPEG: case EpubContentType.IMAGE_PNG: case EpubContentType.IMAGE_SVG: result.Images.Add(fileName, epubByteContentFile); break; case EpubContentType.FONT_TRUETYPE: case EpubContentType.FONT_OPENTYPE: result.Fonts.Add(fileName, epubByteContentFile); break; } result.AllFiles.Add(fileName, epubByteContentFile); break; } } return result; }