/// <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);
        }
示例#2
0
        public override void GenerateBody()
        {
            base.GenerateBody();
            var titlePage = new Div(Compatibility);

            titlePage.GlobalAttributes.Class.Value = ElementStylesV2.TitlePage;
            if (!string.IsNullOrEmpty(BookTitle))
            {
                // try to use FB2 book's title
                var p = new H2(Compatibility);
                p.Add(new SimpleHTML5Text(Compatibility)
                {
                    Text = BookTitle
                });
                p.GlobalAttributes.Class.Value = string.Format(ElementStylesV2.TitleItemFormat, 1);
                titlePage.Add(p);
            }
            else
            {
                titlePage.Add(new SimpleHTML5Text(Compatibility)
                {
                    Text = "Unnamed"
                });
            }

            titlePage.Add(new EmptyLine(Compatibility));

            var sbSeries = new StringBuilder();

            foreach (var serie in _series)
            {
                if (!string.IsNullOrEmpty(sbSeries.ToString()))
                {
                    sbSeries.Append(" , ");
                }
                sbSeries.Append(serie);
            }
            if (sbSeries.ToString() != string.Empty)
            {
                var seriesItem = new SimpleHTML5Text(Compatibility)
                {
                    Text = string.Format("( {0} )", sbSeries)
                };
                var containingText = new EmphasisedText(Compatibility);
                containingText.Add(seriesItem);
                var seriesHeading = new H3(Compatibility);
                seriesHeading.GlobalAttributes.Class.Value = ElementStylesV2.TitleSeries;
                seriesHeading.Add(containingText);
                titlePage.Add(seriesHeading);
            }

            foreach (var author in _authors)
            {
                var authorsHeading = new H3(Compatibility);
                var authorLine     = new SimpleHTML5Text(Compatibility)
                {
                    Text = author
                };
                authorsHeading.Add(authorLine);
                authorsHeading.GlobalAttributes.Class.Value = ElementStylesV2.TitleAuthors;
                titlePage.Add(authorsHeading);
            }


            BodyElement.Add(titlePage);
        }