Esempio n. 1
0
        private void HandleBlocks(BlockContent block)
        {
            // Upon the finding of the first non-table element
            // stop populating the table with rows and end it.
            if (CurrentPage.InTable && block.Type != "row")
            {
                CurrentPage.Html.EndTable();
                CurrentPage.InTable = false;
            }

            switch (block.Type)
            {
            case "text":
                // Don't log when a text block is found
                HandleText(block);
                break;

            case "image":
                // Don't log when an image block is found
                HandleImage(block);
                break;

            case "raw":
                Helper.Warning("Ignoring \"raw\" block");
                break;

            case "spacer":
                CurrentPage.Html.AddSpacer((block.Size as double?) ?? 1.0);
                break;

            case "page_break":
                CurrentPage.Html.AddPageBreak();
                break;

            case "border":
                CurrentPage.Html.AddBorder((block.Width as double?) ?? 1.0, block.Style);
                break;

            case "row":
                HandleRow(block);
                break;

            case "hr":
                CurrentPage.Html.AddHorizontalRule();
                break;

            default:
                // Shouldn't ever happen
                Helper.Warning("Found an unknown block type: " + block.Type);
                break;
            }
        }
Esempio n. 2
0
        private void HandleRow(BlockContent block)
        {
            if (!CurrentPage.InTable)
            {
                // Don't estimate the table column width. Just average it.
                // Leave it to the person post-processing the book.
                CurrentPage.Html.StartTable(block.Cells.Length);
                CurrentPage.InTable = true;
            }

            var columns = new List <HtmlTableColumn>();

            // NOTE: Only text is supported inside tables
            // Meaning, no images, links, etc. are allowed.
            foreach (var cell in block.Cells)
            {
                var text = new List <string>();

                // Similar to the BlockContent type
                foreach (var node in cell.Nodes)
                {
                    if (node.WordCount == 0)
                    {
                        continue;
                    }

                    foreach (var word in node.Words)
                    {
                        text.Add(HandleWordContent(word));
                    }
                }

                // Can occurr from time to time
                if (cell.Style != null)
                {
                    if ((cell.Style as string).Contains("-scribd") && !CurrentPage.WarnedUnsupportedStyle)
                    {
                        Helper.Warning("Double-check table style attribute for an unsupported -scribd* property.");
                        CurrentPage.WarnedUnsupportedStyle = true;
                    }
                }

                columns.Add(new HtmlTableColumn
                {
                    Text  = string.Join(" ", text),
                    Style = cell.Style
                });
            }

            CurrentPage.Html.AddTableRow(columns);
        }
Esempio n. 3
0
        private void HandleText(BlockContent block)
        {
            // Don't do anything when the word count is 0.
            // Should not ever happen with text-type elements.
            if (block.WordCount == 0)
            {
                return;
            }

            var text = new List <string>();

            foreach (var word in block.Words)
            {
                text.Add(HandleWordContent(word));
            }

            var style_attribute = GetStyleAttribute(block);
            var class_attribute = GetClassAttribute(block.Words[0].Style);
            var href_attribute  = GetHrefAttribute(block.Words[0].Metadata);

            var text_string = string.Join(" ", text);

            // If we found an URL in the metadata, then we will create a link.
            // If not, and there are size attributes, then we will create a headline
            // and, finally, if there are no size attributes, then we will create a paragraph.
            if (href_attribute.LocalName != null)
            {
                CurrentPage.Html.AddLink(
                    text_string,
                    href_attribute,
                    class_attribute,
                    style_attribute
                    );
            }
            else
            {
                CurrentPage.Html.AddText(
                    GetTextElementType(block),
                    text_string,
                    class_attribute,
                    style_attribute
                    );
            }
        }
Esempio n. 4
0
        private void HandleImage(BlockContent block)
        {
            // Obtain image path and filename
            var image_path = Book.Directory +
                             Path.DirectorySeparatorChar +
                             CurrentPage.Chapter.FilePath +
                             Path.DirectorySeparatorChar +
                             block.Src;

            var image_name    = Path.GetFileName(image_path);
            var image_content = File.ReadAllBytes(image_path);

            // Shouldn't happen
            if (IsCorruptImage(image_content))
            {
                Helper.Warning("You should redownload the book. Found corrupt image: " + image_name);
            }

            // Only add this image to our image list if it doesn't already exist
            // So we can later add it to the EPUB file
            if (!Book.Images.ContainsKey(image_name))
            {
                Helper.Debug("Adding image: " + image_name);

                Book.Images.Add(image_name, new HtmlImage
                {
                    AbsoluteFilePath = image_path,
                    Content          = image_content,
                    ID = "img" + Book.Images.Count.ToString("D4")
                });
            }

            // Add image element to HTML
            CurrentPage.Html.AddImage(
                image_name,
                block.Alt ?? image_name,
                block.Center ?? false
                );
        }
Esempio n. 5
0
        private HtmlAttribute GetStyleAttribute(BlockContent block)
        {
            // The same logic applies, just as with metadata.
            // NOTE: This abuses an underlying hack in HtmlGenerator.AddText()!
            // Specifically, when LocalName is set to null, then the attribute won't be added.
            // Meaning, that the attribute will only be added if the if cases underneath succeed.
            HtmlAttribute style_attribute = new HtmlAttribute();

            if (block.Align != null)
            {
                var align_type = block.Align as string;
                if (align_type == null)
                {
                    return(style_attribute);
                }

                style_attribute.LocalName = "style";
                style_attribute.Value     = "text-align: " + align_type;
            }

            return(style_attribute);
        }
Esempio n. 6
0
        private string GetTextElementType(BlockContent block)
        {
            if (block.Size == null)
            {
                return("p");
            }

            if ((block.Size as string) != "headline")
            {
                return("p");
            }

            var header_level = 2;

            if (block.SizeClass != null)
            {
                const int MAX_HEADLINE_SIZE = 4;
                header_level  = MAX_HEADLINE_SIZE - block.SizeClass.Value + 1;
                header_level  = Math.Min(Math.Max(1, header_level), MAX_HEADLINE_SIZE);
                header_level += 1;
            }

            return("h" + header_level);
        }