void VoidTag()
        {
            var result = Downsize.Substring("<p>test <img src=\"blah.jpg\"> <strong>stuffo</strong> some stuff</p>",
                                            new DownsizeOptions(words: 2));

            Assert.Equal("<p>test <img src=\"blah.jpg\"> <strong>stuffo</strong></p>", result);
        }
Example #2
0
        void Test()
        {
            var sb = new StringBuilder();

            for (var i = 0; i < 1000000; i++)
            {
                sb.Append("<p>word truncate performance test</p>\n");
            }
            var perfTestSeed = sb.ToString();

            var sw = new Stopwatch();

            sw.Reset();
            sw.Start();

            var options = new DownsizeOptions(words: 5);

            for (var i = 0; i < 100000; i++)
            {
                Downsize.Substring(perfTestSeed, options);
            }

            sw.Stop();

            output.WriteLine($"Operation time: {sw.ElapsedMilliseconds}");
        }
        void CloseUnknownTags()
        {
            var result = Downsize.Substring("<p>test <unknown> <strong>stuffo</strong> some stuff</p>",
                                            new DownsizeOptions(words: 2));

            Assert.Equal("<p>test <unknown> <strong>stuffo</strong></unknown></p>", result);
        }
        void NoTrailingEmptyTags()
        {
            var result = Downsize.Substring("<p>there are five words here</p><i>what</i>",
                                            new DownsizeOptions(words: 5));

            Assert.Equal("<p>there are five words here</p>", result);
        }
        void NestedTag()
        {
            var result = Downsize.Substring("<p>this is a <strong>test of word downsizing</strong></p>",
                                            new DownsizeOptions(words: 5));

            Assert.Equal("<p>this is a <strong>test of</strong></p>", result);
        }
Example #6
0
        void TaglessText()
        {
            var result = Downsize.Substring("this is a test of tagless input",
                                            new DownsizeOptions(characters: 6));

            Assert.Equal("this i", result);
        }
        void ZeroWordsOption()
        {
            var result = Downsize.Substring("<p>this is a <strong>test of word downsizing</strong></p>",
                                            new DownsizeOptions(words: 0));

            Assert.Equal(string.Empty, result);
        }
Example #8
0
        void NoTrailingEmptyTags()
        {
            var result = Downsize.Substring("<p>characters</p><i>what</i>",
                                            new DownsizeOptions(characters: 10));

            Assert.Equal("<p>characters</p>", result);
        }
        void UnescapedCaretsInsideDoubleQuotedStrings()
        {
            var result = Downsize.Substring("<p>test string <img \"<stuffo>\"> <strong>stuffo</strong> some stuff</p>",
                                            new DownsizeOptions(words: 3));

            Assert.Equal("<p>test string <img \"<stuffo>\"> <strong>stuffo</strong></p>", result);
        }
        void SingleQuote()
        {
            var result = Downsize.Substring("<p><img src=\"/someUrl.jpg\" alt=\"Let's get in!\"></p><p>hello world</p>",
                                            new DownsizeOptions(words: 1));

            Assert.Equal("<p><img src=\"/someUrl.jpg\" alt=\"Let's get in!\"></p><p>hello</p>", result);
        }
        void Comments()
        {
            var result = Downsize.Substring(
                "<p>this <!-- is a > test < test --> <strong>test of word downsizing</strong> some stuff</p>",
                new DownsizeOptions(words: 2));

            Assert.Equal("<p>this <!-- is a > test < test --> <strong>test</strong></p>", result);
        }
        void UnicodeCharInTag()
        {
            var result = Downsize.Substring(
                "<p>Рэпудёандаэ конжыквуюнтюр эю прё, <span>нэ квуй янжольэнж квюальизквюэ</span> чадипжкёнг. Ед кюм жкрипта</p>",
                new DownsizeOptions(words: 3));

            Assert.Equal("<p>Рэпудёандаэ конжыквуюнтюр эю</p>", result);
        }
        void UnicodeChar()
        {
            var result = Downsize.Substring(
                "Рэпудёандаэ конжыквуюнтюр эю прё, нэ квуй янжольэнж квюальизквюэ чадипжкёнг. Ед кюм жкрипта",
                new DownsizeOptions(words: 3));

            Assert.Equal("Рэпудёандаэ конжыквуюнтюр эю", result);
        }
        void ManuallyClosedElements()
        {
            var result = Downsize.Substring(
                "<p>tag closing test</p><p>There should only</p><p>be one terminating para</p>",
                new DownsizeOptions(words: 7));

            Assert.Equal("<p>tag closing test</p><p>There should only</p><p>be</p>", result);
        }
        void BalanceMarkup()
        {
            var result =
                Downsize.Substring("<p><p><p><p>this is a <strong>test of word downsizing</strong> some stuff</p>",
                                   new DownsizeOptions(words: 5));

            Assert.Equal("<p><p><p><p>this is a <strong>test of</strong></p></p></p></p>", result);
        }
