/// <summary>
        /// Converts FB2 section element
        /// </summary>
        /// <param name="sectionItem">item to convert</param>
        /// <returns>XHTML representation</returns>
        public List<IHTMLItem> Convert(SectionItem sectionItem)
        {
            if (sectionItem == null)
            {
                throw new ArgumentNullException("sectionItem");
            }
            var resList = new List<IHTMLItem>();
            Logger.Log.Debug("Converting section");

            var content = new Div(HTMLElementType.XHTML11);
            ulong documentSize = 0;
            _checker.MaxSizeLimit = Settings.MaxSize;

            SetClassType(content, string.Format(ElementStylesV2.SectionItemFormat, RecursionLevel));

            content.GlobalAttributes.ID.Value = Settings.ReferencesManager.AddIdUsed(sectionItem.ID, content);

            if (sectionItem.Lang != null)
            {
                content.GlobalAttributes.Language.Value = sectionItem.Lang;
            }

            // Load Title
            if (sectionItem.Title != null)
            {
                IHTMLItem titleItem;
                if (!LinkSection)
                {
                    var titleConverter = new TitleConverterV2();
                    titleItem = titleConverter.Convert(sectionItem.Title,
                        new TitleConverterParamsV2 { Settings = Settings, TitleLevel = RecursionLevel + 1 });
                }
                else
                {
                    var linkSectionConverter = new LinkSectionConverterV2();
                    titleItem = linkSectionConverter.Convert(sectionItem,
                        new LinkSectionConverterParamsV2{Settings = Settings});
                }
                if (titleItem != null)
                {
                    documentSize = SplitBlockHTMLItem(titleItem, content, resList, documentSize);
                }
            }

            // Load epigraphs
            documentSize = (from epigraph in sectionItem.Epigraphs let epigraphConverter = new EpigraphConverterV2() select epigraphConverter.Convert(epigraph, new EpigraphConverterParamsV2 {Settings = Settings, Level = RecursionLevel + 1})).Aggregate(documentSize, (current, epigraphItem) => SplitBlockHTMLItem(epigraphItem, content, resList, current));

            // Load section image
            if (Settings.Images.HasRealImages())
            {
                documentSize = sectionItem.SectionImages.Aggregate(documentSize, (current, sectionImage) => ConvertSectionImage(sectionImage, content, resList, current));
            }

            // Load annotations
            if (sectionItem.Annotation != null)
            {
                var annotationConverter = new AnnotationConverterV2();
                IHTMLItem annotationItem = annotationConverter.Convert(sectionItem.Annotation,  new AnnotationConverterParamsV2{ Level = RecursionLevel + 1 ,Settings = Settings});
                documentSize = SplitBlockHTMLItem(annotationItem, content, resList, documentSize);
            }

            // Parse all elements only if section has no sub section
            if (sectionItem.SubSections.Count == 0)
            {
                bool startSection = true;
                sectionItem.Content.Aggregate(documentSize, (current, item) => ConvertSimpleSubItem(item, sectionItem, content, resList, ref startSection, current));
            }

            resList.Add(content);
            return resList;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts FB2 section element
        /// </summary>
        /// <param name="sectionItem">item to convert</param>
        /// <returns>XHTML representation</returns>
        public List <IHTMLItem> Convert(SectionItem sectionItem)
        {
            if (sectionItem == null)
            {
                throw new ArgumentNullException("sectionItem");
            }
            var resList = new List <IHTMLItem>();

            Logger.Log.Debug("Converting section");

            var   content      = new Div(HTMLElementType.XHTML11);
            ulong documentSize = 0;

            _checker.MaxSizeLimit = Settings.MaxSize;

            SetClassType(content, string.Format(ElementStylesV2.SectionItemFormat, RecursionLevel));

            content.GlobalAttributes.ID.Value = Settings.ReferencesManager.AddIdUsed(sectionItem.ID, content);

            if (sectionItem.Lang != null)
            {
                content.GlobalAttributes.Language.Value = sectionItem.Lang;
            }


            // Load Title
            if (sectionItem.Title != null)
            {
                IHTMLItem titleItem;
                if (!LinkSection)
                {
                    var titleConverter = new TitleConverterV2();
                    titleItem = titleConverter.Convert(sectionItem.Title,
                                                       new TitleConverterParamsV2 {
                        Settings = Settings, TitleLevel = RecursionLevel + 1
                    });
                }
                else
                {
                    var linkSectionConverter = new LinkSectionConverterV2();
                    titleItem = linkSectionConverter.Convert(sectionItem,
                                                             new LinkSectionConverterParamsV2 {
                        Settings = Settings
                    });
                }
                if (titleItem != null)
                {
                    documentSize = SplitBlockHTMLItem(titleItem, content, resList, documentSize);
                }
            }

            // Load epigraphs
            documentSize = (from epigraph in sectionItem.Epigraphs let epigraphConverter = new EpigraphConverterV2() select epigraphConverter.Convert(epigraph, new EpigraphConverterParamsV2 {
                Settings = Settings, Level = RecursionLevel + 1
            })).Aggregate(documentSize, (current, epigraphItem) => SplitBlockHTMLItem(epigraphItem, content, resList, current));

            // Load section image
            if (Settings.Images.HasRealImages())
            {
                documentSize = sectionItem.SectionImages.Aggregate(documentSize, (current, sectionImage) => ConvertSectionImage(sectionImage, content, resList, current));
            }

            // Load annotations
            if (sectionItem.Annotation != null)
            {
                var       annotationConverter = new AnnotationConverterV2();
                IHTMLItem annotationItem      = annotationConverter.Convert(sectionItem.Annotation, new AnnotationConverterParamsV2 {
                    Level = RecursionLevel + 1, Settings = Settings
                });
                documentSize = SplitBlockHTMLItem(annotationItem, content, resList, documentSize);
            }

            // Parse all elements only if section has no sub section
            if (sectionItem.SubSections.Count == 0)
            {
                bool startSection = true;
                sectionItem.Content.Aggregate(documentSize, (current, item) => ConvertSimpleSubItem(item, sectionItem, content, resList, ref startSection, current));
            }

            resList.Add(content);
            return(resList);
        }
Exemplo n.º 3
0
        public HTMLItem Convert(FB2File fb2File, ConverterOptionsV2 settings)
        {
            if (fb2File == null)
            {
                throw new ArgumentNullException("fb2File");
            }
            var info = new Div(HTMLElementType.XHTML11);
            var header = new H3(HTMLElementType.XHTML11);
            header.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = "FB2 document info" });
            info.Add(header);
            if (fb2File.DocumentInfo != null)
            {
                if ( !string.IsNullOrEmpty(fb2File.DocumentInfo.ID) )
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = string.Format("Document ID:  {0}", fb2File.DocumentInfo.ID) });
                    info.Add(p);
                }
                if (fb2File.DocumentInfo.DocumentVersion.HasValue)
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = string.Format("Document version:  {0}", fb2File.DocumentInfo.DocumentVersion) });
                    info.Add(p);                    
                }
                if ((fb2File.DocumentInfo.DocumentDate != null) && !string.IsNullOrEmpty(fb2File.DocumentInfo.DocumentDate.Text))
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = string.Format("Document creation date:  {0}", fb2File.DocumentInfo.DocumentDate.Text) });
                    info.Add(p);
                }
                if ( (fb2File.DocumentInfo.ProgramUsed2Create != null) && !string.IsNullOrEmpty(fb2File.DocumentInfo.ProgramUsed2Create.Text) )
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = string.Format("Created using:  {0} software", fb2File.DocumentInfo.ProgramUsed2Create.Text) });
                    info.Add(p);                    
                }
                if ((fb2File.DocumentInfo.SourceOCR != null) && !string.IsNullOrEmpty(fb2File.DocumentInfo.SourceOCR.Text))
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = string.Format("OCR Source:  {0}", fb2File.DocumentInfo.SourceOCR.Text) });
                    info.Add(p);                                        
                }
                if ((fb2File.DocumentInfo.DocumentAuthors != null) && (fb2File.DocumentInfo.DocumentAuthors.Count > 0))
                {
                    var heading = new H4(HTMLElementType.XHTML11);
                    heading.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = "Document authors :" });
                    info.Add(heading);
                    var authors = new UnorderedList(HTMLElementType.XHTML11) ;
                    foreach (var author in fb2File.DocumentInfo.DocumentAuthors)
                    {
                        var li = new ListItem(HTMLElementType.XHTML11);
                        li.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = DescriptionConverters.GetAuthorAsSting(author) });
                        authors.Add(li);
                    }
                    info.Add(authors);
                }
                if ((fb2File.DocumentInfo.DocumentPublishers != null) && (fb2File.DocumentInfo.DocumentPublishers.Count > 0))
                {
                    var heading = new H4(HTMLElementType.XHTML11);
                    heading.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = "Document publishers :" });
                    info.Add(heading);

                    var publishers = new UnorderedList(HTMLElementType.XHTML11);
                    foreach (var publisher in fb2File.DocumentInfo.DocumentPublishers)
                    {
                        var li = new ListItem(HTMLElementType.XHTML11);
                        li.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = DescriptionConverters.GetAuthorAsSting(publisher) });
                        publishers.Add(li);
                    }
                    info.Add(publishers);
                }

                if ((fb2File.DocumentInfo.SourceURLs != null) && (fb2File.DocumentInfo.SourceURLs.Any()))
                {
                    var heading = new H4(HTMLElementType.XHTML11);
                    heading.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = "Source URLs :" });
                    info.Add(heading);

                    var urls = new UnorderedList(HTMLElementType.XHTML11);
                    foreach (var url in fb2File.DocumentInfo.SourceURLs)
                    {
                        var li = new ListItem(HTMLElementType.XHTML11);
                        if (ReferencesUtils.IsExternalLink(url))
                        {
                            var link = new Anchor(HTMLElementType.XHTML11);
                            link.HRef.Value = url;
                            link.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) {Text = url});
                            li.Add(link);
                        }
                        else
                        {
                            li.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = url });
                        }
                        urls.Add(li);
                    }
                    info.Add(urls);
                }

                if (fb2File.DocumentInfo.History != null)
                {
                    var heading = new H4(HTMLElementType.XHTML11);
                    heading.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = "Document history:" });
                    info.Add(heading);
                    var annotationConverter = new AnnotationConverterV2();
                    info.Add(annotationConverter.Convert(fb2File.DocumentInfo.History,new AnnotationConverterParamsV2 { Level = 1, Settings = settings }));
                    //Paragraph p = new Paragraph();
                    //p.Add(new SimpleHTML5Text() { Text = fb2File.DocumentInfo.History.ToString() });
                    //info.Add(p);                                                            
                }
            }

            // in case there is no elements - no need for a header
            if (info.SubElements().Count <= 1)
            {
                info.Remove(header);
            }

            SetClassType(info,ElementStylesV2.FB2Info);
            return info;
            
        }
