html_generator/
performance.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! Performance-related functionality for HTML processing.
//!
//! This module provides functions for minifying HTML and generating HTML from Markdown, with a focus on performance and efficiency.

use crate::{HtmlError, Result};
use comrak::{markdown_to_html, ComrakOptions};
use minify_html::{minify, Cfg};
use std::{fs, path::Path};
use tokio::task;

/// Returns a default `Cfg` for HTML minification.
///
/// This helper function creates a default configuration for minifying HTML
/// with pre-set options for CSS, JS, and attributes.
///
/// # Returns
/// A `Cfg` object containing the default minification settings.
fn default_minify_cfg() -> Cfg {
    let mut cfg = Cfg::new();
    cfg.do_not_minify_doctype = true;
    cfg.ensure_spec_compliant_unquoted_attribute_values = true;
    cfg.keep_closing_tags = true;
    cfg.keep_html_and_head_opening_tags = true;
    cfg.keep_spaces_between_attributes = true;
    cfg.keep_comments = false;
    cfg.minify_css = true;
    cfg.minify_js = true;
    cfg.remove_bangs = true;
    cfg.remove_processing_instructions = true;
    cfg
}

/// Minifies a single HTML file.
///
/// This function takes a reference to a `Path` object for an HTML file and
/// returns a string containing the minified HTML.
///
/// # Arguments
///
/// * `file_path` - A reference to a `Path` object for the HTML file.
///
/// # Returns
///
/// * `Result<String, HtmlError>` - A result containing a string
///    containing the minified HTML.
///
/// # Examples
///
/// ```no_run
/// use std::path::Path;
/// use html_generator::performance::minify_html;
///
/// let path = Path::new("index.html");
/// match minify_html(path) {
///     Ok(minified) => println!("Minified HTML: {}", minified),
///     Err(e) => eprintln!("Error: {}", e),
/// }
/// ```
pub fn minify_html(file_path: &Path) -> Result<String> {
    // Read the file content
    let content = fs::read_to_string(file_path).map_err(|e| {
        HtmlError::MinificationError(format!(
            "Failed to read file: {}",
            e
        ))
    })?;

    // Minify the content
    let minified_content =
        minify(content.as_bytes(), &default_minify_cfg());

    // Convert the minified content back to a UTF-8 string
    String::from_utf8(minified_content).map_err(|e| {
        HtmlError::MinificationError(format!(
            "Invalid UTF-8 in minified content: {}",
            e
        ))
    })
}

/// Asynchronously generate HTML from Markdown.
///
/// This function converts a Markdown string into an HTML string using
/// Comrak, a CommonMark-compliant Markdown parser and renderer.
/// The conversion is performed in a separate thread to avoid blocking.
///
/// # Arguments
///
/// * `markdown` - A reference to a Markdown string.
///
/// # Returns
///
/// * `Result<String, HtmlError>` - A result containing a string with the
///   generated HTML.
///
/// # Examples
///
/// ```
/// use html_generator::performance::async_generate_html;
///
/// #[tokio::main]
/// async fn main() {
///     let markdown = "# Hello\n\nThis is a test.";
///     match async_generate_html(markdown).await {
///         Ok(html) => println!("Generated HTML: {}", html),
///         Err(e) => eprintln!("Error: {}", e),
///     }
/// }
/// ```
pub async fn async_generate_html(markdown: &str) -> Result<String> {
    let markdown = markdown.to_string();
    task::spawn_blocking(move || {
        let options = ComrakOptions::default();
        Ok(markdown_to_html(&markdown, &options))
    })
    .await
    .map_err(|e| HtmlError::MarkdownConversionError(e.to_string()))?
}

