/// <summary>
        /// Converts FB2 simple text
        /// ( simple text is normal text or text with one of the "styles")
        /// </summary>
        /// <param name="styletypeItem">item to convert</param>
        /// <param name="simpleTextElementConverterParams"></param>
        /// <returns></returns>
        public List <IHTMLItem> Convert(StyleType styletypeItem, SimpleTextElementConverterParamsV2 simpleTextElementConverterParams)
        {
            if (styletypeItem == null)
            {
                throw new ArgumentNullException("styletypeItem");
            }

            var list = new List <IHTMLItem>();

            if (styletypeItem is SimpleText)
            {
                var text = styletypeItem as SimpleText;
                switch (text.Style)
                {
                case FB2Library.Elements.TextStyles.Normal:
                    if (text.HasChildren)
                    {
                        list.AddRange(text.Children.SelectMany(x => Convert(x, simpleTextElementConverterParams)));
                    }
                    else
                    {
                        if (simpleTextElementConverterParams.NeedToInsertDrop && text.Text.Length > 0)
                        {
                            AddAsDrop(list, text);
                        }
                        else
                        {
                            list.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                            {
                                Text = text.Text
                            });
                        }
                    }
                    break;

                case FB2Library.Elements.TextStyles.Code:
                    var code = new CodeText(HTMLElementType.XHTML11);
                    if (text.HasChildren)
                    {
                        foreach (var item in text.Children.SelectMany(x => Convert(x, simpleTextElementConverterParams)))
                        {
                            code.Add(item);
                        }
                    }
                    else
                    {
                        code.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(code);
                    break;

                case FB2Library.Elements.TextStyles.Emphasis:
                    var emph = new EmphasisedText(HTMLElementType.XHTML11);
                    if (text.HasChildren)
                    {
                        foreach (var item in text.Children.SelectMany(x => Convert(x, simpleTextElementConverterParams)))
                        {
                            emph.Add(item);
                        }
                    }
                    else
                    {
                        emph.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(emph);
                    break;

                case FB2Library.Elements.TextStyles.Strong:
                    var str = new Strong(HTMLElementType.XHTML11);
                    if (text.HasChildren)
                    {
                        foreach (var child in text.Children)
                        {
                            foreach (var item in Convert(child, simpleTextElementConverterParams))
                            {
                                str.Add(item);
                            }
                        }
                    }
                    else
                    {
                        str.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(str);
                    break;

                case FB2Library.Elements.TextStyles.Sub:
                    var sub = new Sub(HTMLElementType.XHTML11);
                    if (text.HasChildren)
                    {
                        foreach (var child in text.Children)
                        {
                            foreach (var item in Convert(child, simpleTextElementConverterParams))
                            {
                                sub.Add(item);
                            }
                        }
                    }
                    else
                    {
                        sub.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(sub);
                    break;

                case FB2Library.Elements.TextStyles.Sup:
                    var sup = new Sup(HTMLElementType.XHTML11);
                    if (text.HasChildren)
                    {
                        foreach (var child in text.Children)
                        {
                            foreach (var item in Convert(child, simpleTextElementConverterParams))
                            {
                                sup.Add(item);
                            }
                        }
                    }
                    else
                    {
                        sup.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(sup);
                    break;

                case FB2Library.Elements.TextStyles.Strikethrough:
                    var strike = new DeletedText(HTMLElementType.XHTML11);
                    if (text.HasChildren)
                    {
                        foreach (var child in text.Children)
                        {
                            foreach (var item in Convert(child, simpleTextElementConverterParams))
                            {
                                strike.Add(item);
                            }
                        }
                    }
                    else
                    {
                        strike.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(strike);
                    break;
                }
            }
            else if (styletypeItem is InternalLinkItem)
            {
                var linkConverter = new InternalLinkConverterV2();
                list.AddRange(linkConverter.Convert(styletypeItem as InternalLinkItem, new InternalLinkConverterParamsV2
                {
                    NeedToInsertDrop = simpleTextElementConverterParams.NeedToInsertDrop, Settings = simpleTextElementConverterParams.Settings,
                }));
            }
            else if (styletypeItem is InlineImageItem)
            {
                var inlineItem = styletypeItem as InlineImageItem;
                if (simpleTextElementConverterParams.Settings.Images.IsImageIdReal(inlineItem.HRef))
                {
                    var inlineImageConverter = new InlineImageConverterV2();
                    list.Add(inlineImageConverter.Convert(styletypeItem as InlineImageItem,
                                                          new InlineImageConverterParamsV2 {
                        Settings = simpleTextElementConverterParams.Settings
                    }));
                    simpleTextElementConverterParams.Settings.Images.ImageIdUsed(inlineItem.HRef);
                }
            }

            return(list);
        }
        /// <summary>
        /// Converts FB2 Paragraph to EPUB paragraph
        /// </summary>
        /// <param name="paragraphItem">item to convert</param>
        /// <param name="paragraphConverterParams"></param>
        /// <returns></returns>
        public HTMLItem Convert(ParagraphItem paragraphItem,ParagraphConverterParamsV2 paragraphConverterParams)
        {
            if (paragraphItem == null)
            {
                throw new ArgumentNullException("paragraphItem");
            }
            var paragraph = CreateBlock(paragraphConverterParams.ResultType);
            bool needToInsertDrop = paragraphConverterParams.Settings.CapitalDrop && paragraphConverterParams.StartSection;

            foreach (var item in paragraphItem.ParagraphData)
            {
                if (item is SimpleText)
                {
                    var textConverter = new SimpleTextElementConverterV2();
                    foreach (var s in textConverter.Convert(item,
                        new SimpleTextElementConverterParamsV2 { Settings = paragraphConverterParams.Settings, NeedToInsertDrop = needToInsertDrop}))
                    {
                        if (needToInsertDrop)
                        {
                            needToInsertDrop = false;
                            SetClassType(paragraph, ElementStylesV2.CapitalDrop);
                        }
                        paragraph.Add(s);
                    }
                }
                else if (item is InlineImageItem)
                {
                    // if no image data - do not insert link
                    if (paragraphConverterParams.Settings.Images.HasRealImages())
                    {
                        var inlineItem = item as InlineImageItem;
                        if (paragraphConverterParams.Settings.Images.IsImageIdReal(inlineItem.HRef))
                        {
                            var inlineImageConverter = new InlineImageConverterV2();
                            paragraph.Add(inlineImageConverter.Convert(inlineItem,new InlineImageConverterParamsV2{ Settings = paragraphConverterParams.Settings }));
                            paragraphConverterParams.Settings.Images.ImageIdUsed(inlineItem.HRef);
                        }
                        if (needToInsertDrop) // in case this is "drop" image - no need to create a drop
                        {
                            needToInsertDrop = false;
                        }
                    }
                }
                else if (item is InternalLinkItem)
                {
                    var internalLinkConverter = new InternalLinkConverterV2();
                    foreach (var s in internalLinkConverter.Convert(item as InternalLinkItem,
                        new InternalLinkConverterParamsV2{ Settings = paragraphConverterParams.Settings,  NeedToInsertDrop = needToInsertDrop}))
                    {
                        if (needToInsertDrop)
                        {
                            needToInsertDrop = false;
                            SetClassType(paragraph, ElementStylesV2.CapitalDrop);
                        }
                        paragraph.Add(s);
                    }
                }
            }

            //SetClassType(paragraph);
            paragraph.GlobalAttributes.ID.Value =
                paragraphConverterParams.Settings.ReferencesManager.AddIdUsed(paragraphItem.ID, paragraph);

            return paragraph;
        }
        /// <summary>
        /// Converts FB2 simple text 
        /// ( simple text is normal text or text with one of the "styles")
        /// </summary>
        /// <param name="styletypeItem">item to convert</param>
        /// <param name="simpleTextElementConverterParams"></param>
        /// <returns></returns>
        public List<IHTMLItem> Convert(StyleType styletypeItem,SimpleTextElementConverterParamsV2 simpleTextElementConverterParams)
        {

            if (styletypeItem == null)
            {
                throw new ArgumentNullException("styletypeItem");
            }

            var list = new List<IHTMLItem>();

            if (styletypeItem is SimpleText)
            {
                var text = styletypeItem as SimpleText;
                switch (text.Style)
                {
                    case FB2Library.Elements.TextStyles.Normal:
                        if (text.HasChildren)
                        {
                            list.AddRange(text.Children.SelectMany(x=>Convert(x,simpleTextElementConverterParams)));
                        }
                        else
                        {
                            if (simpleTextElementConverterParams.NeedToInsertDrop && text.Text.Length > 0)
                            {
                                AddAsDrop(list,text);
                            }
                            else
                            {
                                list.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = text.Text });
                            }
                        }
                        break;
                    case FB2Library.Elements.TextStyles.Code:
                        var code = new CodeText(HTMLElementType.XHTML11);
                        if (text.HasChildren)
                        {
                            foreach (var item in text.Children.SelectMany(x=>Convert(x,simpleTextElementConverterParams)))
                            {
                                code.Add(item);
                            }
                        }
                        else
                        {
                            code.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = text.Text });
                        }
                        list.Add(code);
                        break;
                    case FB2Library.Elements.TextStyles.Emphasis:
                        var emph = new EmphasisedText(HTMLElementType.XHTML11);
                        if (text.HasChildren)
                        {
                            foreach (var item in text.Children.SelectMany(x=>Convert(x,simpleTextElementConverterParams)))
                            {
                                emph.Add(item);
                            }
                        }
                        else
                        {
                            emph.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = text.Text });
                        }
                        list.Add(emph);
                        break;
                    case FB2Library.Elements.TextStyles.Strong:
                        var str = new Strong(HTMLElementType.XHTML11);
                        if (text.HasChildren)
                        {
                            foreach (var child in text.Children)
                            {
                                foreach (var item in Convert(child, simpleTextElementConverterParams))
                                {
                                    str.Add(item);
                                }
                            }
                        }
                        else
                        {
                            str.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = text.Text });
                        }
                        list.Add(str);
                        break;
                    case FB2Library.Elements.TextStyles.Sub:
                        var sub = new Sub(HTMLElementType.XHTML11);
                        if (text.HasChildren)
                        {
                            foreach (var child in text.Children)
                            {
                                foreach (var item in Convert(child,simpleTextElementConverterParams))
                                {
                                    sub.Add(item);
                                }
                            }
                        }
                        else
                        {
                            sub.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = text.Text });
                        }
                        list.Add(sub);
                        break;
                    case FB2Library.Elements.TextStyles.Sup:
                        var sup = new Sup(HTMLElementType.XHTML11);
                        if (text.HasChildren)
                        {
                            foreach (var child in text.Children)
                            {
                                foreach (var item in Convert(child,simpleTextElementConverterParams))
                                {
                                    sup.Add(item);
                                }
                            }
                        }
                        else
                        {
                            sup.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = text.Text });
                        }
                        list.Add(sup);
                        break;
                    case FB2Library.Elements.TextStyles.Strikethrough:
                        var strike = new DeletedText(HTMLElementType.XHTML11);
                        if (text.HasChildren)
                        {
                            foreach (var child in text.Children)
                            {
                                foreach (var item in Convert(child,simpleTextElementConverterParams))
                                {
                                    strike.Add(item);
                                }
                            }
                        }
                        else
                        {
                            strike.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = text.Text });
                        }
                        list.Add(strike);
                        break;
                }
            }
            else if (styletypeItem is InternalLinkItem)
            {
                var linkConverter = new InternalLinkConverterV2();
                list.AddRange(linkConverter.Convert(styletypeItem as InternalLinkItem,  new InternalLinkConverterParamsV2
                {
                    NeedToInsertDrop = simpleTextElementConverterParams.NeedToInsertDrop, Settings = simpleTextElementConverterParams.Settings,
                }));
            }
            else if (styletypeItem is InlineImageItem)
            {
                var inlineItem = styletypeItem as InlineImageItem;
                if (simpleTextElementConverterParams.Settings.Images.IsImageIdReal(inlineItem.HRef))
                {
                    var inlineImageConverter = new InlineImageConverterV2();
                    list.Add(inlineImageConverter.Convert(styletypeItem as InlineImageItem,
                        new InlineImageConverterParamsV2 {Settings = simpleTextElementConverterParams.Settings}));
                    simpleTextElementConverterParams.Settings.Images.ImageIdUsed(inlineItem.HRef);
                }
            }

            return list;
        }