Exemplo n.º 4
0
 private void ConvertAnnotation(ItemTitleInfo titleInfo, EPubFileV2 epubFile)
 {
     if (titleInfo.Annotation != null)
     {
         var desc = new Description
         {
             DescInfo = titleInfo.Annotation.ToString(),
         };
         epubFile.BookInformation.Description = desc;
         epubFile.AnnotationPage = new AnnotationPageFileV2();
         var converterSettings = new ConverterOptionsV2
         {
             CapitalDrop = Settings.CommonSettings.CapitalDrop,
             Images = Images,
             MaxSize = Settings.V2Settings.HTMLFileMaxSize,
             ReferencesManager = _referencesManager,
         };
         var annotationConverter = new AnnotationConverterV2();
         epubFile.AnnotationPage.BookAnnotation = (Div)annotationConverter.Convert(titleInfo.Annotation,
            new AnnotationConverterParamsV2 { Settings = converterSettings, Level = 1 });
     }
 }
        public HTMLItem Convert(FB2File fb2File, ConverterOptionsV2 settings)
        {
            if (fb2File == null)
            {
                throw new ArgumentNullException("fb2File");
            }
            var info   = new Div(HTMLElementType.XHTML11);
            var header = new H3(HTMLElementType.XHTML11);

            header.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
            {
                Text = "FB2 document info"
            });
            info.Add(header);
            if (fb2File.DocumentInfo != null)
            {
                if (!string.IsNullOrEmpty(fb2File.DocumentInfo.ID))
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                    {
                        Text = string.Format("Document ID:  {0}", fb2File.DocumentInfo.ID)
                    });
                    info.Add(p);
                }
                if (fb2File.DocumentInfo.DocumentVersion.HasValue)
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                    {
                        Text = string.Format("Document version:  {0}", fb2File.DocumentInfo.DocumentVersion)
                    });
                    info.Add(p);
                }
                if ((fb2File.DocumentInfo.DocumentDate != null) && !string.IsNullOrEmpty(fb2File.DocumentInfo.DocumentDate.Text))
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                    {
                        Text = string.Format("Document creation date:  {0}", fb2File.DocumentInfo.DocumentDate.Text)
                    });
                    info.Add(p);
                }
                if ((fb2File.DocumentInfo.ProgramUsed2Create != null) && !string.IsNullOrEmpty(fb2File.DocumentInfo.ProgramUsed2Create.Text))
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                    {
                        Text = string.Format("Created using:  {0} software", fb2File.DocumentInfo.ProgramUsed2Create.Text)
                    });
                    info.Add(p);
                }
                if ((fb2File.DocumentInfo.SourceOCR != null) && !string.IsNullOrEmpty(fb2File.DocumentInfo.SourceOCR.Text))
                {
                    var p = new Paragraph(HTMLElementType.XHTML11);
                    p.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                    {
                        Text = string.Format("OCR Source:  {0}", fb2File.DocumentInfo.SourceOCR.Text)
                    });
                    info.Add(p);
                }
                if ((fb2File.DocumentInfo.DocumentAuthors != null) && (fb2File.DocumentInfo.DocumentAuthors.Count > 0))
                {
                    var heading = new H4(HTMLElementType.XHTML11);
                    heading.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                    {
                        Text = "Document authors :"
                    });
                    info.Add(heading);
                    var authors = new UnorderedList(HTMLElementType.XHTML11);
                    foreach (var author in fb2File.DocumentInfo.DocumentAuthors)
                    {
                        var li = new ListItem(HTMLElementType.XHTML11);
                        li.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                        {
                            Text = DescriptionConverters.GetAuthorAsSting(author)
                        });
                        authors.Add(li);
                    }
                    info.Add(authors);
                }
                if ((fb2File.DocumentInfo.DocumentPublishers != null) && (fb2File.DocumentInfo.DocumentPublishers.Count > 0))
                {
                    var heading = new H4(HTMLElementType.XHTML11);
                    heading.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                    {
                        Text = "Document publishers :"
                    });
                    info.Add(heading);

                    var publishers = new UnorderedList(HTMLElementType.XHTML11);
                    foreach (var publisher in fb2File.DocumentInfo.DocumentPublishers)
                    {
                        var li = new ListItem(HTMLElementType.XHTML11);
                        li.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                        {
                            Text = DescriptionConverters.GetAuthorAsSting(publisher)
                        });
                        publishers.Add(li);
                    }
                    info.Add(publishers);
                }

                if ((fb2File.DocumentInfo.SourceURLs != null) && (fb2File.DocumentInfo.SourceURLs.Any()))
                {
                    var heading = new H4(HTMLElementType.XHTML11);
                    heading.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                    {
                        Text = "Source URLs :"
                    });
                    info.Add(heading);

                    var urls = new UnorderedList(HTMLElementType.XHTML11);
                    foreach (var url in fb2File.DocumentInfo.SourceURLs)
                    {
                        var li = new ListItem(HTMLElementType.XHTML11);
                        if (ReferencesUtils.IsExternalLink(url))
                        {
                            var link = new Anchor(HTMLElementType.XHTML11);
                            link.HRef.Value = url;
                            link.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                            {
                                Text = url
                            });
                            li.Add(link);
                        }
                        else
                        {
                            li.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                            {
                                Text = url
                            });
                        }
                        urls.Add(li);
                    }
                    info.Add(urls);
                }

                if (fb2File.DocumentInfo.History != null)
                {
                    var heading = new H4(HTMLElementType.XHTML11);
                    heading.Add(new SimpleHTML5Text(HTMLElementType.XHTML11)
                    {
                        Text = "Document history:"
                    });
                    info.Add(heading);
                    var annotationConverter = new AnnotationConverterV2();
                    info.Add(annotationConverter.Convert(fb2File.DocumentInfo.History, new AnnotationConverterParamsV2 {
                        Level = 1, Settings = settings
                    }));
                    //Paragraph p = new Paragraph();
                    //p.Add(new SimpleHTML5Text() { Text = fb2File.DocumentInfo.History.ToString() });
                    //info.Add(p);
                }
            }

            // in case there is no elements - no need for a header
            if (info.SubElements().Count <= 1)
            {
                info.Remove(header);
            }

            SetClassType(info, ElementStylesV2.FB2Info);
            return(info);
        }