-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearnToDocx.cs
More file actions
60 lines (51 loc) · 2.57 KB
/
LearnToDocx.cs
File metadata and controls
60 lines (51 loc) · 2.57 KB
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
using MSLearnRepos;
namespace LearnDocUtils;
public static class LearnToDocx
{
public static async Task<List<string>> ConvertFromRepoAsync(string url, string organization, string repo, string branch, string folder,
string outputFile, string accessToken = null, DocumentOptions options = null)
{
if (string.IsNullOrEmpty(organization))
throw new ArgumentException($"'{nameof(organization)}' cannot be null or empty.", nameof(organization));
if (string.IsNullOrEmpty(repo))
throw new ArgumentException($"'{nameof(repo)}' cannot be null or empty.", nameof(repo));
if (string.IsNullOrEmpty(folder))
throw new ArgumentException($"'{nameof(folder)}' cannot be null or empty.", nameof(folder));
return await Convert(LearnRepoService.Create(organization, repo, branch, accessToken), url,
folder, outputFile, options);
}
public static async Task<List<string>> ConvertFromFolderAsync(string url, string learnFolder, string outputFile, DocumentOptions options = null)
{
if (string.IsNullOrWhiteSpace(learnFolder))
throw new ArgumentException($"'{nameof(learnFolder)}' cannot be null or whitespace.", nameof(learnFolder));
if (!Directory.Exists(learnFolder))
throw new DirectoryNotFoundException($"{learnFolder} does not exist.");
return await Convert(LearnRepoService.Create(learnFolder), url, learnFolder, outputFile, options);
}
private static async Task<List<string>> Convert(ILearnRepoService learnRepo, string url,
string moduleFolder, string docxFile, DocumentOptions options)
{
var rootTemp = options?.Debug == true ? Environment.GetFolderPath(Environment.SpecialFolder.Desktop) : Path.GetTempPath();
var tempFolder = Path.Combine(rootTemp, Path.GetRandomFileName());
while (Directory.Exists(tempFolder))
{
tempFolder = Path.Combine(rootTemp, Path.GetRandomFileName());
}
// Download the module
var (module, markdownFile) = await ModuleCombiner.DownloadModuleAsync(
learnRepo, moduleFolder, tempFolder,
options?.EmbedNotebookContent == true);
try
{
// Convert the file.
return await MarkdownToDocConverter.ConvertMarkdownToDocx(learnRepo, url, moduleFolder, module, markdownFile, docxFile, options?.ZonePivot, options?.Debug==true);
}
finally
{
if (options is {Debug: false})
{
Directory.Delete(tempFolder, true);
}
}
}
}