Example #16
0
        void WordTruncationAppendWithoutHtml()
        {
            var result = Downsize.Substring("here's some text.",
                                            new DownsizeOptions(
                                                words: 2,
                                                append: "..."));

            Assert.Equal("here's some...", result);
        }
Example #17
0
        void WordTruncationAppend()
        {
            var result = Downsize.Substring("<p>abcdefghij</p><p>klmnop</p><p>qrs</p>",
                                            new DownsizeOptions(
                                                characters: 15,
                                                append: "..."));

            Assert.Equal("<p>abcdefghij</p><p>klmno...</p>", result);
        }
Example #18
0
        void SentencesShorterThanRequired()
        {
            var result = Downsize.Substring("<p>here's some text.</p>",
                                            new DownsizeOptions(
                                                words: 5,
                                                round: true));

            Assert.Equal("<p>here's some text.</p>", result);
        }
Example #19
0
        void RoundUpToEndOfSentence()
        {
            var result = Downsize.Substring("<p>here's <em>some</em> text.</p>",
                                            new DownsizeOptions(
                                                characters: 2,
                                                round: true));

            Assert.Equal("<p>here's <em>some</em> text.</p>", result);
        }
Example #20
0
        void RoundSentenceUp()
        {
            var result = Downsize.Substring("<p>abcdefghij</p><p>klmnop</p><p>qrs</p>",
                                            new DownsizeOptions(
                                                characters: 15,
                                                round: true));

            Assert.Equal("<p>abcdefghij</p><p>klmnop</p>", result);
        }
Example #21
0
        void NotAppendEllipsisWhereNotRequired()
        {
            var result = Downsize.Substring("<p>here's some text.</p>",
                                            new DownsizeOptions(
                                                words: 5,
                                                append: "..."));

            Assert.Equal("<p>here's some text.</p>", result);
        }
Example #22
0
        void CharacterTruncationAppendWithoutHtml()
        {
            var result = Downsize.Substring("here's some text.",
                                            new DownsizeOptions(
                                                characters: 6,
                                                append: "..."));

            Assert.Equal("here's...", result);
        }
Example #23
0
        void CharacterTruncationAppend()
        {
            var result = Downsize.Substring("<p>here's some text.</p>",
                                            new DownsizeOptions(
                                                words: 2,
                                                append: "... (read more)"));

            Assert.Equal("<p>here's some... (read more)</p>", result);
        }
