Merge branch 'develop' into feature/cache

This commit is contained in:
Stone_Red
2024-05-16 19:39:01 +02:00
committed by GitHub
18 changed files with 258 additions and 33 deletions
+21
View File
@@ -3,10 +3,15 @@ using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
using CuteUtils.Logging;
using Markdowser.Models;
using Markdowser.Utilities;
using Markdowser.ViewModels;
using Markdowser.Views;
using System.IO;
namespace Markdowser;
public partial class App : Application
@@ -18,6 +23,11 @@ public partial class App : Application
public override void OnFrameworkInitializationCompleted()
{
if (!Directory.Exists(Configuration.ApplicationDataPath))
{
_ = Directory.CreateDirectory(Configuration.ApplicationDataPath);
}
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
@@ -25,12 +35,23 @@ public partial class App : Application
DataContext = new MainWindowViewModel(),
};
// Clear log files
GlobalState.Logger.ClearLogFile(LogSeverity.Debug);
GlobalState.Logger.ClearLogFile(LogSeverity.Info);
GlobalState.Logger.ClearLogFile(LogSeverity.Warn);
GlobalState.Logger.ClearLogFile(LogSeverity.Error);
GlobalState.Logger.ClearLogFile(LogSeverity.Fatal);
desktop.MainWindow.Closing += (sender, e) =>
{
GlobalState.Logger.LogInfo("Saving settings...");
Settings.SaveSettings();
GlobalState.Logger.LogInfo("Settings saved.");
};
}
GlobalState.Logger.LogInfo("Application started.");
base.OnFrameworkInitializationCompleted();
if (Current is not null)
+2 -3
View File
@@ -1,7 +1,6 @@
using Markdowser.Utilities;
using System;
using System.Diagnostics;
using System.Windows.Input;
namespace Markdowser.Commands;
@@ -23,7 +22,7 @@ public class HyperlinkCommand : ICommand
{
if (parameter is string url)
{
Debug.WriteLine($"Hyperlink clicked: {url}");
GlobalState.Logger.LogDebug($"Hyperlink clicked: {url}");
if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
@@ -35,7 +34,7 @@ public class HyperlinkCommand : ICommand
}
else
{
Debug.WriteLine($"Invalid URL: {url}");
GlobalState.Logger.LogDebug($"Invalid URL: {url}");
}
}
}
+3
View File
@@ -7,6 +7,7 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
<Version>0.1.0.0</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
</PropertyGroup>
<ItemGroup>
@@ -30,12 +31,14 @@
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.10" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.10" />
<PackageReference Include="CuteUtils" Version="1.0.0" />
<PackageReference Include="Markdown.Avalonia" Version="11.0.2" />
<PackageReference Include="Projektanker.Icons.Avalonia" Version="9.1.2" />
<PackageReference Include="Projektanker.Icons.Avalonia.FontAwesome" Version="9.1.2" />
<PackageReference Include="Projektanker.Icons.Avalonia.MaterialDesign" Version="9.1.2" />
<PackageReference Include="ReverseMarkdown" Version="4.3.0" />
<PackageReference Include="Semi.Avalonia" Version="11.0.7.1" />
<PackageReference Include="Syroot.Windows.IO.KnownFolders" Version="1.3.0" />
</ItemGroup>
<ItemGroup>
+12
View File
@@ -1,6 +1,8 @@
using Markdowser.Utilities;
using System;
using System.IO;
using System.Reflection;
using System.Text.Json;
namespace Markdowser.Models;
@@ -15,6 +17,8 @@ public class Settings
public string? HomeUrl { get; set; }
public string UserAgent { get; set; } = $"{Assembly.GetExecutingAssembly().GetName().Name}/{Assembly.GetExecutingAssembly().GetName().Version?.ToString()}";
public static void SaveSettings()
{
if (!Directory.Exists(Configuration.ApplicationDataPath))
@@ -28,9 +32,17 @@ public class Settings
private static Settings LoadSettings()
{
if (File.Exists(Configuration.SettingsFilePath))
{
try
{
return JsonSerializer.Deserialize<Settings>(File.ReadAllText(Configuration.SettingsFilePath)) ?? new Settings();
}
catch (Exception ex)
{
GlobalState.Logger.LogWarn(ex.Message);
return new Settings();
}
}
else
{
return new Settings();
@@ -9,6 +9,10 @@ namespace Markdowser.Processing;
internal interface IContentProcessor
{
string Name { get; }
string Description { get; }
bool CanProcess(HttpContentHeaders httpContentHeaders);
Task<ContentViewModelBase> Process(HttpResponseMessage httpResponseMessage, IProgress<ProcessingProgress> progress);
@@ -7,8 +7,13 @@ using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Markdowser.Processing.Processors;
internal class CommonImageProcessor : IContentProcessor
{
public string Name => "Common Image Processor";
public string Description => "Processes common image types (e.g. PNG, JPEG, GIF)";
public bool CanProcess(HttpContentHeaders httpContentHeaders)
{
if (httpContentHeaders.ContentType?.MediaType == "image/svg+xml")
@@ -1,10 +1,10 @@
using HtmlAgilityPack;
using Markdowser.Utilities;
using Markdowser.ViewModels;
using Markdowser.ViewModels.Content;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
@@ -19,6 +19,10 @@ internal partial class HtmlProcessor : IContentProcessor
{
private readonly ReverseMarkdown.Converter markdownConverter = new();
public string Name => "HTML Processor";
public string Description => "Processes HTML content";
public HtmlProcessor()
{
markdownConverter.Config.UnknownTags = ReverseMarkdown.Config.UnknownTagsOption.Bypass;
@@ -52,11 +56,11 @@ internal partial class HtmlProcessor : IContentProcessor
htmlDoc.LoadHtml(html.ToString());
string title = htmlDoc.DocumentNode.SelectSingleNode("html/head/title")?.InnerText?.Trim() ?? httpResponseMessage.RequestMessage?.RequestUri?.ToString() ?? "Untitled";
Debug.WriteLine("Converting HTML to markdown...");
GlobalState.Logger.LogDebug("Converting HTML to markdown...");
string markdown = markdownConverter.Convert(html.ToString());
Debug.WriteLine("Processing markdown...");
GlobalState.Logger.LogDebug("Processing markdown...");
int currentLine = 0;
+28
View File
@@ -1,11 +1,17 @@
using Avalonia;
using Avalonia.ReactiveUI;
using Markdowser.Utilities;
using Projektanker.Icons.Avalonia;
using Projektanker.Icons.Avalonia.FontAwesome;
using Projektanker.Icons.Avalonia.MaterialDesign;
using ReactiveUI;
using System;
using System.Reactive;
using System.Threading.Tasks;
namespace Markdowser;
@@ -16,10 +22,32 @@ internal static class Program
// yet and stuff might break.
[STAThread]
public static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
GlobalState.Logger.LogFatal(e.ExceptionObject.ToString() ?? "Unknown error.");
};
TaskScheduler.UnobservedTaskException += (sender, e) =>
{
GlobalState.Logger.LogFatal(e.Exception.ToString());
};
RxApp.DefaultExceptionHandler = Observer.Create<Exception>(ex =>
{
GlobalState.Logger.LogFatal(ex.ToString());
});
try
{
_ = BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
catch (Exception ex)
{
GlobalState.Logger.LogFatal(ex.ToString());
}
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
+30 -3
View File
@@ -1,12 +1,39 @@
using System;
using Syroot.Windows.IO;
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Markdowser.Utilities;
internal static class Configuration
{
public static string DownloadPath => Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
public static string ApplicationDataPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(Markdowser));
public static string DownloadPath
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return KnownFolders.Downloads.Path;
}
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Downloads");
}
}
public static string ApplicationDataPath
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "StoneRed", nameof(Markdowser));
}
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StoneRed", nameof(Markdowser));
}
}
public static string SettingsFilePath => Path.Combine(ApplicationDataPath, "settings.json");
public static string LogFilePath => Path.Combine(ApplicationDataPath, $"{nameof(Markdowser)}.log");
}
+38
View File
@@ -1,5 +1,7 @@
using Avalonia.Controls;
using CuteUtils.Logging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -16,6 +18,42 @@ internal static class GlobalState
private static string url = string.Empty;
public static Logger Logger { get; } = new()
{
Config = new()
{
DebugConfig = new()
{
LogTarget = LogTarget.DebugConsole
},
InfoConfig = new()
{
LogTarget = LogTarget.DebugConsole | LogTarget.File,
FilePath = Configuration.LogFilePath
},
WarnConfig = new()
{
LogTarget = LogTarget.DebugConsole | LogTarget.File,
FilePath = Configuration.LogFilePath
},
ErrorConfig = new()
{
LogTarget = LogTarget.DebugConsole | LogTarget.File,
FilePath = Configuration.LogFilePath
},
FatalConfig = new()
{
LogTarget = LogTarget.DebugConsole | LogTarget.File,
FilePath = Configuration.LogFilePath
},
FormatConfig = new()
{
DebugConsoleFormat = new LogFormatBuilder().DateTime().Text(" ").LogSeverity(padding: -6).FilePath().Text(":").MemberName().Text(":").LineNumber().Text(Environment.NewLine).Message(),
FileFormat = new LogFormatBuilder().DateTime().Text(" ").LogSeverity(padding: -6).FilePath().Text(":").MemberName().Text(":").LineNumber().Text(Environment.NewLine).Message(),
}
}
};
public static Stack<string> BackHistory { get; } = new();
public static Stack<string> ForwardHistory { get; } = new();
+3 -2
View File
@@ -10,7 +10,6 @@ using Markdowser.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
@@ -40,7 +39,7 @@ public class HttpPathResolver : IPathResolver
relativeOrAbsolutePath = new Uri(new Uri(GlobalState.Url), relativeOrAbsolutePath).ToString();
}
Debug.WriteLine($"Resolving image: {relativeOrAbsolutePath}");
GlobalState.Logger.LogDebug($"Resolving image: {relativeOrAbsolutePath}");
HttpResponseMessage httpResponseMessage;
@@ -80,5 +79,7 @@ public class HttpPathResolver : IPathResolver
{
Dispatcher.UIThread.Post(() => vm.WindowNotificationManager.Show(new Notification("Error", message, NotificationType.Error)));
}
GlobalState.Logger.LogError(message);
}
}
@@ -1,6 +1,12 @@
namespace Markdowser.ViewModels;
using CuteUtils.Logging;
using Markdowser.Utilities;
namespace Markdowser.ViewModels;
public abstract class ContentViewModelBase(string title)
{
public string Title { get; } = title;
public Logger Logger { get; } = GlobalState.Logger;
}
@@ -12,7 +12,6 @@ using ReactiveUI;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Reflection;
@@ -48,7 +47,7 @@ public partial class MainWindowViewModel : ViewModelBase
_ = this.RaiseAndSetIfChanged(ref currentTab!, value);
Url = currentTab?.Tag?.ToString() ?? string.Empty;
Url = value?.Tag?.ToString() ?? string.Empty;
FetchUrl(true);
}
@@ -134,18 +133,32 @@ public partial class MainWindowViewModel : ViewModelBase
public ICommand CloseTab => ReactiveCommand.Create(() =>
{
if (IsBusy)
{
WindowNotificationManager.Show(new Notification("Busy", "The browser is currently busy.", NotificationType.Warning));
return;
}
if (Tabs.Count > 1)
{
int currentIndex = Tabs.IndexOf(CurrentTab);
_ = Tabs.Remove(CurrentTab);
CurrentTab = currentIndex > 0 ? Tabs[currentIndex - 1] : Tabs[0];
Tabs.RemoveAt(currentIndex);
this.RaisePropertyChanged(nameof(CloseTabEnabled));
}
});
public ICommand NewTab => ReactiveCommand.Create(() =>
{
if (IsBusy)
{
WindowNotificationManager.Show(new Notification("Busy", "The browser is currently busy.", NotificationType.Warning));
return;
}
TabItem tab = new() { Header = "New Tab", Name = Guid.NewGuid().ToString() };
Tabs.Add(tab);
CurrentTab = tab;
@@ -163,7 +176,6 @@ public partial class MainWindowViewModel : ViewModelBase
public MainWindowViewModel()
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd($"{nameof(Markdowser)}/{Assembly.GetExecutingAssembly().GetName().Version?.ToString()}");
httpClient.Timeout = TimeSpan.FromSeconds(10);
content = DefaultContent;
@@ -209,7 +221,7 @@ public partial class MainWindowViewModel : ViewModelBase
if (string.IsNullOrWhiteSpace(Url))
{
Content = DefaultContent;
Debug.WriteLine("URL is empty.");
Logger.LogDebug("URL is empty.");
CurrentTab.Header = "New Tab";
return;
}
@@ -234,12 +246,19 @@ public partial class MainWindowViewModel : ViewModelBase
}
}
httpClient.DefaultRequestHeaders.UserAgent.Clear();
if (!httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd(Settings.Current.UserAgent))
{
WindowNotificationManager.Show(new Notification("Invalid User Agent", "Failed to set user agent.", NotificationType.Error));
return;
}
IsBusy = true;
Progress = 0;
_ = Task.Run(async () =>
{
Debug.WriteLine("Fetching URL...");
Logger.LogInfo($"Fetching URL: {Url}");
try
{
@@ -279,6 +298,8 @@ public partial class MainWindowViewModel : ViewModelBase
}
Dispatcher.UIThread.Post(() => CurrentTab.Header = Content.Title);
Logger.LogInfo($"Fetched URL: {Url}");
}
catch (HttpRequestException e)
{
@@ -298,6 +319,8 @@ public partial class MainWindowViewModel : ViewModelBase
_ = errorMessage.AppendLine($"Failed to fetch URL: {e.Message}");
Content = new MarkdownContentViewModel("Error", errorMessage.ToString());
Logger.LogError(e.Message);
}
catch (Exception e)
{
@@ -308,6 +331,8 @@ public partial class MainWindowViewModel : ViewModelBase
Content = new MarkdownContentViewModel("Error", errorMessage.ToString());
Dispatcher.UIThread.Post(() => CurrentTab.Header = $"Error: {e.GetType().Name}");
Logger.LogError(e.Message);
}
finally
{
@@ -16,6 +16,12 @@ public class SettingsViewModel : ViewModelBase
set => Settings.Current.SearchEngineUrl = value;
}
public string UserAgent
{
get => Settings.Current.UserAgent;
set => Settings.Current.UserAgent = value;
}
public bool DarkMode
{
get => Settings.Current.DarkMode;
+23 -1
View File
@@ -2,10 +2,14 @@
using Avalonia.Controls.Notifications;
using Avalonia.Platform;
using CuteUtils.Logging;
using Markdowser.Models;
using Markdowser.Utilities;
using ReactiveUI;
using System.Diagnostics;
using System.Reflection;
namespace Markdowser.ViewModels;
@@ -13,12 +17,30 @@ namespace Markdowser.ViewModels;
public class ViewModelBase : ReactiveObject
{
private WindowNotificationManager? windowNotificationManager;
public string Title => $"{nameof(Markdowser)} - {Assembly.GetExecutingAssembly().GetName().Version?.ToString()}";
public string Title
{
get
{
string title = nameof(Markdowser);
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
string? version = fileVersionInfo.ProductVersion;
#if DEBUG
return $"{title} - Dev {version}";
#else
return $"{title} - {version}";
#endif
}
}
public WindowIcon Icon => Settings.Current.DarkMode ? new WindowIcon(AssetLoader.Open(new("avares://Markdowser/Assets/Markdowser-Dark-Transparent.ico"))) : new WindowIcon(AssetLoader.Open(new("avares://Markdowser/Assets/Markdowser-Light-Transparent.ico")));
public WindowNotificationManager WindowNotificationManager => windowNotificationManager!;
public Logger Logger => GlobalState.Logger;
protected internal void InitializeWindowNotificationManager(Window window)
{
TopLevel? topLevel = TopLevel.GetTopLevel(window);
@@ -26,7 +26,7 @@
</UserControl.Resources>
<Grid>
<md:MarkdownScrollViewer Margin="10 0 10 10" Markdown="{Binding Markdown}">
<md:MarkdownScrollViewer Margin="10 0 10 0" Markdown="{Binding Markdown}">
<md:MarkdownScrollViewer.Styles>
<Style Selector="ctxt|CTextBlock">
<Setter Property="Foreground" Value="{DynamicResource SemiColorText0}" />
+30 -9
View File
@@ -45,9 +45,16 @@
<Grid RowDefinitions="Auto, *, Auto">
<Grid Grid.Row="0" ColumnDefinitions="*, Auto, Auto">
<TabControl Grid.Column="0" Theme="{DynamicResource ScrollTabControl}" SelectedItem="{Binding CurrentTab, Mode=TwoWay}" ItemsSource="{Binding Tabs}" />
<Button Grid.Column="1" Command="{Binding CloseTab}" IsEnabled="{Binding CloseTabEnabled}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="32" i:Attached.Icon="fa-solid fa-xmark" />
<Button Grid.Column="2" Command="{Binding NewTab}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="32" i:Attached.Icon="fa-solid fa-plus" />
<TabControl Grid.Column="0" SelectedItem="{Binding CurrentTab, Mode=TwoWay}" ItemsSource="{Binding Tabs}" IsEnabled="{Binding !IsBusy}" />
<Button Grid.Column="1" Command="{Binding CloseTab}" HorizontalAlignment="Right" VerticalAlignment="Stretch" i:Attached.Icon="fa-solid fa-xmark">
<Button.IsEnabled>
<MultiBinding Converter="{x:Static BoolConverters.And}">
<Binding Path="CloseTabEnabled" />
<Binding Path="!IsBusy" />
</MultiBinding>
</Button.IsEnabled>
</Button>
<Button Grid.Column="2" Command="{Binding NewTab}" HorizontalAlignment="Right" VerticalAlignment="Stretch" i:Attached.Icon="fa-solid fa-plus" IsEnabled="{Binding !IsBusy}" />
</Grid>
<Grid Grid.Row="1">
@@ -57,7 +64,7 @@
<ColumnDefinition Width="*" utils:ColumnDefinition.IsVisible="{Binding ShowSidePanel}" />
</Grid.ColumnDefinitions>
<ContentControl IsEnabled="{Binding !IsBusy}" Margin="0 10 0 0" Content="{Binding Content}" />
<ContentControl IsEnabled="{Binding !IsBusy}" Content="{Binding Content}" />
<GridSplitter Grid.Column="1" IsVisible="{Binding ShowSidePanel}" Background="{DynamicResource SemiColorTertiary}" ResizeDirection="Columns" />
<TabControl Grid.Column="2" IsVisible="{Binding ShowSidePanel}" Margin="10">
@@ -80,11 +87,25 @@
</Grid>
<Border Grid.Row="2" BorderThickness="0 2 0 0" CornerRadius="0" BorderBrush="{DynamicResource SemiColorTertiary}">
<Grid IsEnabled="{Binding !IsBusy}" ColumnDefinitions="Auto, Auto, Auto, Auto, *, Auto">
<Button Grid.Column="0" i:Attached.Icon="fa-solid fa-caret-left" IsEnabled="{Binding BackEnabled}" Command="{Binding Back}" />
<Button Grid.Column="1" i:Attached.Icon="fa-solid fa-caret-right" IsEnabled="{Binding ForwardEnabled}" Command="{Binding Forward}" />
<Button Grid.Column="2" i:Attached.Icon="fa-solid fa-rotate-right" Command="{Binding Reload}" />
<Button Grid.Column="3" i:Attached.Icon="fa-solid fa-house" Command="{StaticResource HomeCommand}" />
<Grid ColumnDefinitions="Auto, Auto, Auto, Auto, *, Auto">
<Button Grid.Column="0" i:Attached.Icon="fa-solid fa-caret-left" Command="{Binding Back}">
<Button.IsEnabled>
<MultiBinding Converter="{x:Static BoolConverters.And}">
<Binding Path="BackEnabled" />
<Binding Path="!IsBusy" />
</MultiBinding>
</Button.IsEnabled>
</Button>
<Button Grid.Column="1" i:Attached.Icon="fa-solid fa-caret-right" Command="{Binding Forward}">
<Button.IsEnabled>
<MultiBinding Converter="{x:Static BoolConverters.And}">
<Binding Path="ForwardEnabled" />
<Binding Path="!IsBusy" />
</MultiBinding>
</Button.IsEnabled>
</Button>
<Button Grid.Column="2" i:Attached.Icon="fa-solid fa-rotate-right" Command="{Binding Reload}" IsEnabled="{Binding !IsBusy}" />
<Button Grid.Column="3" i:Attached.Icon="fa-solid fa-house" Command="{StaticResource HomeCommand}" IsEnabled="{Binding !IsBusy}" />
<Grid Grid.Column="4">
<TextBox IsVisible="{Binding !IsBusy}" Watermark="Search or type a URL" Text="{Binding Url}" BorderThickness="0">
+3
View File
@@ -28,5 +28,8 @@
<TextBlock Text="Home URL" />
<TextBox Text="{Binding HomeUrl}" Watermark="URL, or leave empty to use default" />
<TextBlock Text="User Agent" />
<TextBox Text="{Binding UserAgent}" Watermark="User agent string" />
</StackPanel>
</UserControl>