-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
112 lines (102 loc) · 4.11 KB
/
Utils.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
//using System.Text.Json;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace ResumePDF
{
internal static class Utils
{
//public static bool TryConvertJson<T>()
public static string ReturnFirstFile(string directory, string extension)
{
string[] files = Directory.GetFiles(directory, extension);
Array.Sort(files);
return files[0];
}
public static IComponent JsonToComponent(string fileName)
{
// open the file contents
string jsonContents = File.ReadAllText(fileName);
//parses the Json into the right class type or returns a null otherwise
if (jsonContents.TryParseJson(out BulletModelSimple simpleBullet))
{
return new BulletSimpleComponent() { SimpleBulletModel = simpleBullet };
}
if (jsonContents.TryParseJson(out BulletsModel bullets))
{
return new BulletsComponent() { bulletsModel = bullets };
}
if (jsonContents.TryParseJson(out ContactModel[] Contacts))
{
return new ContactComponent() { contactItems = Contacts }; ;
}
if (jsonContents.TryParseJson(out HighlightModel highlightItems))
{
return new HighlightedItemsComponent() { highlightItems = highlightItems };
}
if (jsonContents.TryParseJson(out ListModel basicList))
{
return new BasicListComponent() { BasicList = basicList };
}
return null; //if none matches
}
public static bool TryParseJson<T>(this string @this, out T result)
{
bool success = true;
var settings = new JsonSerializerSettings
{
Error = (sender, args) => { success = false; args.ErrorContext.Handled = true; },
MissingMemberHandling = MissingMemberHandling.Error
};
//JsonSerializerOptions options = new JsonSerializerOptions { AllowTrailingCommas = true };
// deserialize the file contents, not the fileName
//T? result = JsonSerializer.Deserialize<T>(jsonContents, options);
result = JsonConvert.DeserializeObject<T>(@this, settings);
return success;
}
public static T? ParseJson<T>(string fileName) =>
JsonConvert.DeserializeObject<T>(File.ReadAllText(@fileName));
public static void ForEach<T>(this IEnumerable<T> list, System.Action<T> action)
{
foreach (T item in list)
action(item);
}
public static string BetweenStrings(this string FullString, string LeftString, string RightString)
{
var from = FullString.LastIndexOf(LeftString) + LeftString.Length;
var to = FullString.IndexOf(RightString);
return FullString[from..to]; // THE_TARGET_STRING
}
}
// single typography class can help with keeping document look&feel consistent
internal static class TypographyStyles
{
public static TextStyle Normal => TextStyle
.Default
.FontSize(9)
.FontColor(Colors.Black)
.FontFamily("Ubuntu")
.FontColor(Colors.Black)
.Fallback(x => x.FontFamily(Fonts.SegoeUI))
.Fallback(x => x.FontFamily(Fonts.Calibri));
public static TextStyle Headline => Normal
.FontSize(12).Bold();
public static TextStyle Headline2 => Normal
.FontSize(12).NormalWeight();
public static TextStyle AccentStyle => Normal
.FontColor(Colors.Blue.Accent2);
public static TextStyle AccentStyle2 => AccentStyle
.Italic().FontSize(8);
public static TextStyle Title => Normal
.FontSize(23);
public static TextStyle Title2 => Normal
.FontSize(14).Bold();
}
}