Skip to content

Commit

Permalink
feat: 移除 BlockText 中的 parser 属性。
Browse files Browse the repository at this point in the history
  • Loading branch information
CYJB committed Mar 17, 2024
1 parent 667fa8e commit 360a979
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 47 deletions.
6 changes: 3 additions & 3 deletions Cyjb.Markdown/ParseBlock/BlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public BlockParser(TextReader text, ParseOptions? options)
public Document Parse()
{
Token<BlockKind> token;
BlockText line = new(this, locator);
BlockText line = new(locator);
while (true)
{
// 清除行的旧数据。
Expand Down Expand Up @@ -254,7 +254,7 @@ private void ParseLine(BlockText line)
{
foreach (IBlockFactory factory in factories)
{
currentInlineProcessors.AddRange(factory.TryStart(line, processor));
currentInlineProcessors.AddRange(factory.TryStart(this, line, processor));
if (currentInlineProcessors.Count > 0)
{
break;
Expand All @@ -264,7 +264,7 @@ private void ParseLine(BlockText line)
// 尝试检查缩进代码块
if (currentInlineProcessors.Count == 0)
{
currentInlineProcessors.AddRange(IndentedCodeBlockProcessor.TryStart(line));
currentInlineProcessors.AddRange(IndentedCodeBlockProcessor.TryStart(this, line));
}
if (currentInlineProcessors.Count == 0)
{
Expand Down
19 changes: 2 additions & 17 deletions Cyjb.Markdown/ParseBlock/BlockText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Cyjb.Markdown.ParseBlock;

/// <summary>
/// 行的信息
/// 块的文本
/// </summary>
internal sealed class BlockText
{
Expand All @@ -23,10 +23,6 @@ internal sealed class BlockText
/// </summary>
private readonly LineLocator locator;
/// <summary>
/// 块解析器。
/// </summary>
private readonly BlockParser parser;
/// <summary>
/// 行的起始位置。
/// </summary>
private int start = -1;
Expand Down Expand Up @@ -66,11 +62,9 @@ internal sealed class BlockText
/// <summary>
/// 使用指定的行定位器初始化 <see cref="BlockText"/> 类的新实例。
/// </summary>
/// <param name="parser">块解析器。</param>
/// <param name="locator">行定位器。</param>
internal BlockText(BlockParser parser, LineLocator locator)
internal BlockText(LineLocator locator)
{
this.parser = parser;
this.locator = locator;
}

Expand Down Expand Up @@ -111,10 +105,6 @@ public int Indent
/// 获取行的结束位置。
/// </summary>
public int End => end;
/// <summary>
/// 获取解析的选项。
/// </summary>
public ParseOptions Options => parser.Options;

/// <summary>
/// 获取当前行的文本。
Expand Down Expand Up @@ -177,11 +167,6 @@ public MappedText Text
}
}

/// <summary>
/// 获取当前激活的节点处理器。
/// </summary>
public BlockProcessor ActivatedProcessor => parser.ActivatedProcessor;

/// <summary>
/// 获取行是否是空的。
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion Cyjb.Markdown/ParseBlock/Processors/ATXHeadingProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
if (line.IsCodeIndent)
{
Expand Down
3 changes: 2 additions & 1 deletion Cyjb.Markdown/ParseBlock/Processors/BlockquoteProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
int start = line.Peek().Span.Start;
if (CheckQuoteStart(line))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,17 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
if (line.IsCodeIndent)
{
yield break;
}
MarkdownUtil.ParseFenceStart(line, out int start, out int _,
MarkdownUtil.ParseFenceStart(parser, line, out int start, out int _,
out char _, out int fenceLength, out string? info, out HtmlAttributeList? attrs);
yield return new CustomContainerProcessor(start, fenceLength, info, attrs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
if (line.IsCodeIndent)
{
yield break;
}
MarkdownUtil.ParseFenceStart(line, out int start, out int indent,
MarkdownUtil.ParseFenceStart(parser, line, out int start, out int indent,
out char fenceChar, out int fenceLength, out string? info, out HtmlAttributeList? attrs);
yield return new FencedCodeBlockProcessor(start, fenceChar, fenceLength, indent, info, attrs);
}
Expand Down
3 changes: 2 additions & 1 deletion Cyjb.Markdown/ParseBlock/Processors/FootnoteProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
if (line.IsCodeIndent)
{
Expand Down
7 changes: 4 additions & 3 deletions Cyjb.Markdown/ParseBlock/Processors/HtmlBlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,19 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
if (line.IsCodeIndent)
{
yield break;
}
HtmlInfo info = (HtmlInfo)line.Peek().Value!;
if (!info.CanInterruptParagraph && (line.ActivatedProcessor.Kind == MarkdownKind.Paragraph ||
line.ActivatedProcessor.CanLazyContinuation))
if (!info.CanInterruptParagraph && (parser.ActivatedProcessor.Kind == MarkdownKind.Paragraph ||
parser.ActivatedProcessor.CanLazyContinuation))
{
// 不中断段落。
yield break;
Expand Down
3 changes: 2 additions & 1 deletion Cyjb.Markdown/ParseBlock/Processors/IBlockFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ internal interface IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor);
IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ internal class IndentedCodeBlockProcessor : BlockProcessor
/// <summary>
/// 尝试开始新的缩进代码块处理器。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <returns>新的块处理器数组,若未能成功解析,则返回空数组。</returns>
public static IEnumerable<BlockProcessor> TryStart(BlockText line)
public static IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line)
{
// 缩进代码块不会中断段落。
if (line.IsCodeIndent && !line.IsBlank() &&
line.ActivatedProcessor.Kind != MarkdownKind.Paragraph)
parser.ActivatedProcessor.Kind != MarkdownKind.Paragraph)
{
// 代码块的起始位置包含缩进位置。
int start = line.Start;
Expand Down
4 changes: 2 additions & 2 deletions Cyjb.Markdown/ParseBlock/Processors/ListItemProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ public override BlockContinue TryContinue(BlockText line)
{
if (line.IsBlank())
{
if (item.Children.Count == 0 && line.ActivatedProcessor.Kind == MarkdownKind.ListItem)
if (item.Children.Count == 0 && parent.Parser.ActivatedProcessor.Kind == MarkdownKind.ListItem)
{
// 列表项最多可以包含一个起始空行,因此空列表项后的空行会闭合当前项。
return BlockContinue.None;
}
else
{
// 记录已包含空行。
MarkdownKind kind = line.ActivatedProcessor.Kind;
MarkdownKind kind = parent.Parser.ActivatedProcessor.Kind;
hadBlankLine = kind == MarkdownKind.Paragraph || kind == MarkdownKind.ListItem;
line.SkipIndent();
return BlockContinue.Continue;
Expand Down
16 changes: 12 additions & 4 deletions Cyjb.Markdown/ParseBlock/Processors/ListProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ internal sealed class ListProcessor : BlockProcessor
/// <summary>
/// 使用列表的标记字符和节点初始化 <see cref="ListProcessor"/> 类的新实例。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="marker">列表的标记字符。</param>
/// <param name="list">列表节点。</param>
private ListProcessor(char marker, List list) : base(MarkdownKind.List)
private ListProcessor(BlockParser parser, char marker, List list) : base(MarkdownKind.List)
{
Parser = parser;
this.marker = marker;
this.list = list;
}

/// <summary>
/// 获取关联到的块级语法分析器。
/// </summary>
public BlockParser Parser { get; }

/// <summary>
/// 获取是否是容器节点。
/// </summary>
Expand Down Expand Up @@ -172,10 +179,11 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
if (line.IsCodeIndent)
{
Expand Down Expand Up @@ -227,12 +235,12 @@ public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor match
{
Start = start
};
listProcessor = new ListProcessor(token.Text[^1], list);
listProcessor = new ListProcessor(parser, token.Text[^1], list);
yield return listProcessor;
}
ListItemProcessor itemProcessor = new(listProcessor, itemStart, contentIndent);
// 检查任务列表项。
if (line.Options.UseTaskListItem)
if (parser.Options.UseTaskListItem)
{
itemProcessor.Checked = CheckTaskListItem(line);
}
Expand Down
5 changes: 3 additions & 2 deletions Cyjb.Markdown/ParseBlock/Processors/MathBlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,17 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
if (line.IsCodeIndent)
{
yield break;
}
MarkdownUtil.ParseFenceStart(line, out int start, out int indent,
MarkdownUtil.ParseFenceStart(parser, line, out int start, out int indent,
out char _, out int fenceLength, out string? info, out HtmlAttributeList? attrs);
yield return new MathBlockProcessor(start, fenceLength, indent, info, attrs);
}
Expand Down
5 changes: 3 additions & 2 deletions Cyjb.Markdown/ParseBlock/Processors/SetextHeadingProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
// 要求 Setext 标签之前是段落,而且包含有效内容。
IList<MappedText>? lines;
Expand All @@ -110,7 +111,7 @@ public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor match
lines[^1].TrimEnd();
HtmlAttributeList? attrs = null;
// 尝试解析属性。
if (line.Options.UseHeaderAttributes)
if (parser.Options.UseHeaderAttributes)
{
attrs = ParseAttributes(lines);
// 移除尾行后的空白。
Expand Down
3 changes: 2 additions & 1 deletion Cyjb.Markdown/ParseBlock/Processors/TableProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,11 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回解析器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
// 要求分割行之前是段落,而且包含且只包含一行。
IList<MappedText>? lines;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ private sealed class BlockFactory : IBlockFactory
/// <summary>
/// 尝试开始当前块的解析。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要检查的行。</param>
/// <param name="matchedProcessor">当前匹配到的块处理器。</param>
/// <returns>如果能够开始当前块的解析,则返回处理器序列。否则返回空序列。</returns>
public IEnumerable<BlockProcessor> TryStart(BlockText line, BlockProcessor matchedProcessor)
public IEnumerable<BlockProcessor> TryStart(BlockParser parser, BlockText line, BlockProcessor matchedProcessor)
{
if (line.IsCodeIndent)
{
Expand Down
5 changes: 3 additions & 2 deletions Cyjb.Markdown/Utils/MarkdownUtil.Fence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ public static int GetFenceLength(ReadOnlySpan<char> text)
/// <summary>
/// 解析分隔符的起始。
/// </summary>
/// <param name="parser">块级语法分析器。</param>
/// <param name="line">要解析的行信息。</param>
/// <param name="start">分隔符的起始索引。</param>
/// <param name="indent">分隔符的缩进。</param>
/// <param name="fenceChar">分隔的字符。</param>
/// <param name="fenceLength">分隔符的长度。</param>
/// <param name="info">分隔符的信息。</param>
/// <param name="attrs">分隔符的属性。</param>
public static void ParseFenceStart(BlockText line, out int start, out int indent,
public static void ParseFenceStart(BlockParser parser, BlockText line, out int start, out int indent,
out char fenceChar, out int fenceLength,
out string? info, out HtmlAttributeList? attrs)
{
Expand All @@ -59,7 +60,7 @@ public static void ParseFenceStart(BlockText line, out int start, out int indent
info = null;
}
attrs = token.Value as HtmlAttributeList;
attrs?.AddPrefix(line.Options.AttributesPrefix);
attrs?.AddPrefix(parser.Options.AttributesPrefix);
}
else
{
Expand Down

0 comments on commit 360a979

Please sign in to comment.