2022-02-17 09:08:01 +00:00
|
|
|
namespace Textile.States;
|
|
|
|
|
|
|
|
[FormatterState(@"^\s*<code" + Globals.HtmlAttributesPattern + ">")]
|
|
|
|
public class CodeFormatterState : FormatterState
|
2019-08-19 12:35:39 +00:00
|
|
|
{
|
2022-02-17 09:08:01 +00:00
|
|
|
private bool _shouldExitNextTime = false;
|
|
|
|
private bool _shouldFixHtmlEntities = false;
|
|
|
|
|
|
|
|
public CodeFormatterState(TextileFormatter f)
|
|
|
|
: base(f)
|
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
|
|
|
public override string Consume(string input, Match m)
|
|
|
|
{
|
|
|
|
if (!Regex.IsMatch(input, "</code>"))
|
2019-08-19 12:35:39 +00:00
|
|
|
{
|
2022-02-17 09:08:01 +00:00
|
|
|
this.Formatter.ChangeState(this);
|
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
|
|
|
this.Formatter.ChangeState(new PassthroughFormatterState(this.Formatter));
|
2019-08-19 12:35:39 +00:00
|
|
|
}
|
2022-02-17 09:08:01 +00:00
|
|
|
return input;
|
|
|
|
}
|
2019-08-19 12:35:39 +00:00
|
|
|
|
2022-02-17 09:08:01 +00:00
|
|
|
public override bool ShouldNestState(FormatterState other)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-19 12:35:39 +00:00
|
|
|
|
2022-02-17 09:08:01 +00:00
|
|
|
public override void Enter()
|
|
|
|
{
|
|
|
|
_shouldFixHtmlEntities = false;
|
|
|
|
}
|
2019-08-19 12:35:39 +00:00
|
|
|
|
2022-02-17 09:08:01 +00:00
|
|
|
public override void Exit()
|
|
|
|
{
|
|
|
|
}
|
2019-08-19 12:35:39 +00:00
|
|
|
|
2022-02-17 09:08:01 +00:00
|
|
|
public override void FormatLine(string input)
|
|
|
|
{
|
|
|
|
if (_shouldFixHtmlEntities)
|
|
|
|
input = FixEntities(input);
|
|
|
|
Formatter.Output.WriteLine(input);
|
|
|
|
_shouldFixHtmlEntities = true;
|
|
|
|
}
|
2019-08-19 12:35:39 +00:00
|
|
|
|
2022-02-17 09:08:01 +00:00
|
|
|
public override bool ShouldExit(string input)
|
|
|
|
{
|
|
|
|
if (_shouldExitNextTime)
|
|
|
|
return true;
|
|
|
|
_shouldExitNextTime = Regex.IsMatch(input, @"</code>");
|
|
|
|
_shouldFixHtmlEntities = !_shouldExitNextTime;
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-19 12:35:39 +00:00
|
|
|
|
2022-02-17 09:08:01 +00:00
|
|
|
public override bool ShouldFormatBlocks(string input)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-19 12:35:39 +00:00
|
|
|
|
2022-02-17 09:08:01 +00:00
|
|
|
public override bool ShouldParseForNewFormatterState(string input)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-19 12:35:39 +00:00
|
|
|
|
2022-02-17 09:08:01 +00:00
|
|
|
private string FixEntities(string text)
|
|
|
|
{
|
|
|
|
// de-entify any remaining angle brackets or ampersands
|
|
|
|
text = text.Replace("&", "&");
|
|
|
|
text = text.Replace(">", ">");
|
|
|
|
text = text.Replace("<", "<");
|
|
|
|
//Regex.Replace(text, @"\b&([#a-z0-9]+;)", "x%x%");
|
|
|
|
return text;
|
2019-08-19 12:35:39 +00:00
|
|
|
}
|
2022-02-17 09:08:01 +00:00
|
|
|
}
|