Пример #1
0
 public string AddImageRefferenced(ImageItem item, Image img)
 {
     if (item == null)
     {
         return string.Empty;
     }
     string validName = MakeValidImageName(item.HRef);
     
     if (!_images.ContainsKey(validName))
     {
        var entryList = new List<Image>();
         _images.Add(validName, entryList);
     }
     _images[validName].Add(img);
     return ReferencesUtils.FormatImagePath(validName, FlatStructure);
 }
Пример #2
0
        /// <summary>
        /// Convert FB2 image item
        /// </summary>
        /// <param name="imageItem">item to convert</param>
        /// <param name="imageConverterParams"></param>
        /// <returns>XHTML representation</returns>
        public IHTMLItem Convert(ImageItem imageItem,ImageConverterParamsV2 imageConverterParams)
        {
            if (imageItem == null)
            {
                throw new ArgumentNullException("imageItem");
            }
            var image = new Image(HTMLElementType.XHTML11);
            image.Alt.Value = imageItem.AltText ?? string.Empty; // ePub require image always to have attribute
            image.Source.Value = imageConverterParams.Settings.ReferencesManager.AddImageRefferenced(imageItem, image);

            image.GlobalAttributes.ID.Value = imageConverterParams.Settings.ReferencesManager.AddIdUsed(imageItem.ID, image);
            if (imageItem.Title != null)
            {
                image.GlobalAttributes.Title.Value = imageItem.Title;
            }
            imageConverterParams.Settings.Images.ImageIdUsed(imageItem.HRef);
            return image;
        }
 private ulong ConvertSectionImage(ImageItem sectionImage, Div content, IList<IHTMLItem> resList, ulong documentSize)
 {
     ulong docSize = documentSize;
     if (sectionImage.HRef != null)
     {
         if (Settings.Images.IsImageIdReal(sectionImage.HRef))
         {
             var container = new Div(HTMLElementType.HTML5);
             var sectionImagemage = new Image(HTMLElementType.HTML5);
             sectionImagemage.Alt.Value = sectionImage.AltText ?? string.Empty;
             sectionImagemage.Source.Value = Settings.ReferencesManager.AddImageRefferenced(sectionImage, sectionImagemage);
             sectionImagemage.GlobalAttributes.ID.Value = Settings.ReferencesManager.AddIdUsed(sectionImage.ID,
                                                                     sectionImagemage);
             if (sectionImage.Title != null)
             {
                 sectionImagemage.GlobalAttributes.Title.Value = sectionImage.Title;
             }
             SetClassType(container, ElementStylesV3.SectionImage);
             container.Add(sectionImagemage);
             ulong itemSize = container.EstimateSize();
             if (_checker.ExceedSizeLimit(documentSize + itemSize))
             {
                 var oldContent = content;
                 resList.Add(content);
                 content = new Div(HTMLElementType.HTML5);
                 content.GlobalAttributes.Class.Value = oldContent.GlobalAttributes.Class.Value;
                 content.GlobalAttributes.Language.Value = oldContent.GlobalAttributes.Language.Value;
                 docSize = 0;
             }
             docSize += itemSize;
             content.Add(container);
             Settings.Images.ImageIdUsed(sectionImage.HRef);
         }
     }
     return docSize;
 }
