-
Notifications
You must be signed in to change notification settings - Fork 365
/
ColorizationRule.cs
89 lines (76 loc) · 3.47 KB
/
ColorizationRule.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
79
80
81
82
83
84
85
86
87
88
89
// Copyright © Microsoft Corporation.
// This source file is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
// Change History
// 03/09/2013 - EFW - Moved the class into the Snippets namespace and made it public
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Sandcastle.Tools.BuildComponents.Snippets
{
/// <summary>
/// This defines a colorization rule
/// </summary>
public class ColorizationRule
{
#region Properties
//=====================================================================
/// <summary>
/// This read-only property returns the regular expression used by the rule
/// </summary>
public Regex Pattern { get; }
/// <summary>
/// This read-only property returns the optional region name used to limit the part of the match that
/// is returned from the matches.
/// </summary>
public string Region { get; }
/// <summary>
/// This read-only property returns the class name to use for the matched region
/// </summary>
public string ClassName { get; }
#endregion
#region Constructors
//=====================================================================
/// <summary>
/// Constructor. Defines a rule with a pattern and a class name but no region name
/// </summary>
/// <param name="pattern">The regular expression used to match the region text</param>
/// <param name="className">The class name to apply to the region</param>
/// <overloads>There are two overloads for the constructor</overloads>
public ColorizationRule(string pattern, string className) : this(pattern, null, className)
{
}
/// <summary>
/// Constructor. Defines a rule with a pattern, a region name to limit the match to a specific
/// part of the pattern, and a class name.
/// </summary>
/// <param name="pattern">The regular expression used to match the region text</param>
/// <param name="region">The region name that defines the named part of the regular expression to return
/// for each match.</param>
/// <param name="className">The class name to apply to the region</param>
public ColorizationRule(string pattern, string region, string className)
{
this.Pattern = new Regex(pattern, RegexOptions.Multiline);
this.Region = region;
this.ClassName = className;
}
#endregion
#region Helper methods
//=====================================================================
/// <summary>
/// This returns an enumerable list of captures that match the pattern, if any
/// </summary>
/// <param name="text">The text to search</param>
/// <returns>An enumerable list of captures that match the pattern</returns>
public IEnumerable<Capture> Apply(string text)
{
MatchCollection matches = this.Pattern.Matches(text);
foreach(Match m in matches)
if(this.Region == null)
yield return m;
else
yield return m.Groups[this.Region];
}
#endregion
}
}