Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Maniceraf committed Jun 13, 2024
1 parent 5ef9781 commit 0637ae9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
4 changes: 4 additions & 0 deletions EasyHtmlToolkit.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public static void Example1()

doc.Body.AddChild(table);

doc.AddStyleToHead(@"table, th, td {

Check failure on line 56 in EasyHtmlToolkit.Test/Program.cs

View workflow job for this annotation

GitHub Actions / build

'HtmlDocument' does not contain a definition for 'AddStyleToHead' and no accessible extension method 'AddStyleToHead' accepting a first argument of type 'HtmlDocument' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 56 in EasyHtmlToolkit.Test/Program.cs

View workflow job for this annotation

GitHub Actions / build

'HtmlDocument' does not contain a definition for 'AddStyleToHead' and no accessible extension method 'AddStyleToHead' accepting a first argument of type 'HtmlDocument' could be found (are you missing a using directive or an assembly reference?)
border: 1px solid black;
}");

doc.SaveToFile1("D:\\", "test1.html");
}

Expand Down
57 changes: 57 additions & 0 deletions EasyHtmlToolkit/Extensions/FileMinimizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Text.RegularExpressions;

namespace EasyHtmlToolkit.Extensions
{
public class FileMinimizer
{
public string GetMinimizedFileContent(string filePath)
{
try
{
// Read the file content
string fileContent = File.ReadAllText(filePath);

// Minimize the file content (remove comments, unnecessary whitespace, etc.)
string minimizedContent = MinimizeContent(fileContent);

return minimizedContent;
}
catch (Exception ex)
{
Console.WriteLine($"Error reading or minimizing file: {ex.Message}");
return null;

Check warning on line 22 in EasyHtmlToolkit/Extensions/FileMinimizer.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 22 in EasyHtmlToolkit/Extensions/FileMinimizer.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}
}

private string MinimizeContent(string content)
{
// Remove comments (HTML, CSS, JS) and unnecessary whitespace
content = RemoveComments(content);
content = RemoveWhitespace(content);

return content;
}

private string RemoveComments(string content)
{
// Remove HTML comments: <!-- ... -->
content = Regex.Replace(content, @"<!--(.*?)-->", "", RegexOptions.Singleline);

// Remove CSS comments: /* ... */
content = Regex.Replace(content, @"/\*(.*?)\*/", "", RegexOptions.Singleline);

// Remove JavaScript comments: // ... or /* ... */
content = Regex.Replace(content, @"\/\/(.+?)\n|\/\*(.*?)\*\/", "", RegexOptions.Singleline);

return content;
}

private string RemoveWhitespace(string content)
{
// Remove unnecessary whitespace using regex
content = Regex.Replace(content, @"\s+", " ");

return content;
}
}
}
4 changes: 1 addition & 3 deletions EasyHtmlToolkit/HtmlDocument.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using EasyHtmlToolkit.Models;
using System.Net.Http;
using System.Reflection.Metadata;
using System.Text;

namespace EasyHtmlToolkit
Expand All @@ -26,7 +24,7 @@ public HtmlDocument()

#region Public Methods

public void AddStyleToHead(string css)
public void AddStyle(string css)
{
var styleElement = new HtmlElement("style");
styleElement.InnerText = css;
Expand Down

0 comments on commit 0637ae9

Please sign in to comment.