diff --git a/src/main.rs b/src/main.rs index 2e574cd..8cc3156 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,13 +131,20 @@ fn wrap_in_html(title: &str, content: &str) -> String { {} - - -{} - - -"#, - title, content - ) -} - -fn wrap_for_pdf(title: &str, content: &str) -> String { - format!( - r#" - - - - - {} - - +
+

{}

+ + +
+
{} +
"#, - title, content + title, title, content ) } -fn export_pdf(file_path: &str, output: Option) -> Result<()> { - let path = Path::new(file_path); - let content = read_file(path)?; - let html = render_markdown(&content); - let title = path - .file_stem() - .and_then(|s| s.to_str()) - .unwrap_or("Markdown"); - - let pdf_html = wrap_for_pdf(title, &html); - - let out_path = if let Some(o) = output { - o - } else { - let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); - let out_dir = home.join("docs/pdf"); - std::fs::create_dir_all(&out_dir)?; - out_dir - .join(format!("{}.html", title)) - .to_string_lossy() - .to_string() - }; - - std::fs::write(&out_path, &pdf_html) - .with_context(|| format!("Failed to write to {}", out_path))?; - - println!("PDF export file: {}", out_path); - println!("Open this file in browser and use File > Print > Save as PDF"); - - open::that(&out_path).context("Failed to open file in browser")?; - - Ok(()) -} - fn read_file(path: &Path) -> Result { std::fs::read_to_string(path) .with_context(|| format!("Failed to read file: {}", path.display())) @@ -606,27 +511,189 @@ fn list_directory(path: &Path) -> String { files.join("\n") } -fn preview_markdown(file_path: &str, title: &str, _width: u32, _height: u32) -> Result<()> { +fn preview_markdown(file_path: &str, title: &str, width: u32, height: u32) -> Result<()> { + use tao::event_loop::{EventLoopBuilder, ControlFlow}; + use tao::window::WindowBuilder; + use tao::event::{Event, WindowEvent}; + use wry::WebViewBuilder; + + let path = Path::new(file_path); + let content = read_file(path)?; + let html_content = render_markdown(&content); + let full_html = wrap_in_html(title, &html_content); + + let event_loop = EventLoopBuilder::new().build(); + let window = WindowBuilder::new() + .with_title(title) + .with_inner_size(tao::dpi::LogicalSize::new(width as f64, height as f64)) + .build(&event_loop)?; + + let _webview = WebViewBuilder::new() + .with_html(full_html) + .build(&window)?; + + event_loop.run(move |event, _, control_flow| { + *control_flow = ControlFlow::Wait; + if let Event::WindowEvent { event: WindowEvent::CloseRequested, .. } = event { + *control_flow = ControlFlow::Exit; + } + }); +} + +fn wrap_for_pdf(title: &str, content: &str) -> String { + format!( + r#" + + + + + {} + + + + +{} + + +"#, + title, content + ) +} + +fn export_pdf(file_path: &str, output: Option) -> Result<()> { let path = Path::new(file_path); let content = read_file(path)?; let html = render_markdown(&content); - let full_html = wrap_in_html(title, &html); + let title = path + .file_stem() + .and_then(|s| s.to_str()) + .unwrap_or("Markdown"); - let temp_dir = std::env::temp_dir(); - let safe_title: String = title - .replace(' ', "_") - .chars() - .filter(|c| c.is_alphanumeric() || *c == '_' || *c == '-' || *c == '.') - .collect(); - let temp_file = temp_dir.join(format!("{}.html", safe_title)); - println!("Temp file: {}", temp_file.display()); + let pdf_html = wrap_for_pdf(title, &html); - std::fs::write(&temp_file, &full_html) - .with_context(|| format!("Failed to write temp file: {}", temp_file.display()))?; + let out_path = if let Some(o) = output { + o + } else { + let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); + let out_dir = home.join("docs/pdf"); + std::fs::create_dir_all(&out_dir)?; + out_dir + .join(format!("{}.html", title)) + .to_string_lossy() + .to_string() + }; - open::that(&temp_file).with_context(|| format!("Failed to open file in browser"))?; + std::fs::write(&out_path, &pdf_html) + .with_context(|| format!("Failed to write to {}", out_path))?; + + println!("PDF export file: {}", out_path); + println!("Open this file in browser and use File > Print > Save as PDF"); + + open::that(&out_path).context("Failed to open file in browser")?; - println!("Opening preview in browser... (close browser window to exit)"); Ok(()) } @@ -677,7 +744,11 @@ fn main() { std::process::exit(1); } } - Commands::Export { file, output, format } => { + Commands::Export { + file, + output, + format, + } => { if format == "pdf" { if let Err(e) = export_pdf(&file, Some(output)) { eprintln!("Export error: {}", e);