Exemple #1
0
        public TeaserResult GenerateTeaser(
            TeaserTruncationMode truncationMode,
            int truncationLength,
            string html,
            string cacheKey,
            string slug,
            string languageCode)
        {
            var result = new TeaserResult();

            result.Content     = html;
            result.DidTruncate = false;

            return(result);
        }
        private ContentFilterResult FilterHtmlForList(IPost post, IProjectSettings settings, bool useTeaser)
        {
            var result = new ContentFilterResult();

            if (useTeaser)
            {
                TeaserResult teaserResult = null;
                string       teaser       = null;

                if (!string.IsNullOrWhiteSpace(post.TeaserOverride))
                {
                    if (post.ContentType == "markdown")
                    {
                        teaser = MapImageUrlsToCdn(
                            ConvertMarkdownToHtml(post.TeaserOverride),
                            settings.CdnUrl,
                            settings.LocalMediaVirtualPath);
                    }
                    else
                    {
                        teaser = MapImageUrlsToCdn(
                            post.TeaserOverride,
                            settings.CdnUrl,
                            settings.LocalMediaVirtualPath);
                    }

                    result.FilteredContent = teaser;
                    result.IsFullContent   = false;
                }
                else if (!string.IsNullOrWhiteSpace(post.AutoTeaser)) // as of 2018-08-20 this gets populated on save
                {
                    teaser = MapImageUrlsToCdn(
                        post.AutoTeaser,
                        settings.CdnUrl,
                        settings.LocalMediaVirtualPath);

                    result.FilteredContent = teaser;
                    result.IsFullContent   = false;
                }
                else
                {
                    // need to generate teaser
                    if (post.ContentType == "markdown")
                    {
                        var html = MapImageUrlsToCdn(
                            ConvertMarkdownToHtml(post.CoalesceContentToDraftContent()),
                            settings.CdnUrl,
                            settings.LocalMediaVirtualPath);

                        teaserResult = _teaserService.GenerateTeaser(
                            settings.TeaserTruncationMode,
                            settings.TeaserTruncationLength,
                            html,
                            post.Id,
                            post.Slug,
                            settings.LanguageCode
                            );
                    }
                    else
                    {
                        var html = MapImageUrlsToCdn(
                            post.CoalesceContentToDraftContent(),
                            settings.CdnUrl,
                            settings.LocalMediaVirtualPath);

                        teaserResult = _teaserService.GenerateTeaser(
                            settings.TeaserTruncationMode,
                            settings.TeaserTruncationLength,
                            html,
                            post.Id,
                            post.Slug,
                            settings.LanguageCode
                            );
                    }

                    result.FilteredContent = teaserResult.Content;
                    result.IsFullContent   = !teaserResult.DidTruncate;
                }
            }
            else
            {
                // using full content
                if (post.ContentType == "markdown")
                {
                    result.FilteredContent = MapImageUrlsToCdn(
                        ConvertMarkdownToHtml(post.CoalesceContentToDraftContent()),
                        settings.CdnUrl,
                        settings.LocalMediaVirtualPath);
                }
                else
                {
                    result.FilteredContent = MapImageUrlsToCdn(
                        post.CoalesceContentToDraftContent(),
                        settings.CdnUrl,
                        settings.LocalMediaVirtualPath);
                }

                result.IsFullContent = true;
            }



            return(result);
        }
        public TeaserResult GenerateTeaser(
            TeaserTruncationMode truncationMode,
            int truncationLength,
            string html,
            string cacheKey,
            string slug,
            string languageCode)
        {
            var result = new TeaserResult();

            if (string.IsNullOrWhiteSpace(html))
            {
                result.Content     = html;
                result.DidTruncate = false;
                return(result);
            }

            // Try to get language metadata for humanizer.
            var cultureInfo = CultureInfo.InvariantCulture;

            if (!string.IsNullOrEmpty(languageCode))
            {
                try
                {
                    cultureInfo = new CultureInfo(languageCode);
                }
                catch (CultureNotFoundException) { }
            }

            var contentLength = GetContentLength(html, truncationMode);

            if (contentLength <= truncationLength)
            {
                result.Content     = html;
                result.DidTruncate = false;
                return(result);
            }

            if (_cache != null)
            {
                var cachedTeaser = _cache.GetTeaser(cacheKey);
                if (!string.IsNullOrEmpty(cachedTeaser))
                {
                    result.Content     = cachedTeaser;
                    result.DidTruncate = true;
                    return(result);
                }
            }

            var isRightToLeftLanguage = cultureInfo.TextInfo.IsRightToLeft;

            // Get global teaser settings.
            var truncationLengthToUse = truncationLength <= 0 ? GetDefaultTeaserLength(truncationMode) : truncationLength;

            // Truncate the raw content first. In general, Humanizer is smart enough to ignore tags, especially if using word truncation.
            var text = TruncatePost(truncationMode, html, truncationLengthToUse, isRightToLeftLanguage);

            // Don't leave dangling <p> tags.
            HtmlNode.ElementsFlags["p"] = HtmlElementFlag.Closed;

            var modeDesc = GetModeDescription(truncationMode);

            //if we get bad output try increasing the allowed length unti it is valid
            while (!IsValidMarkup(text) && truncationLengthToUse <= contentLength)
            {
                truncationLengthToUse += 1;
                if (_log != null)
                {
                    _log.LogWarning($"teaser truncation for post {slug}, produced invalid html, so trying again and increasing the truncation length to {truncationLengthToUse} {modeDesc}. Might be best to make an explicit teaser for this post.");
                }

                text = TruncatePost(truncationMode, html, truncationLengthToUse, isRightToLeftLanguage);
            }

            if (!IsValidMarkup(text))
            {
                if (_log != null)
                {
                    _log.LogError($"failed to create valid teaser for post {slug}, so returning full content");
                }

                result.Content     = html;
                result.DidTruncate = false;
                return(result);
            }

            if (_cache != null)
            {
                _cache.AddToCache(text, cacheKey);
            }

            var doc = new HtmlDocument();

            doc.LoadHtml(text);

            result.Content     = doc.DocumentNode.InnerHtml;
            result.DidTruncate = true;
            return(result);
        }