2019-08-19 12:35:39 +00:00
|
|
|
#region License Statement
|
|
|
|
// Copyright (c) L.A.B.Soft. All rights reserved.
|
|
|
|
//
|
|
|
|
// The use and distribution terms for this software are covered by the
|
|
|
|
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
|
|
|
|
// which can be found in the file CPL.TXT at the root of this distribution.
|
|
|
|
// By using this software in any fashion, you are agreeing to be bound by
|
|
|
|
// the terms of this license.
|
|
|
|
//
|
|
|
|
// You must not remove this notice, or any other, from this software.
|
|
|
|
#endregion
|
|
|
|
|
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—$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
|
|
|
|
};
|
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, @"<(\/?" + offtags + ")>", "<$1>");
|
|
|
|
//line = line.Replace("&#", "&#");
|
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
|
|
|
}
|
|
|
|
}
|