本文整理汇总了C#中CommonMark.Parser.Subject类的典型用法代码示例。如果您正苦于以下问题:C# Subject类的具体用法?C# Subject怎么用?C# Subject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Subject类属于CommonMark.Parser命名空间,在下文中一共展示了Subject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: handle_newline
// Parse a hard or soft linebreak, returning an inline.
// Assumes the subject has a newline at the current position.
static Syntax.Inline handle_newline(Subject subj)
{
int nlpos = subj.Position;
// skip over newline
advance(subj);
// skip spaces at beginning of line
while (peek_char(subj) == ' ')
{
advance(subj);
}
if (nlpos > 1 &&
BString.bchar(subj.Buffer, nlpos - 1) == ' ' &&
BString.bchar(subj.Buffer, nlpos - 2) == ' ')
{
return make_linebreak();
}
else
{
return make_softbreak();
}
}
示例2: handle_pointy_brace
// Parse an autolink or HTML tag.
// Assumes the subject has a '<' character at the current position.
static Inline handle_pointy_brace(Subject subj)
{
int matchlen;
string contents;
// advance past first <
subj.Position++;
// first try to match a URL autolink
matchlen = Scanner.scan_autolink_uri(subj.Buffer, subj.Position, subj.Length);
if (matchlen > 0)
{
contents = subj.Buffer.Substring(subj.Position, matchlen - 1);
var resultContents = ParseStringEntities(contents);
var result = Inline.CreateLink(resultContents, contents, string.Empty);
result.SourcePosition = subj.Position - 1;
resultContents.SourcePosition = subj.Position;
subj.Position += matchlen;
result.SourceLastPosition = subj.Position;
resultContents.SourceLastPosition = subj.Position - 1;
return result;
}
// next try to match an email autolink
matchlen = Scanner.scan_autolink_email(subj.Buffer, subj.Position, subj.Length);
if (matchlen > 0)
{
contents = subj.Buffer.Substring(subj.Position, matchlen - 1);
var resultContents = ParseStringEntities(contents);
var result = Inline.CreateLink(resultContents, "mailto:" + contents, string.Empty);
result.SourcePosition = subj.Position - 1;
resultContents.SourcePosition = subj.Position;
subj.Position += matchlen;
result.SourceLastPosition = subj.Position;
resultContents.SourceLastPosition = subj.Position - 1;
return result;
}
// finally, try to match an html tag
matchlen = Scanner.scan_html_tag(subj.Buffer, subj.Position, subj.Length);
if (matchlen > 0)
{
var result = new Inline(InlineTag.RawHtml, subj.Buffer, subj.Position - 1, matchlen + 1);
result.SourcePosition = subj.Position - 1;
subj.Position += matchlen;
result.SourceLastPosition = subj.Position;
return result;
}
else
{
// if nothing matches, just return the opening <:
return new Inline("<", subj.Position - 1, subj.Position);
}
}
示例3: Finalize
public static void Finalize(Block b, LineInfo line)
{
// don't do anything if the block is already closed
if (!b.IsOpen)
return;
b.IsOpen = false;
if (line.IsTrackingPositions)
{
// (b.SourcePosition >= line.LineOffset) determines if the block started on this line.
if (b.SourcePosition >= line.LineOffset && line.Line != null)
b.SourceLastPosition = line.CalculateOrigin(line.Line.Length, false);
else
b.SourceLastPosition = line.CalculateOrigin(0, false);
}
#pragma warning disable 0618
b.EndLine = (line.LineNumber > b.StartLine) ? line.LineNumber - 1 : line.LineNumber;
#pragma warning restore 0618
switch (b.Tag)
{
case BlockTag.Paragraph:
var sc = b.StringContent;
if (!sc.StartsWith('['))
break;
var subj = new Subject(b.Top.ReferenceMap);
sc.FillSubject(subj);
var origPos = subj.Position;
while (subj.Position < subj.Buffer.Length
&& subj.Buffer[subj.Position] == '['
&& 0 != InlineMethods.ParseReference(subj))
{
}
if (subj.Position != origPos)
{
sc.Replace(subj.Buffer, subj.Position, subj.Buffer.Length - subj.Position);
if (sc.PositionTracker != null)
sc.PositionTracker.AddBlockOffset(subj.Position - origPos);
if (Utilities.IsFirstLineBlank(subj.Buffer, subj.Position))
b.Tag = BlockTag.ReferenceDefinition;
}
break;
case BlockTag.IndentedCode:
b.StringContent.RemoveTrailingBlankLines();
break;
case BlockTag.FencedCode:
// first line of contents becomes info
var firstlinelen = b.StringContent.IndexOf('\n') + 1;
b.FencedCodeData.Info = InlineMethods.Unescape(b.StringContent.TakeFromStart(firstlinelen, true).Trim());
break;
case BlockTag.List: // determine tight/loose status
b.ListData.IsTight = true; // tight by default
var item = b.FirstChild;
Block subitem;
while (item != null)
{
// check for non-final non-empty list item ending with blank line:
if (item.IsLastLineBlank && item.NextSibling != null)
{
b.ListData.IsTight = false;
break;
}
// recurse into children of list item, to see if there are spaces between them:
subitem = item.FirstChild;
while (subitem != null)
{
if (EndsWithBlankLine(subitem) && (item.NextSibling != null || subitem.NextSibling != null))
{
b.ListData.IsTight = false;
break;
}
subitem = subitem.NextSibling;
}
if (!b.ListData.IsTight)
break;
item = item.NextSibling;
}
break;
}
}
示例4: handle_backslash
// Parse backslash-escape or just a backslash, returning an inline.
private static Inline handle_backslash(Subject subj)
{
subj.Position++;
if (subj.Position >= subj.Length)
return new Inline("\\", subj.Position - 1, subj.Position);
var nextChar = subj.Buffer[subj.Position];
if (Utilities.IsEscapableSymbol(nextChar))
{
// only ascii symbols and newline can be escaped
// the exception is the unicode bullet char since it can be used for defining list items
subj.Position++;
return new Inline(nextChar.ToString(), subj.Position - 2, subj.Position);
}
else if (nextChar == '\n')
{
subj.Position++;
return new Inline(InlineTag.LineBreak)
{
SourcePosition = subj.Position - 2,
SourceLastPosition = subj.Position
};
}
else
{
return new Inline("\\", subj.Position - 1, subj.Position);
}
}
示例5: ParseEntity
/// <summary>
/// Parses the entity at the current position.
/// Assumes that there is a <c>&</c> at the current position.
/// </summary>
private static string ParseEntity(Subject subj)
{
int match;
string entity;
int numericEntity;
var origPos = subj.Position;
match = Scanner.scan_entity(subj.Buffer, subj.Position, subj.Length - subj.Position, out entity, out numericEntity);
if (match > 0)
{
subj.Position += match;
if (entity != null)
{
entity = EntityDecoder.DecodeEntity(entity);
if (entity != null)
return entity;
return subj.Buffer.Substring(origPos, match);
}
else if (numericEntity > 0)
{
entity = EntityDecoder.DecodeEntity(numericEntity);
if (entity != null)
return entity;
}
return "\uFFFD";
}
else
{
subj.Position++;
return "&";
}
}
示例6: HandleLeftSquareBracket
private static Inline HandleLeftSquareBracket(Subject subj, CommonMarkSettings settings)
{
return HandleLeftSquareBracket(subj, false, settings);
}
示例7: MatchSquareBracketStack
internal static void MatchSquareBracketStack(InlineStack opener, Subject subj, Reference details, InlineParserParameters parameters)
{
if (details != null)
{
var inl = opener.StartingInline;
var isImage = 0 != (opener.Flags & InlineStack.InlineStackFlags.ImageLink);
inl.Tag = isImage ? InlineTag.Image : InlineTag.Link;
inl.FirstChild = inl.NextSibling;
inl.NextSibling = null;
inl.SourceLastPosition = subj.Position;
inl.TargetUrl = details.Url;
inl.LiteralContent = details.Title;
if (!isImage)
{
// since there cannot be nested links, remove any other link openers before this
var temp = opener.Previous;
while (temp != null && temp.Priority <= InlineStack.InlineStackPriority.Links)
{
if (temp.Delimeter == '[' && temp.Flags == opener.Flags)
{
// mark the previous entries as "inactive"
if (temp.DelimeterCount == -1)
break;
temp.DelimeterCount = -1;
}
temp = temp.Previous;
}
}
InlineStack.RemoveStackEntry(opener, subj, null, parameters);
subj.LastInline = inl;
}
else
{
// this looked like a link, but was not.
// remove the opener stack entry but leave the inbetween intact
InlineStack.RemoveStackEntry(opener, subj, opener, parameters);
var inl = new Inline("]", subj.Position - 1, subj.Position);
subj.LastInline.LastSibling.NextSibling = inl;
subj.LastInline = inl;
}
}
示例8: parse_inlines
public static Inline parse_inlines(Subject subj, Dictionary<string, Reference> refmap, InlineParserParameters parameters)
{
var len = subj.Length;
if (len == 0)
return null;
var first = ParseInline(subj, parameters);
subj.LastInline = first.LastSibling;
Inline cur;
while (subj.Position < len)
{
cur = ParseInline(subj, parameters);
if (cur != null)
{
subj.LastInline.NextSibling = cur;
subj.LastInline = cur.LastSibling;
}
}
InlineStack.PostProcessInlineStack(subj, subj.FirstPendingInline, subj.LastPendingInline, InlineStack.InlineStackPriority.Maximum, parameters);
return first;
}
示例9: spnl
// Parse zero or more space characters, including at most one newline.
private static void spnl(Subject subj)
{
var seenNewline = false;
var len = subj.Length;
while (subj.Position < len)
{
var c = subj.Buffer[subj.Position];
if (c == ' ' || (!seenNewline && (seenNewline = c == '\n')))
subj.Position++;
else
return;
}
}
示例10: handle_newline
// Parse a hard or soft linebreak, returning an inline.
// Assumes the subject has a newline at the current position.
static Inline handle_newline(Subject subj)
{
int nlpos = subj.Position;
// skip over newline
subj.Position++;
// skip spaces at beginning of line
var len = subj.Length;
while (subj.Position < len && subj.Buffer[subj.Position] == ' ')
subj.Position++;
if (nlpos > 1 && subj.Buffer[nlpos - 1] == ' ' && subj.Buffer[nlpos - 2] == ' ')
return new Inline(InlineTag.LineBreak) { SourcePosition = nlpos - 2, SourceLastPosition = nlpos + 1 };
else
return new Inline(InlineTag.SoftBreak) { SourcePosition = nlpos, SourceLastPosition = nlpos + 1 };
}
示例11: ParseInline
/// <summary>
/// Parse an inline element from the subject. The subject position is updated to after the element.
/// </summary>
public static Inline ParseInline(Subject subj, InlineParserParameters parameters)
{
var parsers = parameters.Parsers;
var specialCharacters = parameters.SpecialCharacters;
var c = subj.Buffer[subj.Position];
var parser = c < parsers.Length ? parsers[c] : null;
if (parser != null)
return parser(subj);
var startpos = subj.Position;
// we read until we hit a special character
// +1 is so that any special character at the current position is ignored.
var endpos = subj.Buffer.IndexOfAny(specialCharacters, startpos + 1, subj.Length - startpos - 1);
if (endpos == -1)
endpos = subj.Length;
subj.Position = endpos;
// if we're at a newline, strip trailing spaces.
if (endpos < subj.Length && subj.Buffer[endpos] == '\n')
while (endpos > startpos && subj.Buffer[endpos - 1] == ' ')
endpos--;
return new Inline(subj.Buffer, startpos, endpos - startpos, startpos, endpos, c);
}
示例12: spnl
// Parse zero or more space characters, including at most one newline.
private static void spnl(Subject subj)
{
bool seen_newline = false;
while (peek_char(subj) == ' ' ||
(!seen_newline &&
(seen_newline = peek_char(subj) == '\n')))
{
advance(subj);
}
}
示例13: parse_inline
// Parse an inline, advancing subject, and add it to last element.
// Adjust tail to point to new last element of list.
// Return 0 if no inline can be parsed, 1 otherwise.
public static Syntax.Inline parse_inline(Subject subj)
{
Syntax.Inline inew = null;
string contents;
string special_chars;
char? c;
int endpos;
c = peek_char(subj);
if (c == null)
return null;
switch (c)
{
case '\n':
inew = handle_newline(subj);
break;
case '`':
inew = handle_backticks(subj);
break;
case '\\':
inew = handle_backslash(subj);
break;
case '&':
inew = handle_entity(subj);
break;
case '<':
inew = handle_pointy_brace(subj);
break;
case '_':
inew = HandleEmphasis(subj, '_');
break;
case '*':
inew = HandleEmphasis(subj, '*');
break;
case '[':
inew = handle_left_bracket(subj);
break;
case '!':
advance(subj);
if (peek_char(subj) == '[')
{
inew = handle_left_bracket(subj);
if (inew != null && inew.Tag == InlineTag.Link)
{
inew.Tag = InlineTag.Image;
}
else
{
inew = append_inlines(make_str("!"), inew);
}
}
else
{
inew = make_str("!");
}
break;
default:
// we read until we hit a special character
special_chars = "\n\\`&_*[]<!";
endpos = BString.binchr(subj.Buffer, subj.Position, special_chars);
if (endpos == subj.Position)
{
// current char is special: read a 1-character str
contents = take_one(subj);
}
else if (endpos == -1)
{
// special char not found, take whole rest of buffer:
endpos = subj.Buffer.Length;
contents = BString.bmidstr(subj.Buffer, subj.Position, endpos - subj.Position);
subj.Position = endpos;
}
else
{
// take buffer from subj.pos to endpos to str.
contents = BString.bmidstr(subj.Buffer, subj.Position, endpos - subj.Position);
subj.Position = endpos;
// if we're at a newline, strip trailing spaces.
if (peek_char(subj) == '\n')
contents = contents.TrimEnd();
}
inew = make_str(contents);
break;
}
return inew;
}
示例14: parse_inlines_while
// Parse inlines while a predicate is satisfied. Return inlines.
public static Syntax.Inline parse_inlines_while(Subject subj)
{
Syntax.Inline first = null;
Syntax.Inline cur;
while (!is_eof(subj))
{
cur = parse_inline(subj);
if (first == null)
{
first = cur;
subj.LastInline = cur.LastSibling;
}
else
{
subj.LastInline.Next = cur;
subj.LastInline = cur.LastSibling;
}
}
return first;
}
示例15: HandleOpenerCloser
private static Inline HandleOpenerCloser(Subject subj, InlineTag singleCharTag, InlineTag doubleCharTag, InlineParserParameters parameters)
{
bool canOpen, canClose;
var c = subj.Buffer[subj.Position];
var numdelims = ScanEmphasisDelimeters(subj, c, out canOpen, out canClose);
if (canClose)
{
// walk the stack and find a matching opener, if there is one
var istack = InlineStack.FindMatchingOpener(subj.LastPendingInline, InlineStack.InlineStackPriority.Emphasis, c, out canClose);
if (istack != null)
{
var useDelims = MatchInlineStack(istack, subj, numdelims, null, singleCharTag, doubleCharTag, parameters);
// if the closer was not fully used, move back a char or two and try again.
if (useDelims < numdelims)
{
subj.Position = subj.Position - numdelims + useDelims;
// use recursion only if it will not be very deep.
if (numdelims < 10)
return HandleOpenerCloser(subj, singleCharTag, doubleCharTag, parameters);
}
return null;
}
}
var inlText = new Inline(subj.Buffer, subj.Position - numdelims, numdelims, subj.Position - numdelims, subj.Position, c);
if (canOpen || canClose)
{
var istack = new InlineStack();
istack.DelimeterCount = numdelims;
istack.Delimeter = c;
istack.StartingInline = inlText;
istack.Priority = InlineStack.InlineStackPriority.Emphasis;
istack.Flags = (canOpen ? InlineStack.InlineStackFlags.Opener : 0)
| (canClose ? InlineStack.InlineStackFlags.Closer : 0);
InlineStack.AppendStackEntry(istack, subj);
}
return inlText;
}