Example #24
0
        void CharacterTruncateAcrossTag()
        {
            var result = Downsize.Substring("<p>abcdefghij</p><p>klmnop</p><p>qrs</p>",
                                            new DownsizeOptions(characters: 15));

            Assert.Equal("<p>abcdefghij</p><p>klmno</p>", result);

            result = Downsize.Substring("<p>a</p><p>b</p><p>cdefghij</p><p>klmnop</p><p>qrs</p>",
                                        new DownsizeOptions(characters: 15));
            Assert.Equal("<p>a</p><p>b</p><p>cdefghij</p><p>klmno</p>", result);
        }
        void NotCloseSelfclosingTags()
        {
            var result = Downsize.Substring("<p>test <random selfclosing /> <strong>stuffo</strong> some stuff</p>",
                                            new DownsizeOptions(words: 2));

            Assert.Equal("<p>test <random selfclosing /> <strong>stuffo</strong></p>", result);

            result = Downsize.Substring("<p>test <random selfclosing / > <strong>stuffo</strong> some stuff</p>",
                                        new DownsizeOptions(words: 2));
            Assert.Equal("<p>test <random selfclosing / > <strong>stuffo</strong></p>", result);
        }
        void IgnoreErroneouslyUnescapedCarets()
        {
            var result = Downsize.Substring("<p>this < is a <strong>test of word downsizing</strong> some stuff</p>",
                                            new DownsizeOptions(words: 5));

            Assert.Equal("<p>this < is a <strong>test of</strong></p>", result);

            result = Downsize.Substring(
                "<p>this < is a > test < test > <strong>test of word downsizing</strong> some stuff</p>",
                new DownsizeOptions(words: 5));
            Assert.Equal("<p>this < is a > test < test</p>", result);
        }
        void AwaitTheEndOfTheContainingParagraph()
        {
            var result = Downsize.Substring(
                "<p>there are more than seven words in this paragraph</p><p>this is unrelated</p>",
                new DownsizeOptions(
                    words: 7,
                    contextualTags: new List <string>()
            {
                "p", "ul", "ol", "pre", "blockquote"
            }));

            Assert.Equal("<p>there are more than seven words in this paragraph</p>", result);
        }
        void AwaitTheEndOfTheContainingUnorderedList()
        {
            var result = Downsize.Substring(
                "<ul><li>item one</li><li>item two</li><li>item three</li></ul><p>paragraph</p>",
                new DownsizeOptions(
                    words: 5,
                    contextualTags: new List <string>()
            {
                "p", "ul", "ol", "pre", "blockquote"
            }));

            Assert.Equal("<ul><li>item one</li><li>item two</li><li>item three</li></ul>", result);
        }
Example #29
0
        public override void HandlebarsHelper(TextWriter output, dynamic dContext, params object[] oArguments)
        {
            if (dContext is IExcerpt post)
            {
                if (post.Html.IsNullOrEmpty())
                {
                    return;
                }
                string excerpt;
                if (post.CustomExcerpt.IsNullOrEmpty())
                {
                    excerpt = post.Html.RegexReplace("<a href=\"#fn.*?rel=\"footnote\">.*?</a>",
                                                     RegexOptions.IgnoreCase)
                              .RegexReplace("<div class=\"footnotes\"><ol>.*?</ol></div>")
                              .RegexReplace("</?[^>]+>", RegexOptions.IgnoreCase)
                              .RegexReplace("(\r\n|\n|\r)+", " ", RegexOptions.Multiline);
                }
                else
                {
                    excerpt = post.CustomExcerpt;
                }

                var arguments  = oArguments[0].CastTo <HashParameterDictionary>();
                var parameters = new Dictionary <string, object>()
                {
                    { "words", arguments.GetValueOrDefault("words", DownsizeOptions.DefaultTruncationLimit).CastToInt() },
                    { "characters", arguments.GetValueOrDefault("characters", DownsizeOptions.DefaultTruncationLimit).CastToInt() },
                    { "round", arguments.GetValueOrDefault("round", false) },
                };
                var ctor    = typeof(DownsizeOptions).GetConstructors()[0];
                var options = ctor.InvokeWithNamedParameters <DownsizeOptions>(parameters);

                excerpt = Downsize.Substring(excerpt, options);
                output.WriteSafeString(excerpt);
            }
        }
        void TaglessText()
        {
            var result = Downsize.Substring("this is a test of tagless input", new DownsizeOptions(words: 5));

            Assert.Equal("this is a test of", result);
        }