コード例 #1
0
        public static TokenBase Load(BinaryReader reader, int id)
        {
            var type = (TokenType)reader.ReadByte();

            switch (type)
            {
            case TokenType.TagOpenToken:
                return(TagOpenToken.Load(reader, id));

            case TokenType.TagCloseToken:
                return(TagCloseToken.Load(reader, id));

            case TokenType.TextToken:
                return(TextToken.Load(reader, id));

            case TokenType.WhitespaceToken:
                return(new WhitespaceToken(id));

            case TokenType.NewPageToken:
                return(new NewPageToken(id));

            case TokenType.PictureToken:
                return(PictureToken.Load(reader, id));

            default:
                return(null);
            }
        }
コード例 #2
0
 private void AppendToLine(PictureToken token)
 {
     var imageTokenBlock = new ImageTokenBlock
                               {
                                   ImageID = token.ImageID,
                                   FirstTokenID = _firstTokenID,
                                   LastTokenID = token.ID
                               };
     _output.Enqueue(imageTokenBlock);
     _firstTokenID = token.ID + 1;
 }
コード例 #3
0
        private IEnumerable<TokenBase> ParseNodes(XContainer container, Stack<TextVisualProperties> propertiesStack, TokenIndex top, int bookLevel, int parentID = -1)
        {
            foreach (XNode node in container.Nodes())
            {
                var text = node as XText;
                if ((text != null) && !string.IsNullOrEmpty(text.Value))
                {
                    foreach (TokenBase token in ParseText(text.Value, top))
                    {
                        yield return token;
                    }
                }
                var element = node as XElement;
                if(element == null)
                    continue;

                TextVisualProperties properties = propertiesStack.Peek().Clone().Update(element, _styleSheet);
                
                string localName = element.Name.LocalName;
                int level = bookLevel;

                if (localName == "a")
                {
                    ProcessLinks(properties, element);
                }
                ProcessAnchors(top, element);

                if (localName == "section")
                {
                    yield return new NewPageToken(top.Index++);
                    level++;
                }

                if (localName == "title")
                {
                    ProcessTitleData(top, element, level);
                }

                if (localName == "image")
                {
                    XAttribute hrefAttr = element.Attributes().FirstOrDefault(t => (t.Name.LocalName == "href"));
                    string href = ((hrefAttr != null) ? hrefAttr.Value : string.Empty).TrimStart('#');
                    var pictureToken = new PictureToken(top.Index++, href);
                    yield return pictureToken;
                }
                else
                {
                    var tagOpen = new TagOpenToken(top.Index++, element, properties, parentID);
                    yield return tagOpen;

                    propertiesStack.Push(properties);
                    foreach (TokenBase token in ParseNodes(element, propertiesStack, top, level, tagOpen.ID))
                    {
                        yield return token;
                    }
                    propertiesStack.Pop();

                    yield return new TagCloseToken(top.Index++, parentID);
                }
                
            }
        }
コード例 #4
0
        private IEnumerable<TokenBase> ParseNodes(HtmlNode container, Stack<TextVisualProperties> propertiesStack, TokenIndex top, EpubPath path, int parentID = -1)
        {
            foreach (HtmlNode child in container.ChildNodes)
            {
                var asText = child as HtmlTextNode;
                if (asText != null && !string.IsNullOrEmpty(asText.Text))
                {
                    foreach (TokenBase text in ParseText(asText.Text, top))
                    {
                        yield return text;
                    }
                }
                else
                {
                    TextVisualProperties properties = propertiesStack.Peek().Clone().Update(child, _css);
                    properties.LinkID = string.Empty;

                    if (child.Name == "a" || child.Name == "span")
                    {
                        ParseAnchors(top, path, child);
                    }

                    if (child.Name == "a")
                    {
                        string href = child.GetAttributeValue("href", string.Empty);
                        if (!string.IsNullOrEmpty(href))
                        {
                            if (href.StartsWith("#"))
                            {
                                properties.LinkID = path.CurrentFilePath + href;
                            }
                            else
                            {
                                properties.LinkID = path + href;
                            }
                        }
                    }
                    if (string.Equals(child.Name, "img"))
                    {
                        HtmlAttributeCollection attributes = child.Attributes;
                        string src = attributes.Contains("src") ? attributes["src"].Value : string.Empty;
                        var pictureToken = new PictureToken(top.Index++, (path) + src);
                        yield return pictureToken;
                    }
                    else
                    {
                        if(child is HtmlCommentNode)
                            continue;

                        var tagOpenToken = new TagOpenToken(top.Index++, child, properties, parentID);
                        yield return tagOpenToken;
                        propertiesStack.Push(properties);
                        foreach (TokenBase token in ParseNodes(child, propertiesStack, top, path, tagOpenToken.ID))
                        {
                            yield return token;
                        }
                        propertiesStack.Pop();
                        yield return new TagCloseToken(top.Index++, parentID);
                    }
                }
            }
        }