Files
tavernworks/src/converter.rs
Myrddin Dundragon ab83c7afb6 [#2] Database integration complete.
Posts and Authors can now be inserted and retrieved from the database
created. It was decided to use a SQLite database for it high read spead
and ease of use/maintenance.

A build feature was created to seperate how the library is being used.
If you are making the database and storing posts, then use the publisher
flag. If you are just reading from a database then do not use the
publisher flag. This was also set to change the tale contents from a
PathBuf to the String of HTML blog data without having to create a whole
new data object.

An example and a test were made. Test coverage needs to be increased
however.
2025-08-27 17:36:06 -04:00

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();
}
}