DocSpace-client/common/ASC.Textile/Blocks/GlyphBlockModifier.cs

76 lines
3.4 KiB
C#
Raw Normal View History

2022-02-17 09:08:01 +00:00
namespace Textile.Blocks;
public class GlyphBlockModifier : BlockModifier
2019-08-19 12:35:39 +00:00
{
2022-02-17 09:08:01 +00:00
public override string ModifyLine(string line)
2019-08-19 12:35:39 +00:00
{
2022-02-17 09:08:01 +00:00
line = Regex.Replace(line, "\"\\z", "\" ");
2019-08-19 12:35:39 +00:00
2022-02-17 09:08:01 +00:00
// 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(?:[(]([^)]*)[)])", "<acronym title=\"$2\">$1</acronym>" }, // 3+ uppercase acronym
{ @"(\s)?--(\s)?", "$1&#8212;$2" }, // em dash
{ @"\s-\s", " &#8211; " }, // en dash
{ @"(\d+)( )?x( )?(\d+)", "$1$2&#215;$3$4" }, // dimension sign
{ @"\b ?[([](TM|tm)[])]", "&#8482;" }, // trademark
{ @"\b ?[([](R|r)[])]", "&#174;" }, // registered
{ @"\b ?[([](C|c)[])]", "&#169;" } // copyright
};
2019-08-19 12:35:39 +00:00
2022-02-17 09:08:01 +00:00
var sb = new StringBuilder();
2019-08-19 12:35:39 +00:00
2022-02-17 09:08:01 +00:00
if (!Regex.IsMatch(line, "<.*>"))
{
// If no HTML, do a simple search & replace.
for (var i = 0; i < glyphs.GetLength(0); ++i)
2019-08-19 12:35:39 +00:00
{
2022-02-17 09:08:01 +00:00
line = Regex.Replace(line, glyphs[i, 0], glyphs[i, 1]);
2019-08-19 12:35:39 +00:00
}
2022-02-17 09:08:01 +00:00
sb.Append(line);
}
else
{
var splits = Regex.Split(line, "(<.*?>)");
var offtags = "code|pre|notextile";
var codepre = false;
2022-01-21 13:12:05 +00:00
2022-02-17 09:08:01 +00:00
foreach (var split in splits)
{
var modifiedSplit = split;
if (modifiedSplit.Length == 0)
continue;
2019-08-19 12:35:39 +00:00
2022-02-17 09:08:01 +00:00
if (Regex.IsMatch(modifiedSplit, @"<(" + offtags + ")>"))
codepre = true;
if (Regex.IsMatch(modifiedSplit, @"<\/(" + offtags + ")>"))
codepre = false;
2019-08-19 12:35:39 +00:00
2022-02-17 09:08:01 +00:00
if (!Regex.IsMatch(modifiedSplit, "<.*>") && !codepre)
{
for (var i = 0; i < glyphs.GetLength(0); ++i)
2019-08-19 12:35:39 +00:00
{
2022-02-17 09:08:01 +00:00
modifiedSplit = Regex.Replace(modifiedSplit, glyphs[i, 0], glyphs[i, 1]);
2019-08-19 12:35:39 +00:00
}
2022-02-17 09:08:01 +00:00
}
2019-08-19 12:35:39 +00:00
2022-02-17 09:08:01 +00:00
// do htmlspecial if between <code>
if (codepre)
{
//TODO: htmlspecialchars(line)
//line = Regex.Replace(line, @"&lt;(\/?" + offtags + ")&gt;", "<$1>");
//line = line.Replace("&amp;#", "&#");
2019-08-19 12:35:39 +00:00
}
2022-02-17 09:08:01 +00:00
sb.Append(modifiedSplit);
}
2019-08-19 12:35:39 +00:00
}
2022-02-17 09:08:01 +00:00
return sb.ToString();
2019-08-19 12:35:39 +00:00
}
}