-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
350 lines (308 loc) · 13 KB
/
Program.cs
File metadata and controls
350 lines (308 loc) · 13 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using CommandLine;
using Julmar.DocsToMarkdown;
using LearnDocUtils;
using MSLearnRepos;
using System.Diagnostics;
namespace ConvertDocx;
public static class Program
{
public static async Task Main(string[] args)
{
CommandLineOptions? options = null;
new Parser(cfg => cfg.HelpWriter = Console.Error)
.ParseArguments<CommandLineOptions>(args)
.WithParsed(clo => options = clo);
if (options == null) return;
List<string> errors = [];
try
{
if (options.InputFile.StartsWith("http"))
{
if (RequiresGitHubToken(options))
{
options.AccessToken = Environment.GetEnvironmentVariable("GITHUB_TOKEN")
?? await GetGitHubToken();
if (string.IsNullOrEmpty(options.AccessToken))
{
await Console.Error.WriteLineAsync("GitHub access token is required for the specified options.");
return;
}
}
EnsureValidOutputFile(options);
if (options.OutputFormat == OutputFormat.Docx)
{
options.OutputFile = Path.ChangeExtension(options.OutputFile, ".docx");
errors = !string.IsNullOrEmpty(options.AccessToken)
? await ConvertFromRepoAsync(options)
: await DownloadAndConvertAsync(options);
}
else
{
options.OutputFile = Path.ChangeExtension(options.OutputFile, ".md");
await DownloadMarkdown(options);
}
}
else if (options.InputFile.EndsWith(".docx", StringComparison.CurrentCultureIgnoreCase))
{
await ConvertDocxToMarkdown(options);
}
else
{
Console.WriteLine("ConvertDocx: unknown input file type.");
}
}
catch (AggregateException aex)
{
var ex = aex.Flatten();
errors.Add($"Conversion failed: {ex.Message} ({ex.GetType().Name})");
errors.AddRange(ex.InnerExceptions.Select(e => e.Message));
}
catch (Exception ex)
{
errors.Add($"Conversion failed: {ex.Message} ({ex.GetType().Name})");
}
errors?.ForEach(Console.Error.WriteLine);
}
private static void EnsureValidOutputFile(CommandLineOptions options)
{
if (options.OutputFile == null)
{
var segments = options.InputFile.Split('/').Where(s => !string.IsNullOrEmpty(s)).ToArray();
options.OutputFile = Path.ChangeExtension(segments[^1], options.OutputFormat == OutputFormat.Docx ? ".docx" : ".md");
}
if (Path.GetFileName(options.OutputFile).IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
{
throw new ArgumentException($"Invalid characters in output filename: {options.OutputFile}.");
}
}
private static bool RequiresGitHubToken(CommandLineOptions options)
{
return !string.IsNullOrEmpty(options.GitHubRepo) ||
!string.IsNullOrEmpty(options.Organization) ||
!string.IsNullOrEmpty(options.GitHubBranch);
}
private static async Task<List<string>> ConvertFromRepoAsync(CommandLineOptions options)
{
var metadata = await DocsMetadata.LoadFromUrlAsync(options.InputFile);
if (metadata == null || metadata.ContentPath == null)
throw new ArgumentException($"Unable to load metadata from {options.InputFile}.");
options.Organization ??= metadata.Organization ?? Constants.DocsOrganization;
options.GitHubRepo = metadata.Repository;
options.GitHubBranch = metadata.Branch;
var inputFile = metadata.PageType == "conceptual"
? metadata.ContentPath
: Path.GetDirectoryName(metadata.ContentPath)
?? throw new ArgumentException("Invalid metadata for URL.");
if (inputFile.EndsWith(".md", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine($"Converting Docs article {options.InputFile} to {options.OutputFile}");
return await SinglePageToDocx.ConvertFromRepoAsync(
options.InputFile, options.Organization, options.GitHubRepo, options.GitHubBranch,
inputFile, options.OutputFile, options.AccessToken, new DocumentOptions
{
Debug = options.Debug,
ZonePivot = options.ZonePivot
});
}
else
{
Console.WriteLine($"Converting Learn module {options.InputFile} to {options.OutputFile}");
return await LearnToDocx.ConvertFromRepoAsync(
options.InputFile, options.Organization, options.GitHubRepo, options.GitHubBranch,
inputFile, options.OutputFile, options.AccessToken, new DocumentOptions
{
Debug = options.Debug,
ZonePivot = options.ZonePivot,
EmbedNotebookContent = options.ConvertNotebooks
});
}
}
private static async Task DownloadMarkdown(CommandLineOptions options)
{
Console.WriteLine($"Converting {options.InputFile} to {options.OutputFile}");
var tempFolder = Path.Combine(Path.GetTempPath(), "LearnDocs");
if (Directory.Exists(tempFolder)) Directory.Delete(tempFolder, true);
Directory.CreateDirectory(tempFolder);
try
{
var downloader = new DocsConverter(tempFolder, new Uri(options.InputFile));
var createdFiles = await downloader.ConvertAsync(!options.PreferPlainMarkdown,
#if DEBUG
tag =>
{
var trimmed = tag.TrimStart();
var prefix = trimmed.Length > 20 ? trimmed[..20] : trimmed;
Console.Error.WriteLine($"Skipped: {prefix}");
});
#else
null);
#endif
if (createdFiles.Count == 0)
throw new InvalidOperationException("No files created during download.");
// Move the files to the output folder.
var isModule = createdFiles.Any(f => f.Filename.EndsWith(".yml"));
var inputFile = isModule
? createdFiles.First(f => f.FileType == FileType.Folder).Filename
: createdFiles.Single(f => f.FileType == FileType.Markdown).Filename;
if (isModule)
{
var outputFolder = Path.Combine(Path.ChangeExtension(options.OutputFile, null) ?? Directory.GetCurrentDirectory(), Path.GetFileName(inputFile));
MoveOrCopyDirectory(inputFile, outputFolder);
Console.WriteLine($"Module \"{options.InputFile}\" downloaded to {outputFolder}");
}
else
{
var outputFolder = Path.GetDirectoryName(options.OutputFile) ?? Directory.GetCurrentDirectory();
File.Move(inputFile, options.OutputFile!);
if (createdFiles.Count > 1)
{
foreach (var file in createdFiles.Where(f => f.FileType == FileType.Image || f.FileType == FileType.Asset))
{
var relativeFile = Path.GetRelativePath(tempFolder, file.Filename);
var destFile = Path.Combine(outputFolder, relativeFile);
File.Move(file.Filename, destFile);
}
}
Console.WriteLine($"Article \"{options.InputFile}\" downloaded to {options.OutputFile}");
}
}
finally
{
if (Directory.Exists(tempFolder))
Directory.Delete(tempFolder, true);
}
}
private static void MoveOrCopyDirectory(string inputFolder, string outputFolder)
{
inputFolder = Path.GetFullPath(inputFolder);
outputFolder = Path.GetFullPath(outputFolder);
try
{
Directory.Move(inputFolder, outputFolder);
return;
}
catch (IOException)
{
}
CopyFolder(inputFolder, outputFolder);
}
private static void CopyFolder(string inputFolder, string outputFolder)
{
// Copy the files.
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);
// Copy all files
foreach (string file in Directory.GetFiles(inputFolder))
{
string destFile = Path.Combine(outputFolder, Path.GetFileName(file));
File.Copy(file, destFile, true);
}
// Recursively copy all subdirectories
foreach (string dir in Directory.GetDirectories(inputFolder))
{
string destDir = Path.Combine(outputFolder, Path.GetFileName(dir));
CopyFolder(dir, destDir);
}
}
private static async Task<List<string>> DownloadAndConvertAsync(CommandLineOptions options)
{
Console.WriteLine($"Downloading {options.InputFile}");
var tempFolder = Path.Combine(Path.GetTempPath(), "LearnDocs");
if (Directory.Exists(tempFolder)) Directory.Delete(tempFolder, true);
Directory.CreateDirectory(tempFolder);
try
{
var downloader = new DocsConverter(tempFolder, new Uri(options.InputFile));
var createdFiles = await downloader.ConvertAsync(!options.PreferPlainMarkdown,
#if DEBUG
tag =>
{
var trimmed = tag.TrimStart();
var prefix = trimmed.Length > 20 ? trimmed[..20] : trimmed;
Console.Error.WriteLine($"Skipped: {prefix}");
});
#else
null);
#endif
if (createdFiles.Count == 0)
throw new InvalidOperationException("No files created during download.");
var isModule = createdFiles.Any(f => f.Filename.EndsWith(".yml"));
var inputFile = isModule
? createdFiles.First(f => f.FileType == FileType.Folder).Filename
: createdFiles.Single(f => f.FileType == FileType.Markdown).Filename;
if (!isModule)
{
Console.WriteLine($"Converting Docs article {inputFile} to {options.OutputFile}");
return await SinglePageToDocx.ConvertFromFileAsync(options.InputFile, inputFile, options.OutputFile,
new DocumentOptions {
Debug = options.Debug,
ZonePivot = options.ZonePivot
});
}
else
{
Console.WriteLine($"Converting Learn module {inputFile} to {options.OutputFile}");
return await LearnToDocx.ConvertFromFolderAsync(options.InputFile, inputFile, options.OutputFile,
new DocumentOptions {
Debug = options.Debug,
ZonePivot = options.ZonePivot,
EmbedNotebookContent = options.ConvertNotebooks
});
}
}
finally
{
if (Directory.Exists(tempFolder))
Directory.Delete(tempFolder, true);
}
}
private static async Task ConvertDocxToMarkdown(CommandLineOptions options)
{
if (options.SinglePageOutput)
{
options.OutputFile ??= Path.ChangeExtension(options.InputFile, ".md");
Console.WriteLine($"Converting {options.InputFile} to single-page Markdown {options.OutputFile}");
await DocxToSinglePage.ConvertAsync(options.InputFile, options.OutputFile,
new MarkdownOptions { Debug = options.Debug, UsePlainMarkdown = options.PreferPlainMarkdown });
}
else
{
options.OutputFile ??= Path.ChangeExtension(options.InputFile, "");
Console.WriteLine($"Converting {options.InputFile} to Learn module {options.OutputFile}");
await DocxToLearn.ConvertAsync(options.InputFile, options.OutputFile,
new MarkdownOptions { Debug = options.Debug, UsePlainMarkdown = options.PreferPlainMarkdown });
}
}
private static async Task<string?> GetGitHubToken()
{
var psi = new ProcessStartInfo("op")
{
Arguments = "read \"op://personal/github.com/token\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
try
{
using var process = Process.Start(psi);
if (process != null)
{
var token = await process.StandardOutput.ReadToEndAsync();
await process.WaitForExitAsync();
return token.TrimEnd();
}
}
#if DEBUG
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to retrieve GitHub token: {ex.Message}");
}
#else
catch
{
// Ignore errors.
}
#endif
return null;
}
}