84 lines
2.4 KiB
Rust
84 lines
2.4 KiB
Rust
|
|
//use std::io::Write;
|
||
|
|
|
||
|
|
use pulldown_cmark::{html, Parser};
|
||
|
|
|
||
|
|
|
||
|
|
pub enum Converter {}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
impl Converter
|
||
|
|
{
|
||
|
|
/*
|
||
|
|
/// Public function to handle file-to-file conversion
|
||
|
|
pub fn md_to_html(markdown_path: &std::path::Path,
|
||
|
|
html_path: &std::path::Path)
|
||
|
|
-> std::io::Result<()>
|
||
|
|
{
|
||
|
|
let markdown_content = std::fs::read_to_string(markdown_path)?;
|
||
|
|
let html_output = Self::markdown_to_html(&markdown_content);
|
||
|
|
let mut html_file = std::fs::File::create(html_path)?;
|
||
|
|
|
||
|
|
html_file.write_all(html_output.as_bytes())?;
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
*/
|
||
|
|
|
||
|
|
/// Private function to handle the core conversion logic
|
||
|
|
pub fn markdown_to_html(markdown_content: &str) -> String
|
||
|
|
{
|
||
|
|
let parser = Parser::new(markdown_content);
|
||
|
|
let mut html_output = String::new();
|
||
|
|
html::push_html(&mut html_output, parser);
|
||
|
|
html_output
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// tests/integration_test.rs
|
||
|
|
// This file would be in your project's `tests` directory.
|
||
|
|
// It tests the public functions of your main program.
|
||
|
|
// You will also need to add `use crate::*` to access the functions.
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests
|
||
|
|
{
|
||
|
|
use std::fs;
|
||
|
|
use std::io::Write;
|
||
|
|
use std::path::Path;
|
||
|
|
|
||
|
|
use super::*; // Import the parent module's items
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_markdown_conversion_with_files()
|
||
|
|
{
|
||
|
|
let test_dir = Path::new("temp_test_dir");
|
||
|
|
let temp_md_path = test_dir.join("test.md");
|
||
|
|
let temp_html_path = test_dir.join("test.html");
|
||
|
|
|
||
|
|
// Ensure the test directory is clean
|
||
|
|
if test_dir.exists()
|
||
|
|
{
|
||
|
|
fs::remove_dir_all(&test_dir).unwrap();
|
||
|
|
}
|
||
|
|
fs::create_dir(&test_dir).unwrap();
|
||
|
|
|
||
|
|
// Create a temporary Markdown file for the test
|
||
|
|
let markdown_content = "# Hello, World!\n\nThis is a **test**.";
|
||
|
|
let expected_html =
|
||
|
|
"<h1>Hello, World!</h1>\n<p>This is a <strong>test</strong>.</p>\n";
|
||
|
|
|
||
|
|
let mut temp_md_file = fs::File::create(&temp_md_path).unwrap();
|
||
|
|
temp_md_file.write_all(markdown_content.as_bytes()).unwrap();
|
||
|
|
|
||
|
|
// Use the new Converter API
|
||
|
|
Converter::md_to_html(&temp_md_path, &temp_html_path).unwrap();
|
||
|
|
let converted_html = fs::read_to_string(&temp_html_path).unwrap();
|
||
|
|
|
||
|
|
// Assert that the output matches the expected HTML
|
||
|
|
assert_eq!(converted_html.trim(), expected_html.trim());
|
||
|
|
|
||
|
|
// Clean up the temporary directory after the test
|
||
|
|
fs::remove_dir_all(&test_dir).unwrap();
|
||
|
|
}
|
||
|
|
}
|