Ejemplo n.º 1
0
        public static string WikiParse(this WikiSettings settings, string content)
        {
            using (HeavyProfiler.Log("Wiki"))
                using (var t = HeavyProfiler.LogNoStackTrace("SaveCodeRegions"))
                {
                    string result = content;

                    result = SaveCodeRegions(result, out Dictionary <string, string> codeRegions);

                    if (settings.EncodeHtml)
                    {
                        t.Switch("HtmlEncode");
                        //1: Replace token delimiters which are different from their encoded string so that they are not encoded
                        result = Regex.Replace(result, "'{2,}", m => "####" + m.Length + "####");


                        //2: Encode all text
                        result = HttpUtility.HtmlEncode(result);

                        //3: Replace encrypted tokens to original tokens
                        result = Regex.Replace(result, "####(?<count>\\d+)####", m => new string('\'', int.Parse(m.Groups["count"].Value)));
                    }
                    t.Switch("ProcessTokens");
                    result = ProcessTokens(result, settings);
                    t.Switch("ProcessFormat");

                    result = ProcessFormat(result, settings);
                    t.Switch("WriteCodeRegions");

                    result = WriteCodeRegions(result, codeRegions, settings);

                    return(result.Trim());
                }
        }
Ejemplo n.º 2
0
        static string ProcessFormat(string content, WikiSettings settings)
        {
            content = RegexBoldItalics.Replace(content, (settings.Strong && settings.Em) ? "<strong><em>${content}</em></strong>" : "${content}");
            content = RegexBold.Replace(content, settings.Strong ? "<strong>${content}</strong>" : "${content}");
            content = RegexItalics.Replace(content, settings.Em ? "<em>${content}</em>" : "${content}");
            content = RegexUnderline.Replace(content, settings.Underlined ? "<u>${content}</u>" : "${content}");
            content = RegexStrike.Replace(content, settings.Strike ? "<s>${content}</s>" : "${content}");

            content = RegexLI.Replace(content, settings.Lists ? "<li>${content}</li>" : "${content} ");
            content = RegexOLI.Replace(content, settings.Lists ? "<oli>${content}</oli>" : "${content} ");
            content = RegexUL.Replace(content, settings.Lists ? "<ul>${content}</ul>" : "${content} ");
            content = RegexOL.Replace(content, settings.Lists ? "<ol>${content}</ol>" : "${content} ");
            content = RegexLIFin.Replace(content, "li>");

            content = RegexTitles.Replace(content, m =>
                                          settings.Titles ? ("<h" + m.Groups["begin"].Length + ">" + m.Groups["content"].ToString().Trim() + "</h" + m.Groups["end"].Length + ">") :
                                          m.Groups["content"].Value);

            //Remove multiple breakline
            if (settings.MaxTwoLineBreaks)
            {
                content = RegexMaxTwoLineBreaks.Replace(content, "\n\n");
            }

            if (settings.LineBreaks)
            {
                content = RegexNewLine.Replace(content, "<br/>");
                content = RegexCarrageReturn.Replace(content, "");
            }

            return(content);
        }
Ejemplo n.º 3
0
        static HelpWiki()
        {
            DefaultWikiSettings = new WikiSettings(true);
            DefaultWikiSettings.TokenParser += s => LinkParser(s)?.ToHtmlString();
            DefaultWikiSettings.TokenParser += ProcessImages;

            NoLinkWikiSettings = new WikiSettings(false) { LineBreaks = false };
            NoLinkWikiSettings.TokenParser += s => LinkParser(s)?.Text;
            NoLinkWikiSettings.TokenParser += RemoveImages;
        }
 private static string WriteCodeRegions(string result, Dictionary<string, string> codeRegions, WikiSettings settings)
 {
     result = Regex.Replace(result, @"%%%CODE%(?<guid>.+?)%%%", m =>
     {
         var value = codeRegions[m.Groups["guid"].Value];
         return settings.CodeRegion ?
             "<pre><code>" + (settings.EncodeHtml ? HttpUtility.HtmlEncode(value) : value) + "</code></pre>" :
             codeRegions[m.Groups["guid"].Value];
     });
     return result;
 }