/// Synchronously generate HTML from Markdown.
///
/// This function converts a Markdown string into an HTML string using
/// Comrak, a CommonMark-compliant Markdown parser and renderer.
///
/// # Arguments
///
/// * `markdown` - A reference to a Markdown string.
///
/// # Returns
///
/// * `Result<String, HtmlError>` - A result containing a string with the
///   generated HTML.
///
/// # Examples
///
/// ```
/// use html_generator::performance::generate_html;
///
/// let markdown = "# Hello\n\nThis is a test.";
/// match generate_html(markdown) {
///     Ok(html) => println!("Generated HTML: {}", html),
///     Err(e) => eprintln!("Error: {}", e),
/// }
/// ```
pub fn generate_html(markdown: &str) -> Result<String> {
    let options = ComrakOptions::default();
    Ok(markdown_to_html(markdown, &options))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;

    /// Helper function to create an HTML file for testing.
    fn create_html_file(file_path: &Path, content: &str) {
        let mut file = File::create(file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();
    }

    #[test]
    fn test_minify_html_basic() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.html");
        let html = "<html>  <body>    <p>Test</p>  </body>  </html>";

        create_html_file(&file_path, html);

        let result = minify_html(&file_path);
        assert!(result.is_ok());
        assert_eq!(
            result.unwrap(),
            "<html><body><p>Test</p></body></html>"
        );
    }

    #[test]
    fn test_minify_html_with_comments() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test_comments.html");
        let html = "<html>  <body>    <!-- This is a comment -->    <p>Test</p>  </body>  </html>";

        create_html_file(&file_path, html);

        let result = minify_html(&file_path);
        assert!(result.is_ok());
        assert_eq!(
            result.unwrap(),
            "<html><body><p>Test</p></body></html>"
        );
    }

    #[test]
    fn test_minify_html_with_css() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test_css.html");
        let html = "<html><head><style>  body  {  color:  red;  }  </style></head><body><p>Test</p></body></html>";

        create_html_file(&file_path, html);

        let result = minify_html(&file_path);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "<html><head><style>body{color:red}</style></head><body><p>Test</p></body></html>");
    }

    #[test]
    fn test_minify_html_with_js() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test_js.html");
        let html = "<html><head><script>  function  test()  {  console.log('Hello');  }  </script></head><body><p>Test</p></body></html>";

        create_html_file(&file_path, html);

        let result = minify_html(&file_path);
        assert!(result.is_ok());
        let minified = result.unwrap();
        assert!(minified.contains("<script>"));
        assert!(minified.contains("console.log"));
        assert!(minified.contains("Hello"));
        assert!(minified.contains("<p>Test</p>"));
    }

    #[test]
    fn test_minify_html_non_existent_file() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("non_existent.html");

        let result = minify_html(&file_path);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            HtmlError::MinificationError(_)
        ));
    }

    #[test]
    fn test_minify_html_invalid_utf8() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("invalid_utf8.html");
        let invalid_utf8 = vec![0, 159, 146, 150]; // Invalid UTF-8 sequence

        let mut file = File::create(&file_path).unwrap();
        file.write_all(&invalid_utf8).unwrap();

        let result = minify_html(&file_path);

        assert!(
            result.is_err(),
            "Expected an error due to invalid UTF-8 sequence"
        );
        assert!(matches!(
            result.unwrap_err(),
            HtmlError::MinificationError(_)
        ));
    }

    #[test]
    fn test_generate_html_basic() {
        let markdown = "# Test\n\nThis is a test.";
        let result = generate_html(markdown);
        assert!(result.is_ok());
        let html = result.unwrap();
        assert!(html.contains("<h1>Test</h1>"));
        assert!(html.contains("<p>This is a test.</p>"));
    }

    #[test]
    fn test_generate_html_complex() {
        let markdown = "# Header\n\n## Subheader\n\n- List item 1\n- List item 2\n\n```rust\nfn main() {\n    println!(\"Hello, world!\");\n}\n```";
        let result = generate_html(markdown);
        assert!(result.is_ok());
        let html = result.unwrap();
        assert!(html.contains("<h1>Header</h1>"));
        assert!(html.contains("<h2>Subheader</h2>"));
        assert!(html.contains("<ul>"));
        assert!(html.contains("<li>List item 1</li>"));
        assert!(html.contains("<li>List item 2</li>"));
        assert!(html.contains("<pre><code class=\"language-rust\">"));
        assert!(html.contains("fn main()"));
        assert!(html.contains("println!"));
        assert!(html.contains("Hello, world!"));
    }

    #[test]
    fn test_generate_html_empty_input() {
        let markdown = "";
        let result = generate_html(markdown);
        assert!(result.is_ok());
        let html = result.unwrap();
        assert!(html.trim().is_empty());
    }
}