public static string Format(string raw) { StringBuilder sb = new StringBuilder(raw); sb.Replace("\r", ""); if (!sb.ToString().EndsWith("\n")) { sb.Append("\n"); // Very important to make Regular Expressions work! } NoWiki.Compute(sb.ToString()); Format_h(h4, "h4", 5, ref sb); Format_h(h3, "h3", 4, ref sb); Format_h(h2, "h2", 3, ref sb); Format_h(h1, "h1", 2, ref sb); return(sb.ToString()); }
private static void Format_h(Regex hRegex, string tag, int len, ref StringBuilder sb) { string h; int end = 0; Match match = hRegex.Match(sb.ToString()); while (match.Success) { if (!NoWiki.IsNoWikied(match.Index, out end)) { match = hRegex.Match(sb.ToString(), end); sb.Remove(match.Index, match.Length); h = match.Value.Substring(len, match.Value.Length - (len * 2) - (match.Value.EndsWith("\n") ? 1 : 0)); sb.Insert(match.Index, $"<{tag}>{h}</{tag}>"); } NoWiki.Compute(sb.ToString()); match = hRegex.Match(sb.ToString(), end); } }