Ejemplo n.º 5
0
        static string ProcessTokens(string content, WikiSettings settings)
        {
            return(TokenRegex.Replace(content, m =>
            {
                string text = m.Groups["content"].Value.Replace("[[", "[").Replace("]]", "]");

                try
                {
                    return settings.TokenParser
                    .GetInvocationListTyped()
                    .Select(a => a(text))
                    .NotNull()
                    .FirstEx();
                }
                catch
                {
                    return "<span class=\"sf-wiki-error\">{0}</span>".FormatWith(m.Value);
                }
            }));
        }
Ejemplo n.º 6
0
 public static MvcHtmlString WikiParse(this HtmlHelper helper, string text, WikiSettings settings)
 {
     return MvcHtmlString.Create(settings.WikiParse(text ?? "").Replace("\n", "<p>")); 
 }
        static string ProcessFormat(string content, WikiSettings settings)
        {
            content = RegexBoldItalics.Replace(content,(settings.Strong && settings.Em) ? "<strong><em>${content}</em></strong>" : "${content}");
            content = RegexBold.Replace(content, settings.Strong ? "<strong>${content}</strong>" : "${content}");
            content = RegexItalics.Replace(content, settings.Em ? "<em>${content}</em>" : "${content}");
            content = RegexUnderline.Replace(content, settings.Underlined ? "<u>${content}</u>" : "${content}");
            content = RegexStrike.Replace(content, settings.Strike ? "<s>${content}</s>" : "${content}");
            
            content = RegexLI.Replace(content, settings.Lists ? "<li>${content}</li>" : "${content} ");
            content = RegexOLI.Replace(content, settings.Lists ? "<oli>${content}</oli>" : "${content} ");
            content = RegexUL.Replace(content, settings.Lists ? "<ul>${content}</ul>" : "${content} ");
            content = RegexOL.Replace(content, settings.Lists ? "<ol>${content}</ol>" : "${content} ");
            content = RegexLIFin.Replace(content, "li>");

            content = RegexTitles.Replace(content, m =>  
                settings.Titles ? ("<h" + m.Groups["begin"].Length + ">" + m.Groups["content"].ToString().Trim() + "</h" + m.Groups["end"].Length + ">") :
                m.Groups["content"].Value);

            //Remove multiple breakline  
            if (settings.MaxTwoLineBreaks)
            {
                content = RegexMaxTwoLineBreaks.Replace(content, "\n\n");
            }

            if (settings.LineBreaks)
            {
                content = RegexNewLine.Replace(content, "<br/>");
                content = RegexCarrageReturn.Replace(content, "");
            }

            return content;
        }
        static string ProcessTokens(string content, WikiSettings settings)
        {
            return TokenRegex.Replace(content, m =>
            {
                string text = m.Groups["content"].Value.Replace("[[", "[").Replace("]]", "]");

                try
                {
                    return settings.TokenParser
                        .GetInvocationListTyped()
                        .Select(a => a(text))
                        .NotNull()
                        .FirstEx();
                }
                catch
                {
                    return "<span class=\"sf-wiki-error\">{0}</span>".FormatWith(m.Value);
                }
            });
        }
Ejemplo n.º 9
0
 private static string WriteCodeRegions(string result, Dictionary <string, string> codeRegions, WikiSettings settings)
 {
     result = Regex.Replace(result, @"%%%CODE%(?<guid>.+?)%%%", m =>
     {
         var value = codeRegions[m.Groups["guid"].Value];
         return(settings.CodeRegion ?
                "<pre><code>" + (settings.EncodeHtml ? HttpUtility.HtmlEncode(value) : value) + "</code></pre>" :
                codeRegions[m.Groups["guid"].Value]);
     });
     return(result);
 }