Compare commits
	
		
			No commits in common. "4dbf9393170ad7f57605b2f1dfecb8865d3ac8f8" and "c4bf81cdb771497735220df56b364a074d369f0f" have entirely different histories.
		
	
	
		
			4dbf939317
			...
			c4bf81cdb7
		
	
		
							
								
								
									
										2362
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2362
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										17
									
								
								Cargo.toml
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								Cargo.toml
									
									
									
									
									
								
							@ -3,23 +3,14 @@ name = "md2pdf"
 | 
			
		||||
version = "0.0.1"
 | 
			
		||||
authors = ["Thomas Forgione <thomas@forgione.fr>"]
 | 
			
		||||
description = "A small utility to convert markdown files to pdf exploiting tectonic."
 | 
			
		||||
edition = "2021"
 | 
			
		||||
edition = "2018"
 | 
			
		||||
license = "MIT"
 | 
			
		||||
repository = "https://gitea.tforgione.fr/tforgione/md2pdf"
 | 
			
		||||
readme = "README.md"
 | 
			
		||||
cargo = "1.64"
 | 
			
		||||
 | 
			
		||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
tectonic = "0.5.2"
 | 
			
		||||
pulldown-cmark = "0.5.3"
 | 
			
		||||
clap = { version = "4.0.7", features = ["cargo"] }
 | 
			
		||||
 | 
			
		||||
[lib]
 | 
			
		||||
name = "md2pdf"
 | 
			
		||||
path = "src/lib.rs"
 | 
			
		||||
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "md2pdf"
 | 
			
		||||
path = "src/main.rs"
 | 
			
		||||
tectonic = "0.1.11"
 | 
			
		||||
pulldown-cmark = "0.5.2"
 | 
			
		||||
clap = "2.33.0"
 | 
			
		||||
 | 
			
		||||
@ -82,7 +82,7 @@ pub fn markdown_to_latex(markdown: String) -> String {
 | 
			
		||||
            Event::Start(Tag::Image(_, path, title)) => {
 | 
			
		||||
                output.push_str("\\begin{figure}\n");
 | 
			
		||||
                output.push_str("\\centering\n");
 | 
			
		||||
                output.push_str("\\includegraphics[width=\\textwidth]{");
 | 
			
		||||
                output.push_str("\\includegraphics[width=\\textwidth]{");;
 | 
			
		||||
                output.push_str(&*path);
 | 
			
		||||
                output.push_str("}\n");
 | 
			
		||||
                output.push_str("\\caption{");
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										72
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										72
									
								
								src/main.rs
									
									
									
									
									
								
							@ -1,10 +1,8 @@
 | 
			
		||||
use std::fs::File;
 | 
			
		||||
use std::io::{Read, Write};
 | 
			
		||||
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, App, Arg};
 | 
			
		||||
 | 
			
		||||
use md2pdf::{markdown_to_latex, markdown_to_pdf};
 | 
			
		||||
 | 
			
		||||
@ -22,71 +20,41 @@ macro_rules! unwrap {
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
 | 
			
		||||
    let matches = Command::new(crate_name!())
 | 
			
		||||
    let matches = App::new(crate_name!())
 | 
			
		||||
        .bin_name(crate_name!())
 | 
			
		||||
        .version(crate_version!())
 | 
			
		||||
        .author(crate_authors!("\n"))
 | 
			
		||||
        .about(crate_description!())
 | 
			
		||||
        .arg(Arg::new("INPUT")
 | 
			
		||||
        .arg(Arg::with_name("INPUT")
 | 
			
		||||
             .long("input")
 | 
			
		||||
             .short('i')
 | 
			
		||||
             .short("i")
 | 
			
		||||
             .help("Input markdown files")
 | 
			
		||||
             .required(true)
 | 
			
		||||
             .value_parser(clap::value_parser!(PathBuf))
 | 
			
		||||
            )
 | 
			
		||||
        .arg(Arg::new("OUTPUT")
 | 
			
		||||
             .takes_value(true))
 | 
			
		||||
        .arg(Arg::with_name("OUTPUT")
 | 
			
		||||
             .long("output")
 | 
			
		||||
             .short('o')
 | 
			
		||||
             .short("o")
 | 
			
		||||
             .help("Output tex or pdf file")
 | 
			
		||||
             .required(true)
 | 
			
		||||
             .value_parser(clap::value_parser!(PathBuf))
 | 
			
		||||
            )
 | 
			
		||||
             .takes_value(true))
 | 
			
		||||
        .get_matches();
 | 
			
		||||
 | 
			
		||||
    let input_path = matches.get_one::<PathBuf>("INPUT").unwrap();
 | 
			
		||||
    let mut content = String::new();
 | 
			
		||||
    let mut input = unwrap!(File::open(input_path), "couldn't open input file");
 | 
			
		||||
    let mut input = unwrap!(File::open(matches.value_of("INPUT").unwrap()), "couldn't open input file");
 | 
			
		||||
    unwrap!(input.read_to_string(&mut content), "couldn't read file content");
 | 
			
		||||
 | 
			
		||||
    let output_path = matches.get_one::<PathBuf>("OUTPUT").unwrap();
 | 
			
		||||
    let output_path_ext = output_path.extension().and_then(OsStr::to_str);
 | 
			
		||||
    let output_path = matches.value_of("OUTPUT").unwrap();
 | 
			
		||||
    let mut output = unwrap!(File::create(output_path), "couldn't open output file");
 | 
			
		||||
 | 
			
		||||
    match output_path_ext {
 | 
			
		||||
        Some("tex") => {
 | 
			
		||||
            let tex = markdown_to_latex(content);
 | 
			
		||||
            unwrap!(output.write(tex.as_bytes()), "couldn't write output file");
 | 
			
		||||
        },
 | 
			
		||||
        Some("pdf") => {
 | 
			
		||||
            match markdown_to_pdf(content) {
 | 
			
		||||
                Ok(data) => {
 | 
			
		||||
                    match output.write(&data) {
 | 
			
		||||
                        Ok(_) => {
 | 
			
		||||
                            exit(0);
 | 
			
		||||
                        },
 | 
			
		||||
                        Err(error) => {
 | 
			
		||||
                            eprintln!(
 | 
			
		||||
                                "error while writing file: {}", error
 | 
			
		||||
                            );
 | 
			
		||||
                            exit(1);
 | 
			
		||||
                        },
 | 
			
		||||
                    }
 | 
			
		||||
                },
 | 
			
		||||
                Err(error) => {
 | 
			
		||||
                    eprintln!(
 | 
			
		||||
                        "error while compiling latex: {}", error.description()
 | 
			
		||||
                    );
 | 
			
		||||
                    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);
 | 
			
		||||
        },
 | 
			
		||||
    if output_path.ends_with(".tex") {
 | 
			
		||||
        let tex = markdown_to_latex(content);
 | 
			
		||||
        unwrap!(output.write(tex.as_bytes()), "couldn't write output file");
 | 
			
		||||
    } else if output_path.ends_with(".pdf") {
 | 
			
		||||
        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");
 | 
			
		||||
    } else {
 | 
			
		||||
        eprintln!("unknown file format for output: {}", output_path);
 | 
			
		||||
        exit(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user