namespace Textile.Blocks; public class GlyphBlockModifier : BlockModifier { public override string ModifyLine(string line) { line = Regex.Replace(line, "\"\\z", "\" "); // fix: hackish string[,] glyphs = { { @"([^\s[{(>_*])?\'(?(1)|(\s|s\b|" + Globals.PunctuationPattern + @"))", "$1’$2" }, // single closing { @"\'", "‘" }, // single opening { @"([^\s[{(>_*])?""(?(1)|(\s|" + Globals.PunctuationPattern + @"))", "$1”$2" }, // double closing { @"""", "“" }, // double opening { @"\b( )?\.{3}", "$1…" }, // ellipsis { @"\b([A-Z][A-Z0-9]{2,})\b(?:[(]([^)]*)[)])", "$1" }, // 3+ uppercase acronym { @"(\s)?--(\s)?", "$1—$2" }, // em dash { @"\s-\s", " – " }, // en dash { @"(\d+)( )?x( )?(\d+)", "$1$2×$3$4" }, // dimension sign { @"\b ?[([](TM|tm)[])]", "™" }, // trademark { @"\b ?[([](R|r)[])]", "®" }, // registered { @"\b ?[([](C|c)[])]", "©" } // copyright }; var sb = new StringBuilder(); if (!Regex.IsMatch(line, "<.*>")) { // If no HTML, do a simple search & replace. for (var i = 0; i < glyphs.GetLength(0); ++i) { line = Regex.Replace(line, glyphs[i, 0], glyphs[i, 1]); } sb.Append(line); } else { var splits = Regex.Split(line, "(<.*?>)"); var offtags = "code|pre|notextile"; var codepre = false; foreach (var split in splits) { var modifiedSplit = split; if (modifiedSplit.Length == 0) continue; if (Regex.IsMatch(modifiedSplit, @"<(" + offtags + ")>")) codepre = true; if (Regex.IsMatch(modifiedSplit, @"<\/(" + offtags + ")>")) codepre = false; if (!Regex.IsMatch(modifiedSplit, "<.*>") && !codepre) { for (var i = 0; i < glyphs.GetLength(0); ++i) { modifiedSplit = Regex.Replace(modifiedSplit, glyphs[i, 0], glyphs[i, 1]); } } // do htmlspecial if between if (codepre) { //TODO: htmlspecialchars(line) //line = Regex.Replace(line, @"<(\/?" + offtags + ")>", "<$1>"); //line = line.Replace("&#", "&#"); } sb.Append(modifiedSplit); } } return sb.ToString(); } }