-
Notifications
You must be signed in to change notification settings - Fork 58
/
PartialTagParser.cs
78 lines (67 loc) · 2.52 KB
/
PartialTagParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// <copyright file="PartialTagParser.cs" company="Stubble Authors">
// Copyright (c) Stubble Authors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>
using Stubble.Core.Imported;
using Stubble.Core.Tokens;
namespace Stubble.Core.Parser.TokenParsers
{
/// <summary>
/// A parser for partial tags
/// </summary>
public class PartialTagParser : Interfaces.InlineParser
{
private const char TagId = '>';
/// <summary>
/// Tries to match partial tag from the current slice
/// </summary>
/// <param name="processor">The processor</param>
/// <param name="slice">The slice</param>
/// <returns>If a match was found</returns>
public override bool Match(Processor processor, ref StringSlice slice)
{
var tagStart = slice.Start - processor.CurrentTags.StartTag.Length;
var index = slice.Start;
while (slice[index].IsWhitespace())
{
index++;
}
var match = slice[index];
if (match == TagId)
{
index++;
while (slice[index].IsWhitespace())
{
index++;
}
slice.Start = index;
var startIndex = slice.Start;
var partialTag = new PartialToken
{
LineIndent = processor.HasSeenNonSpaceOnLine ? 0 : processor.LineIndent,
TagStartPosition = tagStart,
ContentStartPosition = startIndex,
IsClosed = false
};
processor.CurrentToken = partialTag;
while (!slice.IsEmpty && !slice.Match(processor.CurrentTags.EndTag))
{
slice.NextChar();
}
if (slice.IsEmpty)
{
return false;
}
var content = new StringSlice(slice.Text, startIndex, slice.Start - 1);
content.TrimEnd();
var contentEnd = content.End + 1;
partialTag.ContentEndPosition = contentEnd;
partialTag.TagEndPosition = slice.Start + processor.CurrentTags.EndTag.Length;
partialTag.IsClosed = true;
slice.Start += processor.CurrentTags.EndTag.Length;
return true;
}
return false;
}
}
}