Skip to content

Commit

Permalink
功能优化
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Feb 9, 2024
1 parent 7865abe commit 7689958
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 33 deletions.
24 changes: 20 additions & 4 deletions HTMLToNotion/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
xmlns:service="clr-namespace:HTMLToNotion.Services"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:views="clr-namespace:HTMLToNotion.Views"
xmlns:conv="clr-namespace:HTMLToNotion.Utils"
Title="HTMLToNotion"
Width="550"
Height="300"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<Window.Resources>
<conv:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="941*" />
Expand Down Expand Up @@ -85,10 +89,22 @@
Margin="0,10,0,0"
FontWeight="Bold"
Text="选项" />
<CheckBox
x:Name="OverwriteCheckBox"
Content="覆盖文件"
IsChecked="{Binding Source={x:Static service:AppConfig.Instance}, Path=ConfigData.Overwrite, Mode=TwoWay}" />

<StackPanel>
<RadioButton
x:Name="DocxCheckBox"
Content="导出为DOC"
IsChecked="{Binding Source={x:Static service:AppConfig.Instance}, Path=ConfigData.OutputDocx, Mode=TwoWay}" />

<RadioButton
x:Name="HtmlCheckBox"
Content="导出为HTML"
IsChecked="{Binding Source={x:Static service:AppConfig.Instance}, Path=ConfigData.OutputDocx, Converter={StaticResource InverseBooleanConverter}, Mode=TwoWay}" />

</StackPanel>




<ui:Button
x:Name="ButtonStart"
Expand Down
10 changes: 9 additions & 1 deletion HTMLToNotion/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ private async void ButtonStart_Click(object sender, RoutedEventArgs e)


await Task.Run(() => {
StartDocXWithPath(path);
if (AppConfig.Instance.ConfigData.OutputDocx)
{
StartDocXWithPath(path);
}
else
{
StartHTMLWithPath(path);
}

});


Expand Down
3 changes: 2 additions & 1 deletion HTMLToNotion/Services/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public partial class AppConfigData : INotifyPropertyChanged

public string LastPath { get; set; } = string.Empty;

public bool Overwrite { get; set; }

public bool OutputDocx { get; set; }

}

Expand Down
63 changes: 36 additions & 27 deletions HTMLToNotion/Services/HtmlManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Flurl.Http;
using Aspose.Words.Drawing;
using Flurl.Http;
using HtmlAgilityPack;
using HTMLToNotion.Services;
using System;
Expand All @@ -9,6 +10,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Shapes;

namespace EvernoteToNotion.Services
Expand All @@ -29,7 +31,7 @@ internal class HtmlManager
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static int UploadHtmlData(string filePath, string newFilePath)
public static int ModifyHtml(string filePath, string newFilePath)
{
HtmlDocument doc = new HtmlDocument();
doc.Load(filePath);
Expand All @@ -55,46 +57,53 @@ public static int UploadHtmlData(string filePath, string newFilePath)
Debug.WriteLine($"处理图片");
foreach (var item in nodeList)
{
byte[] imageBytes = new byte[0];

if (item.src.StartsWith("http"))
{
//网络图片
imageBytes = item.src.GetBytesAsync().Result;

}
else
byte[] imageBytes = new byte[0];

try
{
//本地
if (System.IO.Path.IsPathRooted(item.src))
if (item.src.StartsWith("http"))
{
imageBytes = File.ReadAllBytes(item.src);
//网络图片
imageBytes = item.src.GetBytesAsync().Result;

}
else
{
imageBytes = File.ReadAllBytes(System.IO.Path.GetDirectoryName(filePath) + "/" + item.src);
//本地
if (System.IO.Path.IsPathRooted(item.src))
{
imageBytes = File.ReadAllBytes(item.src);
}
else
{
imageBytes = File.ReadAllBytes(System.IO.Path.GetDirectoryName(filePath) + "/" + item.src);
}
}
}

//TODO

//Debug.WriteLine($"处理图片{Path.GetDirectoryName(filePath) + "/" + item}");
//var imageBytes = File.ReadAllBytes(Path.GetDirectoryName(filePath) + "/" + item);
if (imageBytes.Length > 0)
{
//写出
string base64ImageRepresentation = Convert.ToBase64String(imageBytes);
var image = $"data:image/jpeg;base64,{base64ImageRepresentation}";

//string base64ImageRepresentation = Convert.ToBase64String(imageBytes);
//var image = $"data:image/jpeg;base64,{base64ImageRepresentation}";
Bitmap bitmap = new Bitmap(new MemoryStream(imageBytes));

//docString = docString.Replace(item, image);
item.node.SetAttributeValue("src", image);
item.node.SetAttributeValue("style", $"width: {bitmap.Width}px; height: {bitmap.Height}px;");
}
}
catch (Exception ex)
{

}


}

//上传所有图片并替换到html文件中
// var docString = UploadAllImage(imageList, path, doc.DocumentNode.OuterHtml);

//a标签判定为文件类型,不上传
//docString = UploadAllImage(fileList, path, docString);

File.WriteAllText(newFilePath, docString);
File.WriteAllText(newFilePath, doc.DocumentNode.OuterHtml);
return nodeList.Count;

}
Expand Down
31 changes: 31 additions & 0 deletions HTMLToNotion/Utils/InverseBooleanConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace HTMLToNotion.Utils
{
public class InverseBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool booleanValue)
{
return !booleanValue;
}
return value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool booleanValue)
{
return !booleanValue;
}
return value;
}
}
}

0 comments on commit 7689958

Please sign in to comment.