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] | [dependencies] | ||||||
| stdext = "0.3.3" | stdext = "0.3.3" | ||||||
| pandoc = "0.8" |  | ||||||
| chrono = "0.4.38" | chrono = "0.4.38" | ||||||
| regex = "1.10.5" | regex = "1.10.5" | ||||||
| futures = "0.3.30" | futures = "0.3.30" | ||||||
|  |  | ||||||
|  | @ -1,10 +1,10 @@ | ||||||
|  | use std::cell::OnceCell; | ||||||
| use std::error::Error; | use std::error::Error; | ||||||
| use std::fs::File; | use std::fs::File; | ||||||
| use std::io::Read; | use std::io::{Read, Write}; | ||||||
| use std::cell::OnceCell; | use std::process::{Command, Output, Stdio}; | ||||||
| 
 | 
 | ||||||
| use colored::Colorize; | use colored::Colorize; | ||||||
| use pandoc::{PandocError, PandocOutput}; |  | ||||||
| use reqwest::blocking::Client; | use reqwest::blocking::Client; | ||||||
| use serde::Deserialize; | use serde::Deserialize; | ||||||
| use serde_json::json; | 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
 | /// Converts one file type into another using pandoc and saves the result as a txt file
 | ||||||
| fn pandoc_convert( | fn pandoc_convert(old_pad_content: String) -> Result<String, Box<dyn Error>> { | ||||||
|     old_pad_content: String, output_filepath: &str, input_format: pandoc::InputFormat, |     let mut cmd = Command::new("pandoc") | ||||||
|     output_format: pandoc::OutputFormat, |         .args(["--from", "markdown", "--to", "mediawiki", "--no-highlight"]) | ||||||
| ) -> Result<PandocOutput, PandocError> { |         .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
 |     //Convert Markdown into Mediawiki
 | ||||||
|     // Vanilla pandoc Befehl: pandoc --from markdown --to mediawiki --no-highlight
 |     // Vanilla pandoc Befehl: pandoc --from markdown --to mediawiki --no-highlight
 | ||||||
|     let mut p = pandoc::new(); |     let mut p = pandoc::new(); | ||||||
|     p.set_input(pandoc::InputKind::Pipe(old_pad_content)); |     p.set_input(pandoc::InputKind::Pipe(old_pad_content)); | ||||||
|     p.set_input_format(input_format, vec![]); |     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.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()?;
 |     // TODO: use tempfile="3.3", make it a NamedTempFile::new()?;
 | ||||||
|     //       or alternatively use piped stdout to avoid files entirely
 |     //       or alternatively use piped stdout to avoid files entirely
 | ||||||
|     let output_filepath: &str = "./pandoc_mediawiki.txt"; |     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); |     let temp = read_txt_file(output_filepath); | ||||||
|     println!("TEMP: {}", temp.purple()); |     println!("TEMP: {}", temp.purple()); | ||||||
|     temp |     temp | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 nobody
						nobody