1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
3
+ using System . Text . RegularExpressions ;
4
+
5
+ namespace Fragments . Areas . Fragments . Models
6
+ {
7
+ public class FragmentModel
8
+ {
9
+ private const string HtmlType = "html" ;
10
+ private const string JsType = "js" ;
11
+ private const string CssType = "css" ;
12
+
13
+ private static readonly Regex FragmentTypeRegex = new Regex ( @"/[^.]+\.([^.]+)\.[^.]+$" ) ;
14
+
15
+ public FragmentModel ( string fragmentGroupName , IEnumerable < string > templates )
16
+ {
17
+ FragmentGroupName = fragmentGroupName ;
18
+ var fragmentTypes = GetFragmentTypes ( templates ) ;
19
+ Html = GetFragmentType ( HtmlType , fragmentTypes ) ;
20
+ Js = GetFragmentType ( JsType , fragmentTypes ) ;
21
+ Css = GetFragmentType ( CssType , fragmentTypes ) ;
22
+ }
23
+
24
+ public string FragmentGroupName { get ; }
25
+ public string Html { get ; }
26
+ public string Js { get ; }
27
+ public string Css { get ; }
28
+ public string Id => FragmentGroupName . Replace ( "/" , string . Empty ) . ToLowerInvariant ( ) ;
29
+
30
+ private static string GetFragmentType ( string type , IEnumerable < ( string type , string template ) > types )
31
+ {
32
+ return types
33
+ . Where ( ft => ft . type == type )
34
+ . Select ( ft => ft . template )
35
+ . FirstOrDefault ( ) ?? string . Empty ;
36
+ }
37
+
38
+ private static List < ( string type , string template ) > GetFragmentTypes ( IEnumerable < string > templates )
39
+ {
40
+ return templates
41
+ . Select ( t =>
42
+ {
43
+ var match = FragmentTypeRegex . Match ( t ) ;
44
+ return ! match . Success ? ( HtmlType , t ) : ( match . Groups [ 1 ] . Value , t ) ;
45
+ } )
46
+ . ToList ( ) ;
47
+ }
48
+ }
49
+ }
0 commit comments