Esempio n. 4
0
        /// <summary>
        /// Converts FB2 Paragraph to EPUB paragraph
        /// </summary>
        /// <param name="paragraphItem">item to convert</param>
        /// <param name="paragraphConverterParams"></param>
        /// <returns></returns>
        public HTMLItem Convert(ParagraphItem paragraphItem, ParagraphConverterParamsV2 paragraphConverterParams)
        {
            if (paragraphItem == null)
            {
                throw new ArgumentNullException("paragraphItem");
            }
            var  paragraph        = CreateBlock(paragraphConverterParams.ResultType);
            bool needToInsertDrop = paragraphConverterParams.Settings.CapitalDrop && paragraphConverterParams.StartSection;

            foreach (var item in paragraphItem.ParagraphData)
            {
                if (item is SimpleText)
                {
                    var textConverter = new SimpleTextElementConverterV2();
                    foreach (var s in textConverter.Convert(item,
                                                            new SimpleTextElementConverterParamsV2 {
                        Settings = paragraphConverterParams.Settings, NeedToInsertDrop = needToInsertDrop
                    }))
                    {
                        if (needToInsertDrop)
                        {
                            needToInsertDrop = false;
                            SetClassType(paragraph, ElementStylesV2.CapitalDrop);
                        }
                        paragraph.Add(s);
                    }
                }
                else if (item is InlineImageItem)
                {
                    // if no image data - do not insert link
                    if (paragraphConverterParams.Settings.Images.HasRealImages())
                    {
                        var inlineItem = item as InlineImageItem;
                        if (paragraphConverterParams.Settings.Images.IsImageIdReal(inlineItem.HRef))
                        {
                            var inlineImageConverter = new InlineImageConverterV2();
                            paragraph.Add(inlineImageConverter.Convert(inlineItem, new InlineImageConverterParamsV2 {
                                Settings = paragraphConverterParams.Settings
                            }));
                            paragraphConverterParams.Settings.Images.ImageIdUsed(inlineItem.HRef);
                        }
                        if (needToInsertDrop) // in case this is "drop" image - no need to create a drop
                        {
                            needToInsertDrop = false;
                        }
                    }
                }
                else if (item is InternalLinkItem)
                {
                    var internalLinkConverter = new InternalLinkConverterV2();
                    foreach (var s in internalLinkConverter.Convert(item as InternalLinkItem,
                                                                    new InternalLinkConverterParamsV2 {
                        Settings = paragraphConverterParams.Settings, NeedToInsertDrop = needToInsertDrop
                    }))
                    {
                        if (needToInsertDrop)
                        {
                            needToInsertDrop = false;
                            SetClassType(paragraph, ElementStylesV2.CapitalDrop);
                        }
                        paragraph.Add(s);
                    }
                }
            }

            //SetClassType(paragraph);
            paragraph.GlobalAttributes.ID.Value =
                paragraphConverterParams.Settings.ReferencesManager.AddIdUsed(paragraphItem.ID, paragraph);

            return(paragraph);
        }