Пример #4
0
        internal void Load(XElement xSection)
        {
            ClearAll();
            if (xSection == null)
            {
                throw new ArgumentNullException("xSection");
            }

            if (xSection.Name.LocalName != Fb2TextSectionElementName)
            {
                throw new ArgumentException("Element of wrong type passed", "xSection");
            }

            XElement xTitle = xSection.Element(xSection.Name.Namespace + TitleItem.Fb2TitleElementName);
            if (xTitle != null)
            {
                Title = new TitleItem();
                try
                {
                    Title.Load(xTitle);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Failed to load section title : {0}.", ex.Message));
                }
            }
            
            IEnumerable<XElement> xEpigraphs =
                xSection.Elements(xSection.Name.Namespace + EpigraphItem.Fb2EpigraphElementName);
            foreach (var xEpigraph in xEpigraphs)
            {
                EpigraphItem epigraph = new EpigraphItem();
                try
                {
                    epigraph.Load(xEpigraph);
                    _epigraphs.Add(epigraph);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Failed to load section epigraph : {0}.", ex.Message));
                }
            }

            XElement xAnnotation = xSection.Element(xSection.Name.Namespace + AnnotationItem.Fb2AnnotationItemName);
            if (xAnnotation != null)
            {
                Annotation  = new AnnotationItem();
                try
                {
                    Annotation.Load(xAnnotation);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Failed to load section annotation : {0}.", ex.Message));
                }
            }

            IEnumerable<XElement> xElements = xSection.Elements();
            foreach (var xElement in xElements)
            {
                switch (xElement.Name.LocalName)
                {
                    case ParagraphItem.Fb2ParagraphElementName:
                        ParagraphItem paragraph = new ParagraphItem();
                        try
                        {
                            paragraph.Load(xElement);
                            _content.Add(paragraph);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(string.Format("Failed to load section paragraph : {0}.", ex.Message));
                        }
                        break;
                    case PoemItem.Fb2PoemElementName:
                        PoemItem poem = new PoemItem();
                        try
                        {
                            poem.Load(xElement);
                            _content.Add(poem);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(string.Format("Failed to load section poem : {0}.", ex.Message));
                        }
                        break;
                    case ImageItem.Fb2ImageElementName:
                        ImageItem image = new ImageItem();
                        try
                        {
                            image.Load(xElement);
                            AddImage(image);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(string.Format("Failed to load section image : {0}.", ex.Message));
                        }
                        break;
                    case SubTitleItem.Fb2SubtitleElementName:
                        SubTitleItem subtitle = new SubTitleItem();
                        try
                        {
                            subtitle.Load(xElement);
                            _content.Add(subtitle);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(string.Format("Failed to load section subtitle : {0}.", ex.Message));
                        }
                        break;
                    case CiteItem.Fb2CiteElementName:
                        CiteItem cite = new CiteItem();
                        try
                        {
                            cite.Load(xElement);
                            _content.Add(cite);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(string.Format("Failed to load section citation : {0}.", ex.Message));
                        }
                        break;
                    case EmptyLineItem.Fb2EmptyLineElementName:
                        EmptyLineItem eline = new EmptyLineItem();
                        _content.Add(eline);
                        break;
                    case TableItem.Fb2TableElementName:
                        TableItem table = new TableItem();
                        try
                        {
                            table.Load(xElement);
                            _content.Add(table);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(string.Format("Failed to load section emptly line : {0}.", ex.Message));
                        }
                        break;
                    case Fb2TextSectionElementName: // internal <section> read recurive
                        SectionItem section = new SectionItem();
                        try
                        {
                            section.Load(xElement);
                            AddSection(section);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(string.Format("Failed to load sub-section : {0}.", ex.Message));
                        }
                        break;
                    case AnnotationItem.Fb2AnnotationItemName: // already processed
                        break; 
                    case TitleItem.Fb2TitleElementName: // already processed
                        break;
                    case EpigraphItem.Fb2EpigraphElementName: // already processed
                        break;
                    default:
                        if (!string.IsNullOrEmpty(xElement.Value))
                        {
                            ParagraphItem tempParagraph = new ParagraphItem();
                            try
                            {
                                SimpleText text = new SimpleText {Text = xElement.Value};
                                tempParagraph.ParagraphData.Add(text);
                                _content.Add(tempParagraph);
                            }
                            catch
                            {
                                // ignored
                            }
                        }
                        Debug.WriteLine("AnnotationItem:Load - invalid element <{0}> encountered in title .", xElement.Name.LocalName);
                        break;
                }
            }

            ID = null;
            XAttribute xID = xSection.Attribute("id");
            if ((xID != null))
            {
                ID = xID.Value;
            }

            Lang = null;
            XAttribute xLang = xSection.Attribute(XNamespace.Xml + "lang");
            if ((xLang != null))
            {
                Lang = xLang.Value;
            }
        }
Пример #5
0
 private void DetectSectionImage(ImageItem image)
 {
     foreach (var item in _content)
     {
         if (item.GetType() == typeof(ImageItem)) // if we have an image
         {
             if (item == image) // if the item we currently lookin at is our image, means it at start and it's a section image
             {
                 _sectionImages.Add(image); // add it to list of section images
                 return;                       
             }
             continue; // skip to next item in list
         }
         if (item.GetType() == typeof(TitleItem))
         {
             // title according to schema can come before image
             continue;
         }
         if (item.GetType() == typeof(EpigraphItem))
         {
             // epigraph according to schema can come before image
             continue;
         }
         // according to schema images goes first 
         // wrong type means we passed possible section image location
         return;
     }
 }
Пример #6
0
 private void AddImage(ImageItem image)
 {
     _content.Add(image);
     _images.Add(image);
     DetectSectionImage(image);
 }
