Skip to content

Commit

Permalink
完善UI HTML支持文件的导入
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Mar 11, 2024
1 parent 7689958 commit d74fe5e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 49 deletions.
34 changes: 23 additions & 11 deletions HTMLToNotion/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
x:Class="HTMLToNotion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:conv="clr-namespace:HTMLToNotion.Utils"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:HTMLToNotion"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
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"/>
<conv:InverseBooleanConverter x:Key="InverseBooleanConverter" />
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -88,21 +88,33 @@
<TextBlock
Margin="0,10,0,0"
FontWeight="Bold"
Text="选项" />
Text="模式" />

<StackPanel>
<RadioButton
x:Name="DocxCheckBox"
Content="导出为DOC"
IsChecked="{Binding Source={x:Static service:AppConfig.Instance}, Path=ConfigData.OutputDocx, Mode=TwoWay}" />
x:Name="DocxCheckBox"
Content="转换为Word文档"
IsChecked="{Binding Source={x:Static service:AppConfig.Instance}, Path=ConfigData.OutputDocx, Mode=TwoWay}" />
<TextBlock
Margin="29,-7,0,0"
FontSize="12"
Foreground="#FF6F6F6F"
Text="将html转为docx格式的Word文档,文档中包含图片,实现图片保存到Notion" />


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

x:Name="HtmlCheckBox"
Content="图片压缩进HTML"
IsChecked="{Binding Source={x:Static service:AppConfig.Instance}, Path=ConfigData.OutputDocx, Converter={StaticResource InverseBooleanConverter}, Mode=TwoWay}" />

<TextBlock
Margin="29,-7,0,0"
FontSize="12"
Foreground="#FF6F6F6F"
Text="将html文件中的图片压缩进.html文件中,实现图片保存到Notion" />

</StackPanel>




Expand Down
4 changes: 4 additions & 0 deletions HTMLToNotion/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Aspose.Words;
using EvernoteToNotion.Services;
using HTMLToNotion.Services;
using HTMLToNotion.Utils;
using System.Diagnostics;
Expand Down Expand Up @@ -163,6 +164,9 @@ private async void StartHTMLWithPath(string path)


//TODO
var newPath = Path.Combine(docxPath, Path.GetFileName(item) + ".html");

HtmlManager.ModifyHtml(item, newPath);

successCount++;
}
Expand Down
119 changes: 81 additions & 38 deletions HTMLToNotion/Services/HtmlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Shapes;

namespace EvernoteToNotion.Services
{
Expand All @@ -22,12 +17,16 @@ internal class HtmlImageModel
public string src;
}


internal class HtmlFileModel
{
public HtmlNode node;
public string src;
}

internal class HtmlManager
{
/// <summary>
/// 返回图片总数
/// 处理HTML文件
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
Expand All @@ -36,6 +35,12 @@ public static int ModifyHtml(string filePath, string newFilePath)
HtmlDocument doc = new HtmlDocument();
doc.Load(filePath);


if (!Directory.Exists(Path.GetDirectoryName(newFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(newFilePath));
}

//处理img标签
var nodes = doc.DocumentNode.SelectNodes("//img");
if (nodes == null)
Expand All @@ -48,44 +53,39 @@ public static int ModifyHtml(string filePath, string newFilePath)
Debug.WriteLine($"找到图片{nodes.Count}个");

List<HtmlImageModel> nodeList = GetDocAllImageLabel(nodes);
List<string> fileList = GetDocAllFileLabel(nodes);

//FixTodoListNode(path, nodes);


var docString = doc.DocumentNode.OuterHtml;

Debug.WriteLine($"处理图片");
foreach (var item in nodeList)
{

Debug.WriteLine($"处理图片 {item.src}");

byte[] imageBytes = new byte[0];

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

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

var path = Path.GetFullPath(Path.GetDirectoryName(filePath) + "/" + item.src);

imageBytes = File.ReadAllBytes(path);
}
}


if (imageBytes.Length > 0)
{
//写出
string base64ImageRepresentation = Convert.ToBase64String(imageBytes);
var image = $"data:image/jpeg;base64,{base64ImageRepresentation}";

Expand All @@ -101,6 +101,54 @@ public static int ModifyHtml(string filePath, string newFilePath)
}


}

List<HtmlFileModel> fileList = GetDocAllFileLabel(nodes);

foreach (var item in fileList)
{
Debug.WriteLine($"处理文件 {item.src}");

byte[] imageBytes = new byte[0];

try
{
if (item.src.StartsWith("http")) //网络图片则下载
{
imageBytes = item.src.GetBytesAsync().Result;
}
else
{
if (System.IO.Path.IsPathRooted(item.src))
{
imageBytes = File.ReadAllBytes(item.src);
}
else
{

var path = Path.GetFullPath(Path.GetDirectoryName(filePath) + "/" + item.src);

imageBytes = File.ReadAllBytes(path);
}
}


if (imageBytes.Length > 0)
{
string base64ImageRepresentation = Convert.ToBase64String(imageBytes);
var file = $"data:application/octet-stream;base64,{base64ImageRepresentation}";


item.node.SetAttributeValue("href", file);
item.node.SetAttributeValue("download", Path.GetFileName(item.src));
}
}
catch (Exception ex)
{

}


}

File.WriteAllText(newFilePath, doc.DocumentNode.OuterHtml);
Expand All @@ -124,21 +172,10 @@ private static List<HtmlImageModel> GetDocAllImageLabel(HtmlNodeCollection nodes
continue;
}

if (link.ParentNode.Name == "a") //上级node是a标签,文件类型,不按照图片处理;
{
continue;
}
else if (src.Value.Contains("en_todo"))
{
continue;
}
else
{
HtmlImageModel model = new HtmlImageModel();
model.src = src.Value;
model.node = link;
nodeList.Add(model);
}
HtmlImageModel model = new HtmlImageModel();
model.src = src.Value;
model.node = link;
nodeList.Add(model);
}
return nodeList;
}
Expand All @@ -148,9 +185,10 @@ private static List<HtmlImageModel> GetDocAllImageLabel(HtmlNodeCollection nodes
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static List<string> GetDocAllFileLabel(HtmlNodeCollection nodes)
private static List<HtmlFileModel> GetDocAllFileLabel(HtmlNodeCollection nodes)
{
List<string> fileList = new List<string>();
List<HtmlFileModel> nodeList = new List<HtmlFileModel>();

foreach (HtmlNode link in nodes)
{
HtmlAttribute src = link.Attributes["src"];
Expand All @@ -163,10 +201,15 @@ private static List<string> GetDocAllFileLabel(HtmlNodeCollection nodes)
{
HtmlNode parentNode = link.ParentNode;
string href = parentNode.Attributes["href"].Value; //文件路径
fileList.Add(href);


HtmlFileModel model = new HtmlFileModel();
model.src = href;
model.node = parentNode;
nodeList.Add(model);
}
}
return fileList;
return nodeList;
}

/// <summary>
Expand Down

0 comments on commit d74fe5e

Please sign in to comment.