public void ResolveEmphasisMarks(List<Token> tokens, List<Token> marks)
{
bool bContinue = true;
while (bContinue)
{
bContinue = false;
for (int i = 0; i < marks.Count; i++)
{
// Get the next opening or internal mark
Token opening_mark = marks[i];
if (opening_mark.type != TokenType.opening_mark && opening_mark.type != TokenType.internal_mark)
continue;
// Look for a matching closing mark
for (int j = i + 1; j < marks.Count; j++)
{
// Get the next closing or internal mark
Token closing_mark = marks[j];
if (closing_mark.type != TokenType.closing_mark && closing_mark.type != TokenType.internal_mark)
break;
// Ignore if different type (ie: `*` vs `_`)
if (Input[opening_mark.startOffset] != Input[closing_mark.startOffset])
continue;
// strong or em?
int style = Math.Min(opening_mark.length, closing_mark.length);
// Triple or more on both ends?
if (style >= 3)
{
style = (style % 2)==1 ? 1 : 2;
}
// Split the opening mark, keeping the RHS
if (opening_mark.length > style)
{
opening_mark = SplitMarkToken(tokens, marks, opening_mark, opening_mark.length - style);
i--;
}
// Split the closing mark, keeping the LHS
if (closing_mark.length > style)
{
SplitMarkToken(tokens, marks, closing_mark, style);
}
// Connect them
opening_mark.type = style == 1 ? TokenType.open_em : TokenType.open_strong;
closing_mark.type = style == 1 ? TokenType.close_em : TokenType.close_strong;
// Remove the matched marks
marks.Remove(opening_mark);
marks.Remove(closing_mark);
bContinue = true;
break;
}
}
}
}