-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
ParserUtilities.cs
68 lines (61 loc) · 1.92 KB
/
ParserUtilities.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
namespace Our.Umbraco.Nexu.Parsers.Helpers
{
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using global::Umbraco.Core;
/// <summary>
/// Represents the parser utilities. This objects has useful methods for helping to parse content
/// </summary>
internal static class ParserUtilities
{
/// <summary>
/// Gets all the document udi from a text
/// </summary>
/// <param name="text">
/// The text.
/// </param>
/// <returns>
/// The <see cref="IEnumerable{Udi}"/>.
/// </returns>
public static IEnumerable<Udi> GetDocumentUdiFromText(string text)
{
return GetUdiFromText(text, "umb://document/(.{32})");
}
/// <summary>
/// Gets all the media udi from a text
/// </summary>
/// <param name="text">
/// The text.
/// </param>
/// <returns>
/// The <see cref="IEnumerable{Udi}"/>.
/// </returns>
public static IEnumerable<Udi> GetMediaUdiFromText(string text)
{
return GetUdiFromText(text, "umb://media/(.{32})");
}
/// <summary>
/// Get all udi from a text that match the regex
/// </summary>
/// <param name="text">
/// The text.
/// </param>
/// <param name="regex">
/// The regex.
/// </param>
/// <returns>
/// The <see cref="IEnumerable{Udi}"/>.
/// </returns>
private static IEnumerable<Udi> GetUdiFromText(string text, string regex)
{
var udiList = new List<Udi>();
var udiMatches = Regex.Matches(text, regex);
foreach (Match match in udiMatches)
{
udiList.Add(new StringUdi(new Uri(match.Value)));
}
return udiList.DistinctBy(x => x.ToString());
}
}
}