Ejemplo n.º 1
0
        static SrcSet _AttributeKeyValuesToSrcSet(Dictionary <string, string> kvs)
        {
            if (kvs.IsNulle())
            {
                return(null);
            }

            string src    = kvs.V("src");
            string srcSet = kvs.V("srcset");

            if (src.IsNulle())
            {
                return(null);
            }

            var srcImg = new SrcSetImg()
            {
                Url      = src,
                Size     = kvs.V("width").ToInt(0),
                SizeType = SrcSizeType.Width
            };

            SrcSet srcset = new SrcSet()
            {
                Src             = srcImg,
                Sizes           = kvs.V("sizes"),
                SrcSets         = SrcSet.ParseSrcSets(srcSet, out SrcSizeType sizeType),
                SrcSetsSizeType = sizeType
            };

            srcset.InitValues();

            return(srcset);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Searches for the *first* `img` tag within the input string (ideally a content /
        /// description field within an RSS item).
        /// </summary>
        /// <param name="html"></param>
        /// <param name="contentSettings"></param>
        /// <param name="maxStartIndexOfImgTag"></param>
        public SrcSet GetFirstImageTagFromHtmlContent(
            string html,
            SFContentConversionSettings contentSettings,
            int maxStartIndexOfImgTag = -1)
        {
            if (html.IsNulle() || maxStartIndexOfImgTag < 0 || !contentSettings.GetFirstImageTagFromHtml)
            {
                return(null);
            }

            int maxSearchLen = html.Length;

            if (maxStartIndexOfImgTag >= 0)
            {
                maxSearchLen = (maxStartIndexOfImgTag + 15).Min(html.Length);                 // 15, just a rough min after start of img tag, still need an href + the url itself...
            }
            int imgIdx = html.IndexOf("<img ", 0, maxSearchLen);

            if (imgIdx < 0)
            {
                return(null);
            }

            Dictionary <string, string> imgAttributes = contentSettings
                                                        .GetFirstImageTagAttributesFromHtml(html, imgIdx);

            if (imgAttributes.IsNulle() || !imgAttributes.ContainsKey("src"))
            {
                return(null);
            }

            SrcSet srcSet = _AttributeKeyValuesToSrcSet(imgAttributes);

            return(srcSet);
        }