finished pandoc logic; removed warnings

This commit is contained in:
nilsk 2025-05-27 16:06:15 +02:00
parent 81b276be69
commit b3108fd5a9
4 changed files with 68 additions and 32 deletions

View File

@ -17,9 +17,6 @@ public partial class CodeBlock : ComponentBase
[CascadingParameter] [CascadingParameter]
public CodeTabs? ParentTabs { get; set; } public CodeTabs? ParentTabs { get; set; }
private static readonly string DARK_BACKGROUND_COLOR = "#2d2d2d";
private static readonly string DARK_FOREGROUND_COLOR = "#f8f8f2";
protected override void OnInitialized() protected override void OnInitialized()
{ {
if (this.ParentTabs is not null && this.Title is not null) if (this.ParentTabs is not null && this.Title is not null)

View File

@ -23,6 +23,6 @@ public partial class CodeTabs : ComponentBase
private class CodeTabItem private class CodeTabItem
{ {
public string Title { get; init; } = string.Empty; public string Title { get; init; } = string.Empty;
public RenderFragment Fragment { get; init; } public RenderFragment Fragment { get; init; } = null!;
} }
} }

View File

@ -20,6 +20,7 @@ public partial class PandocDialog : ComponentBase
[CascadingParameter] [CascadingParameter]
private IMudDialogInstance MudDialog { get; set; } = null!; private IMudDialogInstance MudDialog { get; set; } = null!;
private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger("PandocDialog");
private static readonly string LICENCE_URI = "https://raw.githubusercontent.com/jgm/pandoc/master/COPYRIGHT"; private static readonly string LICENCE_URI = "https://raw.githubusercontent.com/jgm/pandoc/master/COPYRIGHT";
private static string PANDOC_VERSION = "1.0.0"; private static string PANDOC_VERSION = "1.0.0";
@ -110,7 +111,8 @@ public partial class PandocDialog : ComponentBase
} }
catch (Exception ex) catch (Exception ex)
{ {
this.licenseText = "Error loading license text, please consider following the links to the GPL."; this.licenseText = "Error loading license text, please consider following the links to read the GPL.";
LOG.LogError("Error loading GPL license text:\n{ErrorMessage}", ex.Message);
} }
finally finally
{ {

View File

@ -9,16 +9,18 @@ namespace AIStudio.Tools;
public static partial class Pandoc public static partial class Pandoc
{ {
private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger("PluginFactory"); private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger("PandocService");
private static readonly string DOWNLOAD_URL = "https://github.com/jgm/pandoc/releases/download"; private static readonly string DOWNLOAD_URL = "https://github.com/jgm/pandoc/releases/download";
private static readonly string LATEST_URL = "https://github.com/jgm/pandoc/releases/latest"; private static readonly string LATEST_URL = "https://github.com/jgm/pandoc/releases/latest";
private static readonly Version MINIMUM_REQUIRED_VERSION = new (3, 6); private static readonly Version MINIMUM_REQUIRED_VERSION = new (3, 6);
private static readonly Version FALLBACK_VERSION = new (3, 6, 4); private static readonly Version FALLBACK_VERSION = new (3, 7, 0, 1);
private static readonly string CPU_ARCHITECTURE = "win-x64"; private static readonly string CPU_ARCHITECTURE = "win-x64";
/// <summary> /// <summary>
/// Checks if pandoc is available on the system and can be started as a process /// Checks if pandoc is available on the system and can be started as a process or present in AiStudio's data dir
/// </summary> /// </summary>
/// <param name="rustService">Global rust service to access file system and data dir</param>
/// <param name="showMessages">Controls if snackbars are shown to the user</param>
/// <returns>True, if pandoc is available and the minimum required version is met, else False.</returns> /// <returns>True, if pandoc is available and the minimum required version is met, else False.</returns>
public static async Task<bool> CheckAvailabilityAsync(RustService rustService, bool showMessages = true) public static async Task<bool> CheckAvailabilityAsync(RustService rustService, bool showMessages = true)
{ {
@ -31,15 +33,7 @@ public static partial class Pandoc
return true; return true;
} }
var hasPandoc = false; if (HasPandoc(installDir)) return true;
foreach (var subdirectory in subdirectories)
{
if (subdirectory.Contains("pandoc"))
hasPandoc = true;
}
if (hasPandoc)
return true;
try try
{ {
@ -70,7 +64,7 @@ public static partial class Pandoc
return false; return false;
} }
var versionMatch = PandocRegex().Match(output); var versionMatch = PandocCmdRegex().Match(output);
if (!versionMatch.Success) if (!versionMatch.Success)
{ {
if (showMessages) if (showMessages)
@ -78,10 +72,8 @@ public static partial class Pandoc
LOG.LogError("pandoc --version returned an invalid format:\n {Output}", output); LOG.LogError("pandoc --version returned an invalid format:\n {Output}", output);
return false; return false;
} }
var versions = versionMatch.Groups[1].Value.Split('.'); var versions = versionMatch.Groups[1].Value;
var major = int.Parse(versions[0]); var installedVersion = Version.Parse(versions);
var minor = int.Parse(versions[1]);
var installedVersion = new Version(major, minor);
if (installedVersion >= MINIMUM_REQUIRED_VERSION) if (installedVersion >= MINIMUM_REQUIRED_VERSION)
{ {
@ -105,6 +97,35 @@ public static partial class Pandoc
} }
} }
private static bool HasPandoc(string pandocDirectory)
{
try
{
var subdirectories = Directory.GetDirectories(pandocDirectory);
foreach (var subdirectory in subdirectories)
{
var pandocPath = Path.Combine(subdirectory, "pandoc.exe");
if (File.Exists(pandocPath))
{
return true;
}
}
return false;
}
catch (Exception ex)
{
LOG.LogInformation("Pandoc is not installed in the data directory and might have thrown and error:\n{ErrorMessage}", ex.Message);
return false;
}
}
/// <summary>
/// Automatically decompresses the latest pandoc archive into AiStudio's data directory
/// </summary>
/// <param name="rustService">Global rust service to access file system and data dir</param>
/// <returns>None</returns>
public static async Task InstallAsync(RustService rustService) public static async Task InstallAsync(RustService rustService)
{ {
var installDir = await GetPandocDataFolder(rustService); var installDir = await GetPandocDataFolder(rustService);
@ -136,7 +157,10 @@ public static partial class Pandoc
} }
else if (uri.Contains(".tar.gz")) else if (uri.Contains(".tar.gz"))
{ {
Console.WriteLine("is zip"); var tempTarPath = Path.Join(Path.GetTempPath(), "pandoc.tar.gz");
await File.WriteAllBytesAsync(tempTarPath, fileBytes);
ZipFile.ExtractToDirectory(tempTarPath, installDir);
File.Delete(tempTarPath);
} }
else else
{ {
@ -171,20 +195,25 @@ public static partial class Pandoc
} }
} }
/// <summary>
/// Asynchronously fetch the content from Pandoc's latest release page and extract the latest version number
/// </summary>
/// <remarks>Version numbers can have the following formats: x.x, x.x.x or x.x.x.x</remarks>
/// <returns>Latest Pandoc version number</returns>
public static async Task<string> FetchLatestVersionAsync() { public static async Task<string> FetchLatestVersionAsync() {
using var client = new HttpClient(); using var client = new HttpClient();
var response = await client.GetAsync(LATEST_URL); var response = await client.GetAsync(LATEST_URL);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
LOG.LogError("Code {StatusCode}: Could not fetch pandocs latest page:\n {Response}", response.StatusCode, response.RequestMessage); LOG.LogError("Code {StatusCode}: Could not fetch Pandoc's latest page:\n {Response}", response.StatusCode, response.RequestMessage);
await MessageBus.INSTANCE.SendWarning(new (Icons.Material.Filled.Warning, $"The latest pandoc version was not found, installing version {FALLBACK_VERSION.ToString()} instead.")); await MessageBus.INSTANCE.SendWarning(new (Icons.Material.Filled.Warning, $"The latest pandoc version was not found, installing version {FALLBACK_VERSION.ToString()} instead."));
return FALLBACK_VERSION.ToString(); return FALLBACK_VERSION.ToString();
} }
var htmlContent = await response.Content.ReadAsStringAsync(); var htmlContent = await response.Content.ReadAsStringAsync();
var versionMatch = VersionRegex().Match(htmlContent); var versionMatch = LatestVersionRegex().Match(htmlContent);
if (!versionMatch.Success) if (!versionMatch.Success)
{ {
LOG.LogError("The latest version regex returned nothing:\n {Value}", versionMatch.Groups.ToString()); LOG.LogError("The latest version regex returned nothing:\n {Value}", versionMatch.Groups.ToString());
@ -196,7 +225,10 @@ public static partial class Pandoc
return version; return version;
} }
// win arm not available /// <summary>
/// Reads the systems architecture to find the correct archive
/// </summary>
/// <returns>Full URI to the right archive in Pandoc's repo</returns>
public static async Task<string> GenerateUriAsync() public static async Task<string> GenerateUriAsync()
{ {
var version = await FetchLatestVersionAsync(); var version = await FetchLatestVersionAsync();
@ -212,6 +244,10 @@ public static partial class Pandoc
}; };
} }
/// <summary>
/// Reads the systems architecture to find the correct Pandoc installer
/// </summary>
/// <returns>Full URI to the right installer in Pandoc's repo</returns>
public static async Task<string> GenerateInstallerUriAsync() public static async Task<string> GenerateInstallerUriAsync()
{ {
var version = await FetchLatestVersionAsync(); var version = await FetchLatestVersionAsync();
@ -232,15 +268,16 @@ public static partial class Pandoc
} }
/// <summary> /// <summary>
/// Returns the name of the pandoc executable based on the running operating system /// Reads the os platform to determine the used executable name
/// </summary> /// </summary>
/// <returns>Name of the pandoc executable</returns>
private static string GetPandocExecutableName() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "pandoc.exe" : "pandoc"; private static string GetPandocExecutableName() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "pandoc.exe" : "pandoc";
private static async Task<string> GetPandocDataFolder(RustService rustService) => Path.Join(await rustService.GetDataDirectory(), "pandoc"); private static async Task<string> GetPandocDataFolder(RustService rustService) => Path.Join(await rustService.GetDataDirectory(), "pandoc");
[GeneratedRegex(@"pandoc(?:\.exe)?\s*([0-9]+\.[0-9]+)")] [GeneratedRegex(@"pandoc(?:\.exe)?\s*([0-9]+\.[0-9]+(?:\.[0-9]+)?(?:\.[0-9]+)?)")]
private static partial Regex PandocRegex(); private static partial Regex PandocCmdRegex();
[GeneratedRegex(@"pandoc(?:\.exe)?\s*([0-9]+\.[0-9]+\.[0-9]+(?:\.[0-9]+)?)")] [GeneratedRegex(@"pandoc(?:\.exe)?\s*([0-9]+\.[0-9]+(?:\.[0-9]+)?(?:\.[0-9]+)?)")]
private static partial Regex VersionRegex(); private static partial Regex LatestVersionRegex();
} }