Example #1
0
        public string RenderResults(string searchTerms, int page)
        {
            string errorMessage = "";

            if (searchTerms.Length > 128)
            {
                errorMessage = "The query is too long.";
            }
            else
            {
                try
                {
                    return(RenderResultsInternal(searchTerms, page));
                }
                catch (ParseException)
                {
                    errorMessage = String.Format("Unable to parse query '{0}', be sure to match quotes and parentheses.", searchTerms);
                }
            }

            var summary = new Dictionary <string, Action <TextWriter> >();

            summary.Add("search-terms", w => w.Write(HttpRequestUtil.HtmlEncode(errorMessage)));
            summary.Add("search-result", w => { });

            using (StringWriter sw = new StringWriter())
            {
                RenderTemplate(sw, searchTerms, summary);
                return(sw.ToString());
            }
        }
Example #2
0
        private string RenderResults(string description, string terms, int page, int pageSize, int total, IEnumerable <ContentSearchResult> results)
        {
            using (StringWriter wtr = new StringWriter())
            {
                var summary = new Dictionary <string, Action <TextWriter> >();
                summary.Add("search-terms", w => w.Write(HttpRequestUtil.HtmlEncode(description)));
                try
                {
                    if (total > 0)
                    {
                        summary.Add("search-result", w => WriteResultHtml(w, terms, page, pageSize, total, results));
                    }
                    else
                    {
                        summary.Add("search-result", w => WriteErrorHtml(w, "No results found."));
                    }

                    RenderTemplate(wtr, terms, summary);
                }
                catch
                {
                    wtr.GetStringBuilder().Length = 0;

                    summary.Remove("search-result");
                    RenderTemplate(wtr, terms, summary);
                }

                return(wtr.ToString());
            }
        }
Example #3
0
        private void RenderTemplate(TextWriter wtr, string terms, IDictionary <string, Action <TextWriter> > values)
        {
            string[] parts = GetTemplate();
            if (((parts.Length - 1) % 4) != 0)
            {
                throw new ApplicationException("Invalid template format.");
            }

            if (_valuePartIndex >= 0 && _valuePartOffset >= 0)
            {
                parts = (string[])parts.Clone();
                parts[_valuePartIndex] = parts[_valuePartIndex].Insert(_valuePartOffset, HttpRequestUtil.HtmlEncode(terms));
            }

            wtr.Write(parts[0]);
            for (int ix = 1; ix < parts.Length; ix += 4)
            {
                if (parts[ix + 2] != '/' + parts[ix])
                {
                    throw new ApplicationException("Invalid template format.");
                }

                Action <TextWriter> fn;
                if (values.TryGetValue(parts[ix], out fn))
                {
                    fn(wtr);
                }
                else
                {
                    wtr.Write(parts[ix + 1]);
                }

                wtr.Write(parts[ix + 3]);
            }
        }