diff --git a/HTMLToNotion/MainWindow.xaml b/HTMLToNotion/MainWindow.xaml index ed508b0..8f4fac3 100644 --- a/HTMLToNotion/MainWindow.xaml +++ b/HTMLToNotion/MainWindow.xaml @@ -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"> - + @@ -88,21 +88,33 @@ + Text="模式" /> + x:Name="DocxCheckBox" + Content="转换为Word文档" + IsChecked="{Binding Source={x:Static service:AppConfig.Instance}, Path=ConfigData.OutputDocx, Mode=TwoWay}" /> + + - + x:Name="HtmlCheckBox" + Content="图片压缩进HTML" + IsChecked="{Binding Source={x:Static service:AppConfig.Instance}, Path=ConfigData.OutputDocx, Converter={StaticResource InverseBooleanConverter}, Mode=TwoWay}" /> + + + - + diff --git a/HTMLToNotion/MainWindow.xaml.cs b/HTMLToNotion/MainWindow.xaml.cs index adafaf5..9b6f627 100644 --- a/HTMLToNotion/MainWindow.xaml.cs +++ b/HTMLToNotion/MainWindow.xaml.cs @@ -1,4 +1,5 @@ using Aspose.Words; +using EvernoteToNotion.Services; using HTMLToNotion.Services; using HTMLToNotion.Utils; using System.Diagnostics; @@ -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++; } diff --git a/HTMLToNotion/Services/HtmlManager.cs b/HTMLToNotion/Services/HtmlManager.cs index 9d35f62..b2b5045 100644 --- a/HTMLToNotion/Services/HtmlManager.cs +++ b/HTMLToNotion/Services/HtmlManager.cs @@ -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 { @@ -22,12 +17,16 @@ internal class HtmlImageModel public string src; } - + internal class HtmlFileModel + { + public HtmlNode node; + public string src; + } internal class HtmlManager { /// - /// 返回图片总数 + /// 处理HTML文件 /// /// /// @@ -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) @@ -48,44 +53,39 @@ public static int ModifyHtml(string filePath, string newFilePath) Debug.WriteLine($"找到图片{nodes.Count}个"); List nodeList = GetDocAllImageLabel(nodes); - List 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}"; @@ -101,6 +101,54 @@ public static int ModifyHtml(string filePath, string newFilePath) } + } + + List 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); @@ -124,21 +172,10 @@ private static List 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; } @@ -148,9 +185,10 @@ private static List GetDocAllImageLabel(HtmlNodeCollection nodes /// /// /// - private static List GetDocAllFileLabel(HtmlNodeCollection nodes) + private static List GetDocAllFileLabel(HtmlNodeCollection nodes) { - List fileList = new List(); + List nodeList = new List(); + foreach (HtmlNode link in nodes) { HtmlAttribute src = link.Attributes["src"]; @@ -163,10 +201,15 @@ private static List 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; } ///