Fix extension detection by using the extension function
This commit is contained in:
		
							parent
							
								
									aed79f467b
								
							
						
					
					
						commit
						c0bc67d9ca
					
				
							
								
								
									
										38
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								src/main.rs
									
									
									
									
									
								
							@ -1,6 +1,8 @@
 | 
				
			|||||||
use std::fs::File;
 | 
					use std::fs::File;
 | 
				
			||||||
use std::io::{Read, Write};
 | 
					use std::io::{Read, Write};
 | 
				
			||||||
use std::process::exit;
 | 
					use std::process::exit;
 | 
				
			||||||
 | 
					use std::path::PathBuf;
 | 
				
			||||||
 | 
					use std::ffi::OsStr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use clap::{crate_authors, crate_description, crate_name, crate_version, Command, Arg};
 | 
					use clap::{crate_authors, crate_description, crate_name, crate_version, Command, Arg};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -30,34 +32,42 @@ fn main() {
 | 
				
			|||||||
             .short('i')
 | 
					             .short('i')
 | 
				
			||||||
             .help("Input markdown files")
 | 
					             .help("Input markdown files")
 | 
				
			||||||
             .required(true)
 | 
					             .required(true)
 | 
				
			||||||
             .value_parser(clap::value_parser!(std::path::PathBuf))
 | 
					             .value_parser(clap::value_parser!(PathBuf))
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
        .arg(Arg::new("OUTPUT")
 | 
					        .arg(Arg::new("OUTPUT")
 | 
				
			||||||
             .long("output")
 | 
					             .long("output")
 | 
				
			||||||
             .short('o')
 | 
					             .short('o')
 | 
				
			||||||
             .help("Output tex or pdf file")
 | 
					             .help("Output tex or pdf file")
 | 
				
			||||||
             .required(true)
 | 
					             .required(true)
 | 
				
			||||||
             .value_parser(clap::value_parser!(std::path::PathBuf))
 | 
					             .value_parser(clap::value_parser!(PathBuf))
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
        .get_matches();
 | 
					        .get_matches();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let input_path = matches.get_one::<std::path::PathBuf>("INPUT").unwrap();
 | 
					    let input_path = matches.get_one::<PathBuf>("INPUT").unwrap();
 | 
				
			||||||
    let mut content = String::new();
 | 
					    let mut content = String::new();
 | 
				
			||||||
    let mut input = unwrap!(File::open(input_path), "couldn't open input file");
 | 
					    let mut input = unwrap!(File::open(input_path), "couldn't open input file");
 | 
				
			||||||
    unwrap!(input.read_to_string(&mut content), "couldn't read file content");
 | 
					    unwrap!(input.read_to_string(&mut content), "couldn't read file content");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let output_path = matches.get_one::<std::path::PathBuf>("OUTPUT").unwrap();
 | 
					    let output_path = matches.get_one::<PathBuf>("OUTPUT").unwrap();
 | 
				
			||||||
 | 
					    let output_path_ext = output_path.extension().and_then(OsStr::to_str);
 | 
				
			||||||
    let mut output = unwrap!(File::create(output_path), "couldn't open output file");
 | 
					    let mut output = unwrap!(File::create(output_path), "couldn't open output file");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if output_path.ends_with(".tex") {
 | 
					    match output_path_ext {
 | 
				
			||||||
        let tex = markdown_to_latex(content);
 | 
					        Some("tex") => {
 | 
				
			||||||
        unwrap!(output.write(tex.as_bytes()), "couldn't write output file");
 | 
					            let tex = markdown_to_latex(content);
 | 
				
			||||||
    } else if output_path.ends_with(".pdf") {
 | 
					            unwrap!(output.write(tex.as_bytes()), "couldn't write output file");
 | 
				
			||||||
        let data = unwrap!(markdown_to_pdf(content), "error while compiling latex, this is most likely a bug");
 | 
					        },
 | 
				
			||||||
        unwrap!(output.write(&data), "coudln't write output file");
 | 
					        Some("pdf") => {
 | 
				
			||||||
    } else {
 | 
					            let data = unwrap!(markdown_to_pdf(content), "error while compiling latex, this is most likely a bug");
 | 
				
			||||||
        eprintln!("unknown file format for output: {}", output_path.display());
 | 
					            unwrap!(output.write(&data), "couldn't write output file");
 | 
				
			||||||
        exit(1);
 | 
					        },
 | 
				
			||||||
 | 
					        Some(ext) => {
 | 
				
			||||||
 | 
					            eprintln!("unknown file format ({}) for output: {}", ext, output_path.display());
 | 
				
			||||||
 | 
					            exit(1);
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        None => {
 | 
				
			||||||
 | 
					            eprintln!("unknown file format for output: {}", output_path.display());
 | 
				
			||||||
 | 
					            exit(1);
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user