DocSpace-buildtools/common/ASC.Textile/Blocks/ImageBlockModifier.cs

57 lines
2.3 KiB
C#
Raw Normal View History

2022-02-17 09:08:01 +00:00
namespace Textile.Blocks;
public class ImageBlockModifier : BlockModifier
2019-08-19 12:35:39 +00:00
{
2022-02-17 09:08:01 +00:00
public override string ModifyLine(string line)
{
line = Regex.Replace(line,
@"\!" + // opening !
@"(?<algn>\<|\=|\>)?" + // optional alignment atts
Globals.BlockModifiersPattern + // optional style, public class atts
@"(?:\. )?" + // optional dot-space
@"(?<url>[^\s(!]+)" + // presume this is the src
@"\s?" + // optional space
@"(?:\((?<title>([^\)]+))\))?" +// optional title
@"\!" + // closing
@"(?::(?<href>(\S+)))?" + // optional href
@"(?=\s|\.|,|;|\)|\||$)", // lookahead: space or simple punctuation or end of string
new MatchEvaluator(ImageFormatMatchEvaluator)
);
return line;
}
string ImageFormatMatchEvaluator(Match m)
2019-08-19 12:35:39 +00:00
{
2022-02-17 09:08:01 +00:00
var atts = BlockAttributesParser.ParseBlockAttributes(m.Groups["atts"].Value, "img");
if (m.Groups["algn"].Length > 0)
atts += " align=\"" + Globals.ImageAlign[m.Groups["algn"].Value] + "\"";
if (m.Groups["title"].Length > 0)
2019-08-19 12:35:39 +00:00
{
2022-02-17 09:08:01 +00:00
atts += " title=\"" + m.Groups["title"].Value + "\"";
atts += " alt=\"" + m.Groups["title"].Value + "\"";
2019-08-19 12:35:39 +00:00
}
2022-02-17 09:08:01 +00:00
else
2019-08-19 12:35:39 +00:00
{
2022-02-17 09:08:01 +00:00
atts += " alt=\"\"";
}
// Get Image Size?
2019-08-19 12:35:39 +00:00
2022-02-17 09:08:01 +00:00
var res = "<img src=\"" + m.Groups["url"].Value + "\"" + atts + " />";
2019-08-19 12:35:39 +00:00
2022-02-17 09:08:01 +00:00
if (m.Groups["href"].Length > 0)
{
var href = m.Groups["href"].Value;
var end = string.Empty;
var endMatch = Regex.Match(href, @"(.*)(?<end>\.|,|;|\))$");
if (m.Success && !string.IsNullOrEmpty(endMatch.Groups["end"].Value))
2019-08-19 12:35:39 +00:00
{
2022-02-17 09:08:01 +00:00
href = href[0..^1];
end = endMatch.Groups["end"].Value;
2019-08-19 12:35:39 +00:00
}
2022-02-17 09:08:01 +00:00
res = "<a href=\"" + Globals.EncodeHTMLLink(href) + "\">" + res + "</a>" + end;
2019-08-19 12:35:39 +00:00
}
2022-02-17 09:08:01 +00:00
return res;
2019-08-19 12:35:39 +00:00
}
}