Пример #7
0
        internal void Load(XElement xSection)
        {
            ClearAll();
            if (xSection == null)
            {
                throw new ArgumentNullException("xSection");
            }

            if (xSection.Name.LocalName != Fb2TextSectionElementName)
            {
                throw new ArgumentException("Element of wrong type passed", "xSection");
            }

            XElement xTitle = xSection.Element(xSection.Name.Namespace + TitleItem.Fb2TitleElementName);

            if (xTitle != null)
            {
                Title = new TitleItem();
                try
                {
                    Title.Load(xTitle);
                }
                catch (Exception ex)
                {
                    Debug.Fail(string.Format("Failed to load section title : {0}.", ex.Message));
                }
            }

            IEnumerable <XElement> xEpigraphs =
                xSection.Elements(xSection.Name.Namespace + EpigraphItem.Fb2EpigraphElementName);

            foreach (var xEpigraph in xEpigraphs)
            {
                EpigraphItem epigraph = new EpigraphItem();
                try
                {
                    epigraph.Load(xEpigraph);
                    _epigraphs.Add(epigraph);
                }
                catch (Exception ex)
                {
                    Debug.Fail(string.Format("Failed to load section epigraph : {0}.", ex.Message));
                }
            }

            XElement xAnnotation = xSection.Element(xSection.Name.Namespace + AnnotationItem.Fb2AnnotationItemName);

            if (xAnnotation != null)
            {
                Annotation = new AnnotationItem();
                try
                {
                    Annotation.Load(xAnnotation);
                }
                catch (Exception ex)
                {
                    Debug.Fail(string.Format("Failed to load section annotation : {0}.", ex.Message));
                }
            }

            IEnumerable <XElement> xElements = xSection.Elements();

            foreach (var xElement in xElements)
            {
                switch (xElement.Name.LocalName)
                {
                case ParagraphItem.Fb2ParagraphElementName:
                    ParagraphItem paragraph = new ParagraphItem();
                    try
                    {
                        paragraph.Load(xElement);
                        _content.Add(paragraph);
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail(string.Format("Failed to load section paragraph : {0}.", ex.Message));
                    }
                    break;

                case PoemItem.Fb2PoemElementName:
                    PoemItem poem = new PoemItem();
                    try
                    {
                        poem.Load(xElement);
                        _content.Add(poem);
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail(string.Format("Failed to load section poem : {0}.", ex.Message));
                    }
                    break;

                case ImageItem.Fb2ImageElementName:
                    ImageItem image = new ImageItem();
                    try
                    {
                        image.Load(xElement);
                        AddImage(image);
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail(string.Format("Failed to load section image : {0}.", ex.Message));
                    }
                    break;

                case SubTitleItem.Fb2SubtitleElementName:
                    SubTitleItem subtitle = new SubTitleItem();
                    try
                    {
                        subtitle.Load(xElement);
                        _content.Add(subtitle);
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail(string.Format("Failed to load section subtitle : {0}.", ex.Message));
                    }
                    break;

                case CiteItem.Fb2CiteElementName:
                    CiteItem cite = new CiteItem();
                    try
                    {
                        cite.Load(xElement);
                        _content.Add(cite);
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail(string.Format("Failed to load section citation : {0}.", ex.Message));
                    }
                    break;

                case EmptyLineItem.Fb2EmptyLineElementName:
                    EmptyLineItem eline = new EmptyLineItem();
                    _content.Add(eline);
                    break;

                case TableItem.Fb2TableElementName:
                    TableItem table = new TableItem();
                    try
                    {
                        table.Load(xElement);
                        _content.Add(table);
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail(string.Format("Failed to load section emptly line : {0}.", ex.Message));
                    }
                    break;

                case Fb2TextSectionElementName:     // internal <section> read recurive
                    SectionItem section = new SectionItem();
                    try
                    {
                        section.Load(xElement);
                        AddSection(section);
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail(string.Format("Failed to load sub-section : {0}.", ex.Message));
                    }
                    break;

                case AnnotationItem.Fb2AnnotationItemName:     // already processed
                    break;

                case TitleItem.Fb2TitleElementName:     // already processed
                    break;

                case EpigraphItem.Fb2EpigraphElementName:     // already processed
                    break;

                default:
                    if (!string.IsNullOrEmpty(xElement.Value))
                    {
                        ParagraphItem tempParagraph = new ParagraphItem();
                        try
                        {
                            SimpleText text = new SimpleText {
                                Text = xElement.Value
                            };
                            tempParagraph.ParagraphData.Add(text);
                            _content.Add(tempParagraph);
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                    Debug.Print("AnnotationItem:Load - invalid element <{0}> encountered in title .", xElement.Name.LocalName);
                    break;
                }
            }

            ID = null;
            XAttribute xID = xSection.Attribute("id");

            if ((xID != null))
            {
                ID = xID.Value;
            }

            Lang = null;
            XAttribute xLang = xSection.Attribute(XNamespace.Xml + "lang");

            if ((xLang != null))
            {
                Lang = xLang.Value;
            }
        }
Пример #8
0
 private void AddImage(ImageItem image)
 {
     _content.Add(image);
     _images.Add(image);
     DetectSectionImage(image);
 }
Пример #9
0
        internal void Load(XElement xBody)
        {
            if (xBody == null)
            {
                throw new ArgumentNullException("xBody");
            }

            if (xBody.Name.LocalName != Fb2BodyItemName)
            {
                throw new ArgumentException("Element of wrong type passed", "xBody");
            }


            ImageName = null;
            XElement xImage = xBody.Element(NameSpace + Fb2ImageElementName);

            if ((xImage != null))
            {
                ImageName = new ImageItem();
                try
                {
                    ImageName.Load(xImage);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Error reading body image: {0}", ex.Message));
                }
            }


            XElement xTitle = xBody.Element(NameSpace + TitleItem.Fb2TitleElementName);

            if ((xTitle != null) && (xTitle.Value != null))
            {
                try
                {
                    title.Load(xTitle);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Error reading body title: {0}", ex.Message));
                }
            }


            // Load epigraph elements
            IEnumerable <XElement> xEpigraphs = xBody.Elements(NameSpace + EpigraphItem.Fb2EpigraphElementName);

            epigraphs.Clear();
            foreach (var xEpigraph in xEpigraphs)
            {
                EpigraphItem item = new EpigraphItem();
                try
                {
                    item.Load(xEpigraph);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Error reading body epigraph: {0}", ex.Message));
                    continue;
                }
                epigraphs.Add(item);
            }


            // Load body elements (first is main text)
            IEnumerable <XElement> xSections = xBody.Elements(NameSpace + SectionItem.Fb2TextSectionElementName);

            sections.Clear();
            foreach (var section in xSections)
            {
                SectionItem item = new SectionItem();
                try
                {
                    item.Load(section);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Error reading body sections: {0}", ex.Message));
                    continue;
                }
                sections.Add(item);
            }

            Name = string.Empty;
            XAttribute xName = xBody.Attribute(Fb2NameAttributeName);

            if ((xName != null) && (xName.Value != null))
            {
                Name = xName.Value;
            }



            Lang = null;
            XAttribute xLang = xBody.Attribute(XNamespace.Xml + "lang");

            if ((xLang != null) && (xLang.Value != null))
            {
                Lang = xLang.Value;
            }
        }
Пример #10
0
        internal void Load(XElement xBody)
        {
            if (xBody == null)
            {
                throw new ArgumentNullException("xBody");
            }

            if (xBody.Name.LocalName != Fb2BodyItemName)
            {
                throw new ArgumentException("Element of wrong type passed", "xBody");
            }


            ImageName = null;
            XElement xImage = xBody.Element(NameSpace + Fb2ImageElementName);
            if ((xImage != null))
            {
                ImageName = new ImageItem();
                try
                {
                    ImageName.Load(xImage);
                }
                catch (Exception ex)
                {
                    Debug.Fail(string.Format("Error reading body image: {0}", ex.Message));
                }
            }


            XElement xTitle = xBody.Element(NameSpace + TitleItem.Fb2TitleElementName);
            if ((xTitle != null) && (xTitle.Value != null))
            {
                try
                {
                    title.Load(xTitle);
                }
                catch (Exception ex)
                {
                    Debug.Fail(string.Format("Error reading body title: {0}", ex.Message));
                }
            }


            // Load epigraph elements
            IEnumerable<XElement> xEpigraphs = xBody.Elements(NameSpace + EpigraphItem.Fb2EpigraphElementName);
            epigraphs.Clear();
            foreach (var xEpigraph in xEpigraphs)
            {
                EpigraphItem item = new EpigraphItem();
                try
                {
                    item.Load(xEpigraph);
                }
                catch (Exception ex)
                {
                    Debug.Fail(string.Format("Error reading body epigraph: {0}", ex.Message));
                    continue;
                }
                epigraphs.Add(item);
            }


            // Load body elements (first is main text)
            IEnumerable<XElement> xSections = xBody.Elements(NameSpace + SectionItem.Fb2TextSectionElementName);
            sections.Clear();
            foreach (var section in xSections)
            {
                SectionItem item = new SectionItem();
                try
                {
                    item.Load(section);
                }
                catch (Exception ex)
                {
                    Debug.Fail(string.Format("Error reading body sections: {0}", ex.Message));
                    continue;
                }
                sections.Add(item);
            }

            Name = string.Empty;
            XAttribute xName = xBody.Attribute(Fb2NameAttributeName);
            if ((xName != null) && (xName.Value != null))
            {
                Name = xName.Value;
            }



            Lang = null;
            XAttribute xLang = xBody.Attribute(XNamespace.Xml + "lang");
            if ((xLang != null) && (xLang.Value != null))
            {
                Lang = xLang.Value;
            }


        }