public string Execute(PageParser parser, string currentUrlPath, string baseUrlPath, string text) { foreach (var filter in _filters) { var context = new PreFilterContext(parser) { TextToParse = text, CurrentUrlPath = currentUrlPath, RootUrlPath = baseUrlPath, }; text = filter.Parse(context); } return text; }
/// <summary> /// Parse text /// </summary> /// <param name="filterContext"></param> /// <returns>Text with the modifications done by this script</returns> public string Parse(PreFilterContext filterContext) { var text = filterContext.TextToParse; var sb = new StringBuilder(); var lastPos = 0; while (true) { var pos = text.IndexOf("\r\n```", lastPos); if (pos == -1) break; sb.Append(text.Substring(lastPos, pos - lastPos)); sb.AppendLine(); pos += 5; var nlPos = text.IndexOfAny(new[] { '\r', '\n' }, pos); var codeLang = text.Substring(pos, nlPos - pos); lastPos = text.IndexOf("\r\n```\r\n", pos + 1); if (lastPos == -1) lastPos = text.Length - 1; sb.AppendFormat(@"<pre><code data-lang=""{0}"" class=""language-{0}"">", codeLang); var code = text.Substring(nlPos + 2, lastPos - nlPos - 2); code = ProcessCode(code); sb.Append(code); sb.Append("</code></pre>\r\n"); lastPos += 5; if (lastPos > text.Length) break; } if (lastPos < text.Length - 1) sb.AppendLine(text.Substring(lastPos)); return sb.ToString(); }
/// <summary> /// Parse text /// </summary> /// <param name="filterContext"></param> /// <returns>Text with the modifications done by this script</returns> public string Parse(PreFilterContext filterContext) { var reader = new StringReader(filterContext.TextToParse); var line = ""; var sb = new StringBuilder(); while ((line = reader.ReadLine()) != null) { if (!line.Contains(" | ")) { sb.AppendLine(line); continue; } var firstLine = line; line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) { sb.AppendLine(firstLine); continue; } if (!line.All(x => x == ' ' || x == '|' || x == '-')) { sb.AppendLine(line); continue; } //got a table. sb.AppendLine(@"<table class=""table table-striped table-bordered""><thead><tr><th>"); sb.AppendLine(firstLine.Replace(" | ", "</th><th>")); sb.AppendLine("</th></tr></thead><tbody>"); while (true) { line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) { sb.AppendLine("</tbody></table>"); sb.AppendLine("<br>\r\n"); break; } line = line.Trim('|'); sb.Append("<tr>"); var oldPos = 0; while (true) { var pos = line.IndexOf('|', oldPos); if (pos == -1) { pos = line.Length; } var cell = line.Substring(oldPos, pos - oldPos); sb.Append("<td> "); var data = filterContext.Parse(cell).Replace("<p>", "").Replace("</p>", ""); sb.Append(data); sb.Append(" </td>"); oldPos = pos + 1; if (oldPos >= line.Length) { sb.AppendLine("</tr>"); break; } } } } return sb.ToString(); }
/// <summary> /// Parse text /// </summary> /// <param name="filterContext"></param> /// <returns>Text with the modifications done by this script</returns> public string Parse(PreFilterContext filterContext) { var regex = @"(\\\\[A-Z_a-z0-9$\\.\-]+)[\s|\n]"; var r = new Regex(regex, RegexOptions.IgnoreCase); return r.Replace(filterContext.TextToParse, "<a href=\"file://$1\" target=\"_blank\">\\$1</a>"); }