Beispiel #1
0
        /// <summary>
        /// Gets XHTML content for a file.
        /// </summary>
        /// <param name="ebook"></param>
        /// <param name="navPoint"></param>
        /// <returns></returns>
        public static async Task <string> GetContentsAsync(this EBook ebook, ManifestItem manifestItem, bool embedImages = true)
        {
            var fullContentPath = Path.GetFullPath(ebook._rootFolder.Path.EnsureEnd("\\") + ebook.ContentLocation);
            var tocPath         = Path.GetFullPath(Path.GetDirectoryName(fullContentPath).EnsureEnd("\\") + ebook.Manifest["ncx"].ContentLocation);
            var filePath        = Path.GetFullPath(Path.GetDirectoryName(tocPath).EnsureEnd("\\") + manifestItem.ContentLocation);
            var contentFile     = await ebook._rootFolder.GetFileFromPathAsync(filePath.Substring(ebook._rootFolder.Path.Length));

            var contents = await FileIO.ReadTextAsync(contentFile);

            contents = WebUtility.HtmlDecode(contents);

            if (embedImages)
            {
                var contentPath  = Path.Combine(ebook._rootFolder.Path, manifestItem.ContentLocation);
                var imageMatches = new Regex(@"<img.*/>", RegexOptions.IgnoreCase).Matches(contents).OfType <Match>().ToList();

                foreach (var match in imageMatches)
                {
                    var imageNode   = HtmlNode.CreateNode(match.Value);
                    var imageSource = imageNode.Attributes["src"].Value;

                    var imgPath   = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(contentPath), imageSource));
                    var imageFile = await ebook._rootFolder.GetFileFromPathAsync(imgPath.Substring(ebook._rootFolder.Path.Length));

                    var image = await FileIO.ReadBufferAsync(imageFile);

                    var base64 = Convert.ToBase64String(image.ToArray());
                    imageNode.Attributes["src"].Value = $"data:image/{imageFile.FileType};base64,{base64}";
                    contents = contents.Replace(match.Value, imageNode.OuterHtml);
                }
            }

            return(contents);
        }
Beispiel #2
0
        public static SpineItem GetSpineItem(this EBook ebook, ManifestItem manifestItem)
        {
            var spineItem = ebook.Spine.FirstOrDefault(a => a.IdRef == manifestItem.Id);

            return(spineItem);
        }
Beispiel #3
0
        public static string GetContents(this EBook eBook, ManifestItem manifestItem, bool embedImages = false, bool embedStyles = false)
        {
            var contents = File.ReadAllText(manifestItem.ContentLocation);

            contents = WebUtility.HtmlDecode(contents);

            if (embedImages)
            {
                var imageMatches = new Regex(@"<img.*/>", RegexOptions.IgnoreCase).Matches(contents).OfType <Match>().ToList();

                foreach (var match in imageMatches)
                {
                    var node   = HtmlNode.CreateNode(match.Value);
                    var source = node.Attributes["src"].Value;

                    if (source.StartsWith("data:"))
                    {
                        continue;
                    }

                    var imagePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(manifestItem.ContentLocation), source));
                    var buffer    = File.ReadAllBytes(imagePath);
                    var base64    = Convert.ToBase64String(buffer);
                    node.Attributes["src"].Value = $"data:image/{new FileInfo(imagePath).Extension};base64,{base64}";
                    contents = contents.Replace(match.Value, node.OuterHtml);
                }
            }

            if (embedStyles)
            {
                var styleRegex   = new Regex(@"<style>(.*?)<\/style>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                var styleMatch   = styleRegex.Match(contents);
                var styleBuilder = new StringBuilder();

                if (styleMatch.Success)
                {
                    styleBuilder.Append(styleMatch.Groups[styleMatch.Groups.Count - 1]);
                }

                var stylesheetRegex   = new Regex(@"<link.*?rel=""stylesheet"".*?>", RegexOptions.IgnoreCase);
                var stylesheetMatches = stylesheetRegex.Matches(contents).OfType <Match>().ToList();

                foreach (var match in stylesheetMatches)
                {
                    var node     = HtmlNode.CreateNode(match.Value);
                    var location = node.Attributes["href"].Value;
                    var file     = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(manifestItem.ContentLocation), location));

                    if (!File.Exists(file))
                    {
                        continue;
                    }

                    var css = File.ReadAllText(file);
                    styleBuilder.Append(css);
                    contents = contents.Replace(match.Value, string.Empty);
                }

                var styleTag = $"<style>{styleBuilder.ToString()}</style>";

                if (styleMatch.Success)
                {
                    contents = contents.Replace(styleMatch.Value, styleTag);
                }

                else
                {
                    var endOfHead = contents.IndexOf("</head>");
                    contents = contents.Insert(endOfHead, styleTag);
                }
            }

            return(contents);
        }