mediawiki/pandoc: call directly instead of via lib
Pandoc crate is confusing and badly documented. Using std::process::command now to avoid temporary files.
This commit is contained in:
		
							parent
							
								
									3608838949
								
							
						
					
					
						commit
						d3681e1699
					
				
					 2 changed files with 34 additions and 11 deletions
				
			
		|  | @ -5,7 +5,6 @@ edition = "2021" | |||
| 
 | ||||
| [dependencies] | ||||
| stdext = "0.3.3" | ||||
| pandoc = "0.8" | ||||
| chrono = "0.4.38" | ||||
| regex = "1.10.5" | ||||
| futures = "0.3.30" | ||||
|  |  | |||
|  | @ -1,10 +1,10 @@ | |||
| use std::cell::OnceCell; | ||||
| use std::error::Error; | ||||
| use std::fs::File; | ||||
| use std::io::Read; | ||||
| use std::cell::OnceCell; | ||||
| use std::io::{Read, Write}; | ||||
| use std::process::{Command, Output, Stdio}; | ||||
| 
 | ||||
| use colored::Colorize; | ||||
| use pandoc::{PandocError, PandocOutput}; | ||||
| use reqwest::blocking::Client; | ||||
| use serde::Deserialize; | ||||
| use serde_json::json; | ||||
|  | @ -283,18 +283,41 @@ pub fn pad_ins_wiki(old_pad_content: String, wiki: &Mediawiki) -> Result<(), Box | |||
| } | ||||
| 
 | ||||
| /// Converts one file type into another using pandoc and saves the result as a txt file
 | ||||
| fn pandoc_convert( | ||||
|     old_pad_content: String, output_filepath: &str, input_format: pandoc::InputFormat, | ||||
|     output_format: pandoc::OutputFormat, | ||||
| ) -> Result<PandocOutput, PandocError> { | ||||
| fn pandoc_convert(old_pad_content: String) -> Result<String, Box<dyn Error>> { | ||||
|     let mut cmd = Command::new("pandoc") | ||||
|         .args(["--from", "markdown", "--to", "mediawiki", "--no-highlight"]) | ||||
|         .stdin(Stdio::piped()) | ||||
|         .stdout(Stdio::piped()) | ||||
|         .stderr(Stdio::piped()) | ||||
|         .spawn()?; | ||||
|     if let Some(mut stdin) = cmd.stdin.take() { | ||||
|         stdin.write_all(old_pad_content.as_bytes())?; | ||||
|     } | ||||
|     let mut output = String::new(); | ||||
|     if let Some(mut stdout) = cmd.stdout.take() { | ||||
|         stdout.read_to_string(&mut output)?; | ||||
|     } | ||||
|     let mut errmsg = String::new(); | ||||
|     if let Some(mut stderr) = cmd.stderr.take() { | ||||
|         stderr.read_to_string(&mut errmsg)?; | ||||
|     } | ||||
|     let status = cmd.wait()?; | ||||
|     if status.success() { | ||||
|         Ok(output) | ||||
|     } else { | ||||
|         Err( format!("Pandoc error, exit {:?}\n{}", status, errmsg).into() ) | ||||
|     } | ||||
|     /* | ||||
|     //Convert Markdown into Mediawiki
 | ||||
|     // Vanilla pandoc Befehl: pandoc --from markdown --to mediawiki --no-highlight
 | ||||
|     let mut p = pandoc::new(); | ||||
|     p.set_input(pandoc::InputKind::Pipe(old_pad_content)); | ||||
|     p.set_input_format(input_format, vec![]); | ||||
|     p.set_output(pandoc::OutputKind::File(output_filepath.parse().unwrap())); | ||||
|     p.set_output(pandoc::OutputKind::Pipe); // File(output_filepath.parse().unwrap()));
 | ||||
|     p.set_output_format(output_format, vec![]); | ||||
|     p.execute() | ||||
|     let output = p.execute()?; | ||||
|     Ok(output.into()) | ||||
|     */ | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
|  | @ -313,7 +336,8 @@ fn convert_md_to_mediawiki(old_pad_content: String) -> String { | |||
|     // TODO: use tempfile="3.3", make it a NamedTempFile::new()?;
 | ||||
|     //       or alternatively use piped stdout to avoid files entirely
 | ||||
|     let output_filepath: &str = "./pandoc_mediawiki.txt"; | ||||
|     pandoc_convert(old_pad_content, output_filepath, pandoc::InputFormat::Markdown, pandoc::OutputFormat::MediaWiki).expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); | ||||
|     pandoc_convert(old_pad_content) | ||||
|     .expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); | ||||
|     let temp = read_txt_file(output_filepath); | ||||
|     println!("TEMP: {}", temp.purple()); | ||||
|     temp | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 nobody
						nobody