コード例 #1
0
        /// <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, SimpleTextElementConverterParamsV3 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.HTML5) { Text = text.Text });
                            }
                        }
                        break;
                    case FB2Library.Elements.TextStyles.Code:
                        var code = new CodeText(HTMLElementType.HTML5);
                        if (text.HasChildren)
                        {
                            foreach (var item in text.Children.SelectMany(x => Convert(x, simpleTextElementConverterParams)))
                            {
                                code.Add(item);
                            }
                        }
                        else
                        {
                            code.Add(new SimpleHTML5Text(HTMLElementType.HTML5) { Text = text.Text });
                        }
                        list.Add(code);
                        break;
                    case FB2Library.Elements.TextStyles.Emphasis:
                        var emph = new EmphasisedText(HTMLElementType.HTML5);
                        if (text.HasChildren)
                        {
                            foreach (var item in text.Children.SelectMany(x => Convert(x, simpleTextElementConverterParams)))
                            {
                                emph.Add(item);
                            }
                        }
                        else
                        {
                            emph.Add(new SimpleHTML5Text(HTMLElementType.HTML5) { Text = text.Text });
                        }
                        list.Add(emph);
                        break;
                    case FB2Library.Elements.TextStyles.Strong:
                        var str = new Strong(HTMLElementType.HTML5);
                        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.HTML5) { Text = text.Text });
                        }
                        list.Add(str);
                        break;
                    case FB2Library.Elements.TextStyles.Sub:
                        var sub = new Sub(HTMLElementType.HTML5);
                        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.HTML5) { Text = text.Text });
                        }
                        list.Add(sub);
                        break;
                    case FB2Library.Elements.TextStyles.Sup:
                        var sup = new Sup(HTMLElementType.HTML5);
                        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.HTML5) { Text = text.Text });
                        }
                        list.Add(sup);
                        break;
                    case FB2Library.Elements.TextStyles.Strikethrough:
                        var strike = new DeletedText(HTMLElementType.HTML5);
                        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.HTML5) { Text = text.Text });
                        }
                        list.Add(strike);
                        break;
                }
            }
            else if (styletypeItem is InternalLinkItem)
            {
                var linkConverter = new InternalLinkConverterV3();
                list.AddRange(linkConverter.Convert(styletypeItem as InternalLinkItem, new InternalLinkConverterParamsV3
                {
                    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 InlineImageConverterV3();
                    list.Add(inlineImageConverter.Convert(styletypeItem as InlineImageItem,
                        new InlineImageConverterParamsV3 { Settings = simpleTextElementConverterParams.Settings }));
                    simpleTextElementConverterParams.Settings.Images.ImageIdUsed(inlineItem.HRef);
                }
            }

            return list;
        }
コード例 #2
0
        /// <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, SimpleTextElementConverterParamsV3 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.HTML5)
                            {
                                Text = text.Text
                            });
                        }
                    }
                    break;

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

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

                case FB2Library.Elements.TextStyles.Strong:
                    var str = new Strong(HTMLElementType.HTML5);
                    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.HTML5)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(str);
                    break;

                case FB2Library.Elements.TextStyles.Sub:
                    var sub = new Sub(HTMLElementType.HTML5);
                    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.HTML5)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(sub);
                    break;

                case FB2Library.Elements.TextStyles.Sup:
                    var sup = new Sup(HTMLElementType.HTML5);
                    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.HTML5)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(sup);
                    break;

                case FB2Library.Elements.TextStyles.Strikethrough:
                    var strike = new DeletedText(HTMLElementType.HTML5);
                    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.HTML5)
                        {
                            Text = text.Text
                        });
                    }
                    list.Add(strike);
                    break;
                }
            }
            else if (styletypeItem is InternalLinkItem)
            {
                var linkConverter = new InternalLinkConverterV3();
                list.AddRange(linkConverter.Convert(styletypeItem as InternalLinkItem, new InternalLinkConverterParamsV3
                {
                    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 InlineImageConverterV3();
                    list.Add(inlineImageConverter.Convert(styletypeItem as InlineImageItem,
                                                          new InlineImageConverterParamsV3 {
                        Settings = simpleTextElementConverterParams.Settings
                    }));
                    simpleTextElementConverterParams.Settings.Images.ImageIdUsed(inlineItem.HRef);
                }
            }

            return(list);
        }
コード例 #3
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, ParagraphConverterParamsV3 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 SimpleTextElementConverterV3();
                    foreach (var s in textConverter.Convert(item,
                        new SimpleTextElementConverterParamsV3 { Settings = paragraphConverterParams.Settings, NeedToInsertDrop = needToInsertDrop }))
                    {
                        if (needToInsertDrop)
                        {
                            needToInsertDrop = false;
                            SetClassType(paragraph, ElementStylesV3.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 InlineImageConverterV3();
                            paragraph.Add(inlineImageConverter.Convert(inlineItem, new InlineImageConverterParamsV3 { 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 InternalLinkConverterV3();
                    foreach (var s in internalLinkConverter.Convert(item as InternalLinkItem,
                        new InternalLinkConverterParamsV3 { Settings = paragraphConverterParams.Settings, NeedToInsertDrop = needToInsertDrop }))
                    {
                        if (needToInsertDrop)
                        {
                            needToInsertDrop = false;
                            SetClassType(paragraph, ElementStylesV3.CapitalDrop);
                        }
                        paragraph.Add(s);
                    }
                }
            }

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

            return paragraph;
        }
コード例 #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, ParagraphConverterParamsV3 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 SimpleTextElementConverterV3();
                    foreach (var s in textConverter.Convert(item,
                                                            new SimpleTextElementConverterParamsV3 {
                        Settings = paragraphConverterParams.Settings, NeedToInsertDrop = needToInsertDrop
                    }))
                    {
                        if (needToInsertDrop)
                        {
                            needToInsertDrop = false;
                            SetClassType(paragraph, ElementStylesV3.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 InlineImageConverterV3();
                            paragraph.Add(inlineImageConverter.Convert(inlineItem, new InlineImageConverterParamsV3 {
                                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 InternalLinkConverterV3();
                    foreach (var s in internalLinkConverter.Convert(item as InternalLinkItem,
                                                                    new InternalLinkConverterParamsV3 {
                        Settings = paragraphConverterParams.Settings, NeedToInsertDrop = needToInsertDrop
                    }))
                    {
                        if (needToInsertDrop)
                        {
                            needToInsertDrop = false;
                            SetClassType(paragraph, ElementStylesV3.CapitalDrop);
                        }
                        paragraph.Add(s);
                    }
                }
            }

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

            return(paragraph);
        }